Show Menu
Cheatography

Git cheat sheet Cheat Sheet (DRAFT) by

This cheat sheet was made following the Git course made by Mosh Hamedani.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Initial commands

git --version
Check yout git version
git config --global user.name "Your name"
Configure your name as default
git config --global user.email "Your email"
Configure your email as default
git config --global core.e­ditor "code --wait­"
Configure a default editor, here vscode is being used as default
git config --global -e
Open and edit your config­uration settings
git config --global core.a­utocrlf "­inp­ut/­tru­e"
How git should handle end of lines

Viewing staged and unstaged changes

git diff --staged
Staging area changes that are going to the next commit

Viewing history

git log
Show history of your repository
git log --oneline
Short history output
git log --oneline --reverse
Show history from the first commit
git log --oneline --stat
Show all the files that have been changed in each commit
git log --stat
More details about each commit
git log --oneline --patch
Full changes details in each commit
git log --oneline --auth­or=­"­Nam­e"
Commits from a given author
git log --oneline --afte­r|-­-be­for­e="Date | Relative date"
Commits from a given date
git log --oneline --grep­="So­met­ext­"
Commits that contain the specified text, case sensitive
git log --oneline -S"S­ome­tex­t"
Find all a commits that have added or removed the specified text
git log --oneline -S"S­ome­tex­t" --patch
Mixed with patch to show full details about the commit
git log --oneline hash..hash
Filter commits by a given range of commits
git log --oneline -- file.txt
Find commits that have modified a given file
git log --oneline --stat -- file.txt
Short output for changes over a given file
git log --oneline --patch -- file.txt
Full changes over a given file
git log --pret­ty=­for­mat­:"Your format­"
Customize the way you see output, then use alias for ease of use
git config --global alias.y­ou­ralias "log --pret­ty=­for­mat­:'Your format'
Alias example

Stashing

git stash push -m "Your messag­e"
Save your changes without committing them if you need to switch to a different branch. Stashing = Saving something in a safe place
git stash push --all -m "Your messag­e"
Stashing new untracked files
git stash list
Show a list of stashed files
git stash show stash@{0} | 0
Show specific stashed file by its index
git stash apply 0
Apply this stash to our working directory
git stash drop 1
Remove a specific stash
git stash clear
Remove all stashes

Viewing changes across commits

git diff HEAD~2 HEAD
Show differ­ences between a range of commits
git diff HEAD~2 HEAD file.txt
Same as above but for a single file
git diff HEAD~2 HEAD --name­-only
Show list of modified files in a given range of commits
git diff HEAD~2 HEAD --name­-status
Show list of files and the type of change for each one

About merging

Fast-f­orward
Fast forward merge can be performed when there is a direct linear path from the source branch to the target branch. In fast-f­orward merge, git simply moves the source branch pointer to the target branch pointer without creating an extra merge commit.
Three way merge
Fast-f­orward merge is not possible if the branches have diverged. Then we need a 3-way merge which uses a dedicated commit to merge two histories or you can say branches. This new commit is based on three different commits, the common ancestor of our branches which includes the before code and the tips of our branches which contains the after code.
Fast-f­orward contro­versy
Cons: Pollutes the history, linear history is preferred for some people
 
Pros: True reflection of history, easier to undo a feature

No fast forward merge

git merge --no-ff bugfix­/lo­gin­-form
Merges the specified branch without using fast forward merge
git config --global ff no
Disable fast forward in every repository

Managing merges

git merge bugfix­/si­gnu­p-form
Merges a branch into master
git log --oneline --all --graph
Shows a graph for easier unders­tanding of merges
git branch --merged
View list of branches that have been merged into master, it's safe to delete these branches
git branch -d bugfix­/si­gnu­p-form
Delete a branch
git branch --no-m­erged
View list of branches that have not been merged into master
git merge --abort
Abort a merge if you run into a conflict that you're not ready to fix
 

Managing your first repository

git init
Initialize your repository
git status
Get status of your current changes
git status -s
Short status inform­ation
git add "./f­ile­1|f­ile­2/*.tx­t"
Add files to the staging area for review

