Git Cheat Sheet: the commands you actually reach for day to day, grouped by what you are trying to do — configuring Git once, starting or cloning a project, inspecting and staging local changes, stashing work, browsing history, working with branches and tags, syncing forks and remotes, merging or rebasing, and undoing mistakes.
Git Configuration
Set the name that will be seen in commits and tags
git config --global user.name "Robert Eisele" Set the e-mail address attached to commits and tags
git config --global user.email "robert@example.org" Enable colorization of Git output
git config --global color.ui auto Starting a Project
Initialize a new local repository in the current folder
git init
Clone an existing repository with its entire history from a URL
git clone git@github.com:foo/bar.git Local Changes
Display changed files in the working directory
git status
Show changes of tracked files in the working directory and the staging area
git diff [file]
Show changes between the staging area and the repository
git diff --staged [file]
Add all current changes to the staging area for the next commit
git add .
Add some file changes to the staging area for the next commit
git add -p <file>
Undo git add without touching the file
git restore --staged <file>
Stop tracking a file but keep the local copy
git rm --cached <file>
Commit all local changes in tracked files
git commit -a -m "<commit message>" Commit all added files
git commit -m "<commit message>" Add staged changes to the previous local commit without changing its message
git commit --amend --no-edit
Reset the author of the previous local commit to the current Git identity
git commit --amend --reset-author --no-edit
Stashing
Temporarily store tracked changes with a descriptive label
git stash push -m "<message>" Stash tracked and untracked files
git stash push -u -m "<message>" List saved stashes
git stash list
Show the complete patch stored in the latest stash
git stash show -p
Show the complete patch stored in an arbitrary stash
git stash show -p stash@{1} Apply a stash while keeping it in the stash list
git stash apply stash@{0} Apply the latest stash and remove it after a successful merge
git stash pop
Delete a stash without applying it
git stash drop stash@{0} Commit History
Show all commits
git log
Show a compact graph of all branches and tags
git log --oneline --graph --decorate --all
Show changes for a specific file
git log -p <file>
Figure out who changed a file, line by line
git blame <file>
Show recent movements of HEAD to recover lost commits
git reflog
Branches
List all existing branches
git branch
Switch to another branch
git switch <branch> Create a new branch
git branch <branch>
Create and switch to a new branch in one step
git switch -c <branch> Push a new branch and configure its upstream tracking branch
git push -u <remote> <branch>
Create a local branch that tracks an existing remote branch
git fetch <remote>
git switch --track <remote>/<branch> Delete a branch locally
git branch -d <branch>
Delete a branch on the remote
git push <remote> --delete <branch> Tags
Add a tag, like v1.0.0, to mark the current commit
git tag <tag>
Push the new tag to a remote
git push <remote> <tag>
Delete a tag locally
git tag -d <tag>
Delete a tag on the remote
git push <remote> --delete <tag> Remotes and Forks
Add a new remote upstream
git remote add <remote> <url>
Fetch all changes from a remote
git fetch <remote>
Fetch and merge from a remote
git pull <remote> <branch>
Push local commits to a remote
git push <remote> <branch>
List all currently configured remotes
git remote -v
Show information about a remote
git remote show <remote>
Rebase a fork's default branch onto the upstream repository
git fetch upstream
git switch <default-branch>
git rebase upstream/<default-branch>
git push --force-with-lease origin <default-branch> Merge and Rebase
Merge a branch into the current HEAD
git merge <branch>
Rebase the current HEAD onto <branch>
git rebase <branch>
Interactively reorder, edit, or squash the last commits
git rebase -i HEAD~<count> Squash the last commits by keeping their combined changes staged
git reset --soft HEAD~<count>
git commit Abort a rebase
git rebase --abort
Continue a rebase after resolving conflicts
git rebase --continue Open the configured merge tool to resolve conflicts
git mergetool
After fixing a conflict manually, mark the file as resolved
git add <file> git rm <file>
Rebase and soft-reset squashing replace existing commits. Avoid rewriting shared history; if the branch was already published and rewriting is coordinated, update it with git push --force-with-lease, never plain --force.
Undo
Undo the previous commit, keep changes staged
git reset --soft "HEAD~1" Discard all tracked local changes irreversibly
git reset --hard HEAD Discard local changes in a specific file
git restore <file>
Revert a commit by creating a new, opposite commit
git revert <commit>
Reset HEAD to a previous commit and discard changes
git reset --hard <commit>
… and keep the changes as unstaged
git reset <commit>
… and preserve uncommitted local changes
git reset --keep <commit>
Find and restore a deleted file
git rev-list -n 1 HEAD -- <file>
git restore --source=<commit>^ -- <file>