Git cheatsheet

Git commands for everyday work, searchable and one click to copy.

loading Git cheatsheet
About this tool

What is a Git cheatsheet?

Git has a few dozen commands that people use every day and a few hundred more that they look up once a month. A cheatsheet is the short list you keep open next to the terminal: the command for the thing you want to do, ready to copy, so you do not have to remember whether the flag is -d or -D. This one is organised by task instead of alphabetically, because you rarely start from the name of a command. You start from a situation: I committed to the wrong branch, I need to undo my last commit, I deleted a branch by mistake.

The commands are grouped in eleven sections: configuration, get started, stage and commit, branches, merge and rebase, remotes and sync, stash, inspect, tags, "I've made a mistake" and worktrees and submodules. Each item has a plain description and one to four commands. Placeholders are written in square brackets, such as [branch-name], [commit-hash], [url] and [file], and N stands for a number; replace them before you run the command.

Some commands here rewrite history or delete work, such as git reset --hard and git push --force-with-lease, and the descriptions say so. Read them before pasting. The page is plain reference material that loads with your browser, with no account and no server. For the same idea applied to another tool, see the regular expressions cheatsheet.

How to use the Git cheatsheet

  1. Open the page and scroll: the sections appear in the order of a normal working day, from the first-time setup to the recovery tricks at the end.
  2. Type in the search box to filter. It looks at the description, the section title and the commands themselves, and every word you type has to match, so undo commit finds the items about undoing commits. The search ignores accents and capital letters. In Portuguese and Spanish you can search in your language or in English: the English description still counts.
  3. While you search, a counter at the bottom tells you how many commands were found. If nothing matches, the page says so and suggests other words such as undo, branch or stash.
  4. Press the round copy button next to a command to copy it. When an item has several commands, they are copied together, one per line, in the order you would run them.
  5. Use the copy section button in a section title to take all the commands of that section.
  6. Replace the placeholders in square brackets, then run the command in your terminal.

When you want to see exactly what a command changed in a file, compare the two versions with the text diff.

Examples

Undo the last commit and keep the changes

Item "Undo most recent commit and keep changes". Command:

git reset HEAD~1

Git prints what is left uncommitted, and the file is changed but no longer committed:

Unstaged changes after reset:
M	app.txt

git status -s then shows M app.txt. Nothing is lost; the commit is gone from the branch and the edits are back in your working directory.

Switch branches and come back

git switch -c feature/login
git switch -
git branch

The first command prints Switched to a new branch 'feature/login', the second returns to the previous branch with Switched to branch 'main', and git branch lists them, with * before the current one:

  feature/login
* main

See what git clean would delete

Item "Remove untracked files (dry run first)":

git clean -n
Would remove junk.log

Only after reading that list, run git clean -fd to delete for real. The first command deletes nothing.

Recover a deleted branch

You deleted a branch with git branch -D tmp and Git answered Deleted branch tmp (was 897592c). Find the commit and create the branch again:

git reflog
git branch tmp 897592c

git reflog lists every place HEAD has been, such as commit: onlytmp and checkout: moving from tmp to main, so you can copy the hash of the last commit of the lost branch.

Common errors and how to fix them

  • error: Your local changes to the following files would be overwritten by merge: Git refuses because a file has edits that the operation would overwrite. Commit them, or put them aside with git stash, run the operation again and then git stash pop.
  • fatal: not a git repository (or any of the parent directories): .git: you are not inside a repository. Move to the project folder, or create one with git init or git clone [url].
  • ! [rejected] main -> main (non-fast-forward): the remote has commits you do not have. Run git pull --rebase and push again. Use git push --force-with-lease only on your own branch after rewriting history on purpose; it refuses to overwrite commits you have not seen.
  • You are in 'detached HEAD' state: you checked out a commit instead of a branch. Go back with git switch [branch-name], or keep the work with git switch -c [branch-name].
  • No command matches your search: remove a word. All words must match at once. Try a simpler one such as undo, branch, stash or tag.

Frequently asked questions

How do I undo the last commit in Git?

To keep the changes, run git reset HEAD~1. To throw them away too, git reset HEAD~1 --hard, which cannot be undone easily. If the commit was already pushed and others may have it, use git revert [commit-hash], which adds a new commit that cancels the old one.

What is the difference between git reset and git revert?

reset moves the branch back and rewrites history, which is fine on your own local work. revert leaves history alone and adds a commit with the opposite change, which is the safe choice on shared branches.

What does git push --force-with-lease do?

It forces the push like --force, but only if the remote branch is still where you last saw it. If someone pushed in the meantime, it stops instead of erasing their work.

Should I use merge or rebase?

git merge [branch-name] joins two lines of work and keeps their history, with a merge commit when needed (--no-ff always makes one). git rebase [branch-name] replays your commits on top of the other branch for a straight history. Do not rebase commits that others already use.

Report a problem