Commiting changes

git commit -m "Your messag­e"
Commit changes from your staging area and add message
git commit
Add a longer message for bigger or more detailed descri­ptions
git commit -am "Your messag­e"
Skip the staging area and commit changes directly
Tips
Commits shouldn't be too big or too short, also use present or past tense verbs but stick to only one and be clear with your messages

Restoring files

git restore --staged file.txt
Unstage or restore a file in the staging area taking the content from the latest commit
git restore --sour­ce=hash file.txt | HEAD~1 file.txt
Restore a file to an earlier version
git clean -fd
Discard local changes for new or modified files and direct­ories
Note
The restore command takes a copy from the next enviro­nment, for example, the working directory takes a copy from the staging area and the staging area takes it from the latest commit
git checkout hash file.txt
Restore file from a given commit hash

Viewing a commit

git show "hash | HEAD~1­"
Show what was changed in a given commit
git show "­has­h:f­ile­s/f­ile­1.txt | HEAD~1­:fi­les­/fi­le1.tx­t"
Show the content of a file in a given commit
git ls-tree "hash | HEAD~1­"
Show all files in a given commit
git show HEAD~1 --name­-only
Show files that have been modified in a given commit
git show HEAD~1 --name­-status
Show files + status: added, deleted, modifi­ede­tc...

Blaming

git blame file.txt
Show who modified a given file
git blame -e file.txt
With email
git blame -e -L 1,3 file.txt
With a range of specific lines

Working with branches

git branch
Show a list of existing branches
git branch name
Create a new branch with a given name
git switch name
Switch to a different branch
git switch -C name
Create and switch to a branch
git branch -m name bugfix­/si­gnu­p-form
Change the name of a branch
git diff master..b­ugf­ix/­sig­nup­-form
See differ­ences between branches
git diff bugfix­/si­gnu­p-form
If you're already in master there is no need to specify it
git branch -d bugfix­/si­gnu­p-form
Delete a branch after it has served its purpose
git branch -D bugfix­/si­gnu­p-form
Force deletion if you want to discard any changes made in this branch
 

Removing files

git ls-files
Show current files in your staging area
git rm "./f­ile­1|f­ile­2/*.tx­t"
Remove files from the current directory and staging area at the same time

Renaming or Moving files

git mv oldnam­e.txt newnam­e.txt
Move or rename files in the working directory and staging area at the same time

Ignore files

Create .gitignore
Include any files that you want to ignore here, examples: logs/ | main.log | *.log
Note
This only ignores files or direct­ories if they have not been committed in the repository before
git rm --cached -r logs/
Remove directory that was already committed by accident to start ignoring it with .gitignore

Checking out a commit

git checkout "­has­h"
Go back in time and check a previous commit, this will show the state of every file as it was at that point in time
git log --oneline --all
You will need to add the parameter --all to show every commit when you're checking an old commit
git checkout master
Go back to your latest commit

Finding contri­butors using shortlog

git shortlog
Show people that have contri­buted to the project
git shortlog -n
Sorted by number of commits per author
git shortlog -n -s
Suppress the commit messages
git shortlog -n -s -e
Show email address
git shortlog -n -s -e --befo­re=­"­" --afte­r=""
Show contri­butors for a given date

Tagging

git tag
Show a list of existing tags
git tag -n
With their messages
git tag v1.0
Create a tag for the current latest commit
git tag v1.0 hash
Create a tag for a specific commit
git checkout v1.0
Then you can reference a commit by its tag
Note
Git supports two types of tags: lightw­eight and annotated. A lightw­eight tag is very much like a branch that doesn’t change­ — it’s just a pointer to a specific commit. Annotated tags, however, are stored as full objects in the Git database. They’re checks­ummed; contain the tagger name, email, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG). It’s generally recomm­ended that you create annotated tags so you can have all this inform­ation; but if you want a temporary tag or for some reason don’t want to keep the other inform­ation, lightw­eight tags are available too.
git tag -a v1.1 -m "Your messag­e"
Create an annotated tag and provide a message to it
git show v1.1
Show commit by its tag
git tag -d v1.1
Delete a tag