Git Cheat Sheet
A compact reference of everyday Git commands. Commands on the left, descriptions on the right.
Setup
git config --global user.name "Your Name"
set a name that is identifiable for credit when review version history
git config --global user.email "[email protected]"
set an email address that will be associated with each history marker
git config --global color.ui auto
set automatic command line coloring for Git
Setup & Init
git init
initialize an existing directory as a Git repository
git clone <url>
retrieve an entire repository from a hosted location via URL
Stage & Snapshot
git status
show modified files in working directory, staged for your next commit
git add <file>
add a file as it looks now to your next commit (stage)
git reset <file>
unstage a file while retaining the changes in working directory
git diff
diff of what is changed but not staged
git diff --staged
diff of what is staged but not yet committed
git commit -m "message"
commit your staged content as a new commit snapshot
Branch & Merge
git branch
list your branches; a * will appear next to the current branch
git branch <name>
create a new branch called <name>
git switch <name> # or: git checkout <name>
switch to another branch and check it out into your working directory
git merge <name>
merge the specified branch’s history into the current one
git log --oneline --graph --decorate --all
show a graphical log of commits across branches
Inspect & Compare
git log
show commit history for the currently active branch
git log <branchA>..<branchB>
show commits on branchB that are not on branchA
git diff <branchA>..<branchB>
show diff of what is in branchB that is not in branchA
git show <commit>
show any object in Git in human-readable format
Tracking Path Changes
git rm <file>
delete the file from project and stage the removal for commit
git mv <existing> <new>
change an existing file path and stage the move
git log --stat -M
show all commit logs with indication of any paths that moved
Share & Update
git remote add origin <url>
add a git URL as an alias
git fetch [alias]
fetch down all the branches from that Git remote
git merge [alias]/[branch]
merge a remote branch into your current branch to bring it up to date
git push [alias] [branch]
Transmit local branch commits to the remote repository branch
git pull
fetch and merge any commits from the tracking remote branch
Rewrite History
git reset --hard <commit>
reset your HEAD pointer to a previous commit and discard all changes since then
git revert <commit>
create a new commit that undoes all of the changes made in <commit>
git commit --amend
amend the most recent commit
Temporary Commits
git stash
save modified and staged changes
git stash list
list stack-order of stashed file changes
git stash pop
write working from top of stash stack
git stash drop
discard the changes from top of stash stack
Undoing Changes
git restore <file> # or: git checkout -- <file>
discard changes in working directory to a file
git restore --staged <file> # or: git reset HEAD <file>
unstage a file while retaining the changes
git clean -fd
remove untracked files and directories