Account

What new tool do you want?

Developer Tools

Git Commands Cheat Sheet

The Git commands you actually need — setup, branching, undoing mistakes, stashing, rebasing and working with remotes — with what each does.

How to use

  1. 1Search for a task, like 'undo' or 'branch'.
  2. 2Copy the command and replace the placeholders in <angle brackets>.

Frequently asked questions

How do I undo my last commit but keep the changes?

git reset --soft HEAD~1 moves the branch back one commit and leaves the changes staged.

What's the difference between merge and rebase?

Merge joins histories with a merge commit. Rebase replays your commits on top of another branch for a linear history — don't rebase commits others already pulled.

How do I get back something I deleted?

git reflog lists every position HEAD has been in. Find the commit and git checkout or git reset to it.

A worked example of the three undo levels

The three "Undo" commands that look similar do genuinely different things, in increasing order of how much they touch: git restore <file> discards uncommitted changes to a tracked file, leaving your commit history untouched. git reset --soft HEAD~1 moves your branch pointer back one commit but leaves all those changes staged, ready to re-commit differently — nothing is deleted, just uncommitted again. git reset --hard HEAD~1 does the same pointer move but also discards the actual file changes, which is the one genuinely dangerous command in the list — anything not committed or stashed elsewhere is simply gone.

Why reflog is the safety net underneath all of this

Even after a git reset --hard, the commit you reset away from isn't immediately garbage-collected — git keeps a local log of every position HEAD has pointed to (git reflog), typically for 90 days by default. That means a "deleted" commit from an accidental hard reset is usually still recoverable: find it in the reflog output and git reset --hard back to that SHA. This is specifically a local, per-repository safety net — it doesn't survive a fresh clone and doesn't help if you never had the commit locally to begin with.

Why rebase and revert both exist for "undoing," but for different audiences

git revert creates a new commit that reverses an old one, leaving the original commit intact in history — which is the only safe option once a commit has been pushed and others may have pulled it, since it doesn't rewrite anything anyone else already has. git rebase replays commits onto a new base, rewriting commit hashes in the process — fine for commits that exist only in your local, unpushed branch, but genuinely disruptive if you rebase commits others have already based their own work on, since their history and yours diverge. The rule of thumb baked into this distinction: rebase before you push, revert after.

The first rows are the name and email, and both say global

The sheet starts with setup, not with commit. The first command sets the name recorded in commits, and the second sets the email, and both are written as global, so they are not limited to the repository I am standing in. Start is either init, which creates a repository in the current folder, or clone, which downloads the history as well as the files. Status is described as showing changed, staged, and untracked files. Add is a single row with two forms, one file or everything. Amend is described as changing the last commit, message or content. Diff is one row too, with the plain form and the staged form side by side.

Related tools

More: Developer Tools