Git cheat sheet
The Git commands you reach for most, grouped by task and using the modern switch and restore forms. Copy, paste, and replace the placeholder names.
Each block is a group of related commands. Names like feature/x, a1b2c3d, and file.txt are placeholders.
Set up and start
git config --global user.name "Your Name"
git config --global user.email [email protected]
git config --global init.defaultBranch main
git init # make the current folder a repository
git clone https://github.com/owner/repo.git
Everyday
git status # what changed?
git diff # unstaged changes
git diff --staged # staged changes
git add file.txt # stage a file
git add -p # stage piece by piece
git commit -m "Describe the change"
git commit --amend # fix the last commit (before pushing)
Branches
git branch # list local branches
git branch -a # include remote branches
git switch feature/x # go to a branch
git switch -c feature/x # create and go
git switch - # back to the previous branch
git merge feature/x # merge into the current branch
git rebase main # replay current branch onto main
git branch -d feature/x # delete if merged
git branch -m new-name # rename the current branch
Sync with a remote
git remote -v # which remotes, which URLs
git fetch # download, change nothing local
git pull # fetch + merge (or rebase)
git pull --rebase
git push
git push -u origin feature/x # first push of a new branch
git push --force-with-lease # after rewriting your own branch
git fetch --prune # forget deleted remote branches
Undo
Check before you run this
- Can permanently discard commits. Make sure you have a backup branch.
git restore file.txt # discard unstaged edits
git restore --staged file.txt # unstage
git reset --soft HEAD~1 # undo last commit, keep changes staged
git reset HEAD~1 # undo last commit, keep changes unstaged
git reset --hard HEAD~1 # undo last commit and discard changes
git revert a1b2c3d # new commit that undoes an old one
git reflog # find "lost" commits
History
git log --oneline --graph --all
git log -p -- file.txt # changes to one file
git log -S 'text' # commits that added or removed text
git show a1b2c3d
git blame file.txt
git diff main..feature/x
Stash
git stash push -m "message"
git stash push -u # include untracked files
git stash list
git stash pop
git stash drop stash@{0}
Tags
git tag v1.2.0
git tag -a v1.2.0 -m "Release 1.2.0"
git push origin v1.2.0
Find a bug
git bisect start HEAD v1.0
git bisect run ./test.sh
git bisect reset
Clean up
git branch --merged main # branches already merged
git clean -nd # preview what would be deleted
git clean -fd # delete untracked files and folders
git clean -fd deletes files Git is not tracking and they do not go to the trash. Always run git clean -nd first.
Every command has a manual page: git help switch or git switch -h for a short summary.
See also
- 0032 First-time Git setup: name, email, default branch, SSH or HTTPS Tell Git who you are, pick sensible defaults, and choose between SSH and HTTPS for GitHub. Includes a small script that applies… Beginner · How-to · Linux, macOS, Windows Script Setup 15 Oct 2026
- 0033 The everyday Git workflow: status, add, commit, push, pull Git moves your changes through three places: the working folder, the staging area, and the commit history. These five commands, plus .gitignore… Beginner · Concept · Any system Article Housekeeping · Setup 15 Oct 2026
- 0034 Merging vs rebasing in Git Merge and rebase both bring commits from one branch into another. Merge keeps history exactly as it happened; rebase replays your commits… Intermediate · Concept · Any system Article Branching & rebasing 15 Oct 2026
- 0035 Undoing changes in Git: restore, reset, revert, and reflog Pick the undo command by where the mistake lives: in a file, in the staging area, in a local commit, or in… Intermediate · Concept · Any system Article Undoing changes 15 Oct 2026
- 0036 Finding the commit that broke it: log, blame, and git bisect When something used to work and now does not, Git can point at the commit that changed it. Start with log and… Advanced · How-to · Any system Article Finding bugs · Reading history 15 Oct 2026
- 0040 Fix: error: failed to push some refs to … Git refused your push, usually because the remote branch has commits you do not have yet. Bring them in, combine them with… Beginner · Fix · Any system Article Common errors 15 Oct 2026
Repos in the wild
- git-extras tj/git-extras A collection of extra Git subcommands for common chores, such as summarizing a repository, deleting merged branches, and undoing commits. GitHub
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- commitpedia-admin +3,249 chars