Skip to content
gohacks.dev
Tooling

Git Cheat Sheet

A searchable Git cheat sheet: switching and restoring, safe force pushes, interactive rebase and fixups, stash, log searches, blame, bisect, reflog, undo and worktrees.

Free · No sign-up · Your inputs stay in your browser

Reference

Git cheat sheet

CommandDoesNotes
git switch -c feature/xCreate a branch and switch to itThe modern form of git checkout -b.
git restore fileDiscard unstaged changes to a filegit restore --staged file unstages instead.
git add -pStage changes hunk by hunkReview what goes into the commit.
git commit --amend --no-editAdd staged changes to the last commitOnly before pushing, or push with --force-with-lease.
git push --force-with-leaseForce push safelyRefuses if someone else pushed in the meantime; plain --force does not.
git pull --rebaseUpdate without a merge commitgit config --global pull.rebase true makes it the default.
git rebase -i HEAD~5Squash, reorder or reword recent commitsInteractive; mark commits fixup or squash.
git commit --fixup=<sha> && git rebase -i --autosquash <sha>~1Fix an older commitThe fixup is folded into the commit automatically.
git stash push -m "wip" && git stash popPark and restore work in progressgit stash list shows them; -u includes untracked files.
git log --oneline --graph --allSee the branch structureAdd --decorate for branch names on older Git.
git log -S 'functionName' --onelineFind commits that added or removed a stringThe pickaxe: how to find when code appeared.
git log --follow -p -- path/to/fileHistory of one file, across renames
git blame -w -C -L 10,40 fileWho last changed lines 10-40-w ignores whitespace, -C follows code moved from other files.
git bisect start BAD GOODBinary-search for the commit that broke somethingThen git bisect good / bad until it names the commit; git bisect run automates it.
git reflogEvery position HEAD has been inHow to recover a branch lost to a reset or rebase.
git reset --soft HEAD~1Undo the last commit, keep the changes staged--mixed (default) unstages them, --hard deletes them.
git revert <sha>Undo a pushed commit with a new commitSafe on shared branches; reset rewrites history.
git cherry-pick <sha>Apply one commit onto the current branch-x records where it came from.
git clean -fdnPreview deleting untracked filesRemove the n to actually delete; add -x for ignored files too.
git branch --merged main | grep -v main | xargs git branch -dDelete local branches already merged
git fetch --pruneDrop remote-tracking branches deleted on the servergit config --global fetch.prune true makes it the default.
git worktree add ../hotfix mainCheck out a second branch in another folderNo stashing needed to work on two branches at once.
git diff --stagedWhat will be committedgit diff alone shows unstaged changes.
git diff main...featureWhat a branch changed since it forkedThree dots: changes on feature only, as a pull request shows them.
git shortlog -sn --no-mergesCommits per author
git tag -a v1.2.0 -m "v1.2.0" && git push --tagsCreate and push an annotated tagAnnotated tags carry a date and author; release tools expect them.
git config --global init.defaultBranch mainName new repositories' first branch main
git rm --cached fileStop tracking a file but keep it on diskThen add it to .gitignore.
git submodule update --init --recursiveFetch submodules after cloningOr clone with --recurse-submodules.
git maintenance startSchedule background repository optimisationKeeps large repositories fast.

For Git 2.23 and later, which split checkout into switch (branches) and restore (files). Anything that rewrites history (amend, rebase, reset) is safe on commits you have not pushed; on shared branches, prefer revert.

The modern commands

Git 2.23 split checkout into switch, for branches, and restore, for files, because one command doing both was a common source of lost work. The older forms still work. The Git reference documents every command.

Rewriting history safely

amend, rebase and reset rewrite commits, which is fine for commits nobody else has. Once pushed to a shared branch, prefer revert, which adds a new commit, and if a rewrite is needed, push with --force-with-lease so you do not overwrite someone else's work.

Share

Frequently asked questions

How do I undo the last commit but keep the changes?
git reset --soft HEAD~1 keeps them staged; git reset HEAD~1 keeps them unstaged. The Git undo helper covers other cases.
How do I find which commit introduced a bug?
git bisect start BAD GOOD, then mark each checked-out commit good or bad, or let git bisect run execute a test script.
Is what I paste sent anywhere?
No. The tool runs entirely in your browser with JavaScript. Nothing you type or paste is sent to our server or stored.

Keep planning

Similar tools