Git Commands, with Undo Recipes

Search by what you want to do, such as "undo commit" or "wrong branch". Each command says what it does in plain words, and the recipes at the end get you out of the usual messes.

How it works

Git keeps a history of commits, and a branch is only a label that points at one of them. Most commands either make new commits, move labels around, or copy commits between your computer and a remote such as GitHub. Knowing which of the three a command does tells you how risky it is.

This list uses the modern commands git has had since version 2.23: git switch for changing branches and git restore for files. They split up the old git checkout, which did both and was easy to misuse. The older forms still work.

The undo recipes are the part worth bookmarking. Almost nothing in git is lost once it has been committed, because git reflog remembers every commit you have been on, for at least 30 days by default. The recipes say which steps are safe and which throw work away.

How to use it

  1. 1 Type what you want to do in plain words, such as "rename branch" or "unstage".
  2. 2 Read the explanation, and any warning about pushed commits.
  3. 3 Press Copy, paste into your terminal, and replace the parts in angle brackets, such as <file>.
  4. 4 For a recipe with several lines, run them one at a time and check git status between them.

Good to know

  • Run git status before and after anything unfamiliar. It shows the state and usually suggests the next command.
  • Never rewrite commits other people have already pulled. Use git revert for those.
  • If you must overwrite a remote branch, use git push --force-with-lease, which refuses if someone else pushed meanwhile.
  • HEAD~1 means "the commit before the current one", HEAD~2 the one before that.

Browser support

  • Chrome 94 and later
  • Firefox 93 and later
  • Safari 16.4 and later
  • Edge 94 and later

These are the oldest versions with everything the tool uses. They are worked out from the tool itself, so they change when it does.

Version numbers checked on 2026-09-04.

Walking through "I committed to the wrong branch"

The most common git mistake is also a good lesson in how git works. You meant to start a new branch for a fix, forgot, and committed straight onto main. You have not pushed yet. git log --oneline shows the new commit, say f3a9c21 "Fix login timeout", on top of main's previous commit, say 8b1e0d4. The goal is to have the fix on its own branch and main back where it was.

The recipe is three commands, run on main. First, git branch fix-login-timeout. That creates a new label pointing at the current commit, f3a9c21, but leaves you on main. Your fix is now safe on a branch, whatever happens next. Second, git reset --hard HEAD~1. HEAD~1 means one commit before the current one, which is 8b1e0d4, so main moves back there and your files change to match. Third, git switch fix-login-timeout, and your fix is in front of you again, on the branch where it belongs.

It is worth checking each step, and the commands to do it are cheap. After the first step, git log --oneline --all shows both main and fix-login-timeout pointing at f3a9c21. After the second, main points at 8b1e0d4 while the new branch still points at f3a9c21. Nothing was deleted: the commit is still there, it just has one label instead of two. That is the key idea behind most git rescues. Branches are labels, and moving a label never destroys a commit while another label, or the reflog, still points at it.

There are two warnings, and the recipe on this page says them too. git reset --hard throws away any changes you have not committed, so commit or stash them first. And if you had already pushed main, rewriting it would cause trouble for everyone who pulled it. In that case, create the branch the same way, then on main run git revert f3a9c21 and push. That adds a new commit undoing the fix on main, while the branch keeps it.

This page has honest limits. It covers the commands most people use every week, not all of git, which has well over a hundred commands and thousands of options. It does not run anything: it only explains and copies. The explanations describe git 2.23 and later, which is what any computer updated in the last few years has. And when a situation is truly tangled, such as a rebase halfway through with conflicts, git status and git reflog will tell you more about your own repository than any cheat sheet can.

Frequently asked questions

What is the difference between reset --soft, --mixed and --hard?

All three move the branch back to an older commit. --soft keeps the undone changes staged. --mixed, the default, keeps them in your files but unstaged. --hard throws them away, along with any uncommitted work. Only --hard can lose work.

What is the difference between git revert and git reset?

git reset moves the branch back, as if the commits never happened, which rewrites history. git revert adds a new commit that undoes an old one, so history only grows. Use revert for anything already pushed to a shared branch.

Should I use git checkout or git switch?

Either works. git switch changes branches and git restore changes files; together they do what git checkout did, with less chance of overwriting a file when you meant to change branch. This page uses the newer forms.

I deleted a branch. Is it gone?

Usually not. When git deletes a branch it prints the commit it pointed at, such as "was abc1234". Run git switch -c <name> abc1234 to bring it back. If you missed that line, find the commit in git reflog.

Related tools

Your files are processed on your device and are not uploaded. You can verify this in your browser’s developer tools under the Network tab.