N git cheat sheet: branching, merge vs rebase, stash, reset vs revert and cherry-pick - Netscape _ □ ×
Back Forward Home Reload Guides Sign
URL: http://www.nicnonac.com/guides/git-cheat-sheet/
☷ git cheat sheet: branching, merge vs rebase, stash, reset vs revert and cherry-pick - nicnonac guides _□×

← back to guides

git cheat sheet: branching, merge vs rebase, stash, reset vs revert and cherry-pick

Stacks · 12 min read · posted Nov 30, 2026, 2:45 pm

Most of the git you need day to day comes down to five things: branching, merge vs rebase, stash, reset vs revert, and cherry-pick. Once you can picture what each one does to your history, the commands stop being guesswork.

This is the cheat sheet from the reel. For each of the five you get when you'd reach for it, the commands, what the history looks like before and after, the mistake that usually bites, and how to back out of it. There's a quick lookup table first, and a section at the end on undoing almost anything with git reflog.

Everything here was run with git 2.55.0 on macOS in a throwaway repo, and the output is pasted the way git printed it. Each section starts from the same two commits, 6b40555 add readme and ce12668 add app. The commits were made with a fixed date, so a commit's hash stays the same from one section to the next. Your hashes will be different, so swap in your own.

The history views use git log --oneline --graph --all --decorate. In a terminal you can leave off --decorate and git still shows which branch points where. It's there because this output was captured by a script, and git leaves the labels out when it isn't printing to a terminal.

the quick lookup

i want to ... run
start a new branch from where i am git switch -c login
go back to main git switch main
bring a branch into main and keep its history as it happened git switch main, then git merge login
move my branch on top of the latest main git switch login, then git rebase main
get out of a merge or rebase that hit a conflict git merge --abort or git rebase --abort
push a branch after rebasing it git push --force-with-lease
put my changes aside for a minute git stash, later git stash pop
put them aside, new files included git stash push -u
undo my last commit but keep the changes git reset --soft HEAD~1
throw away my last commit and its changes git reset --hard HEAD~1
undo a commit that's already pushed git revert HEAD
copy one commit from another branch git cherry-pick 1a4d4a0
unstage a file git restore --staged notes.txt
throw away my changes to one file git restore app.py
find a commit i thought was gone git reflog
delete a branch git branch -d login (-D if it isn't merged)

what each one does to your history

command what happens
branch adds a label to a commit. no commits change
merge adds one commit with two parents. nothing is rewritten
rebase copies your commits onto a new starting point. the copies get new hashes and the old commits drop off the branch
stash your branch doesn't change. uncommitted changes go onto a separate stack
reset moves the branch back. the commits after that point drop off the branch
revert adds a new commit that does the opposite of an earlier one
cherry-pick adds a copy of one commit, with a new hash

1. branching

When you reach for it. You're about to start something: a feature, a fix, an experiment. A branch keeps that work apart from main until it's ready.

The idea. A branch is a label that points at a commit. HEAD marks the branch you're on. When you commit, the branch you're on moves forward to the new commit and every other branch stays where it was. git switch -c puts a new label on the commit you're at and moves HEAD to it, in one step.

If you've read git for vibe coders, git checkout -b there does the same job as git switch -c here. git switch is the newer command, and it only deals with branches, so it's harder to mix up with the other things checkout does.

Before.

$ git log --oneline --graph --all --decorate
* ce12668 (HEAD -> main) add app
* 6b40555 add readme

The commands.

$ git switch -c login
Switched to a new branch 'login'
$ git branch
* login
  main
$ git add .
$ git commit -m "add login form"
[login 83514e4] add login form
 1 file changed, 2 insertions(+)
 create mode 100644 login.py

After. The new commit is on login. Main hasn't moved.

$ git log --oneline --graph --all --decorate
* 83514e4 (HEAD -> login) add login form
* ce12668 (main) add app
* 6b40555 add readme

What bites. Committing on main when you meant to be on a branch. The fix is to put a branch label on that commit, then move main back one:

$ git switch main
Switched to branch 'main'
$ git commit -m "add signup form"
[main 92ca18e] add signup form
 1 file changed, 2 insertions(+)
 create mode 100644 signup.py
$ git branch signup
$ git reset --hard HEAD~1
HEAD is now at ce12668 add app
$ git log --oneline --graph --all --decorate
* 83514e4 (login) add login form
| * 92ca18e (signup) add signup form
|/  
* ce12668 (HEAD -> main) add app
* 6b40555 add readme

The commit now lives on its own branch and main is back where it was. reset --hard also throws away any uncommitted changes, so commit or stash them first (sections 3 and 4).

How to back out. git switch - takes you back to the branch you were on before. git branch -d deletes a branch, and refuses when its commits aren't merged anywhere, so you don't lose them by accident. -D deletes it anyway.

$ git switch signup
Switched to branch 'signup'
$ git switch -
Switched to branch 'main'
$ git branch -d signup
error: the branch 'signup' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D signup'
hint: Disable this message with "git config set advice.forceDeleteBranch false"
$ git branch -D signup
Deleted branch signup (was 92ca18e).

2. merge vs rebase

When you reach for it. Your branch is ready to go into main, or main has moved on since you branched and you want its changes.

The idea. Both end with main's commits and yours in one line of work. A merge adds one new commit whose two parents are the tips of both branches, and leaves every existing commit alone. A rebase replays your commits one at a time on top of main. Replayed commits are new commits, with new hashes.

Before. Main got update readme after login split off.

$ git log --oneline --graph --all --decorate
* 5d4e29e (login) add login tests
* 83514e4 add login form
| * c53b34e (HEAD -> main) update readme
|/  
* ce12668 add app
* 6b40555 add readme

After a merge. Run from main. Git opens your editor for the merge commit's message; save and close it to keep the default.

$ git merge login
Merge made by the 'ort' strategy.
 login.py      | 2 ++
 test_login.py | 1 +
 2 files changed, 3 insertions(+)
 create mode 100644 login.py
 create mode 100644 test_login.py
$ git log --oneline --graph --all --decorate
*   ff200e1 (HEAD -> main) Merge branch 'login'
|\  
| * 5d4e29e (login) add login tests
| * 83514e4 add login form
* | c53b34e update readme
|/  
* ce12668 add app
* 6b40555 add readme
$ git branch -d login
Deleted branch login (was 5d4e29e).

The fork stays in the history, and ff200e1 joins the two lines.

After a rebase. Same starting point, run from login this time.

$ git switch login
Switched to branch 'login'
$ git rebase main
Rebasing (1/2)
Rebasing (2/2)
Successfully rebased and updated refs/heads/login.
$ git log --oneline --graph --all --decorate
* 01109b5 (HEAD -> login) add login tests
* fa2c142 add login form
* c53b34e (main) update readme
* ce12668 add app
* 6b40555 add readme

One straight line. The two login commits now sit on top of update readme, and their hashes changed: 83514e4 became fa2c142 and 5d4e29e became 01109b5. Merging into main afterwards is a fast-forward, which just moves the main label up. No merge commit:

$ git switch main
Switched to branch 'main'
$ git merge login
Updating c53b34e..01109b5
Fast-forward
 login.py      | 2 ++
 test_login.py | 1 +
 2 files changed, 3 insertions(+)
 create mode 100644 login.py
 create mode 100644 test_login.py
$ git log --oneline --graph --all --decorate
* 01109b5 (HEAD -> main, login) add login tests
* fa2c142 add login form
* c53b34e update readme
* ce12668 add app
* 6b40555 add readme

Which one. Merge when other people work on the branch too, or when you want the history to show when the work came in. Rebase your own branch before you open a pull request, if your team prefers a straight history.

What bites. Rebasing a branch you've already pushed. The rebased commits are new, so a plain push is refused:

$ git switch login
Switched to branch 'login'
Your branch is up to date with 'origin/login'.
$ git rebase main
Rebasing (1/2)
Rebasing (2/2)
Successfully rebased and updated refs/heads/login.
$ git push
To ../remote.git
 ! [rejected]        login -> login (non-fast-forward)
error: failed to push some refs to '../remote.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
$ git push --force-with-lease
To ../remote.git
 + 5d4e29e...01109b5 login -> login (forced update)

Don't follow that hint here. If your git is set to merge when it pulls, git pull brings the old 83514e4 and 5d4e29e back next to their copies fa2c142 and 01109b5, joined by a merge commit. With no pull setting at all, git 2.55 refuses and asks you to choose one. --force-with-lease replaces the remote branch, but only if it's still where you last saw it, so it won't wipe out commits someone else pushed in the meantime. If other people work on the branch, merge instead of rebasing.

How to back out. When both branches changed the same lines, git stops with a conflict. If you don't want to sort it out right now, abort and you're back exactly where you started:

$ git merge login
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
$ git status --short
UU README.md
$ git merge --abort
$ git status --short

A rebase stops the same way, and git rebase --abort undoes it:

$ git switch login
Switched to branch 'login'
$ git rebase main
Rebasing (1/1)
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
error: could not apply 7ceade6... rename readme title
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Could not apply 7ceade6... # rename readme title
$ git rebase --abort
$ git status --short

To undo a merge or rebase that already finished, see the reflog section at the end.

3. stash

When you reach for it. You have changes you aren't ready to commit, and you need a clean working tree for a minute: to switch branches, pull, or check something on the last commit.

The idea. git stash saves your uncommitted changes on a stack and puts your files back to the last commit. git stash pop puts the changes back and takes them off the stack.

The commands. Git won't switch branches when your changes would be overwritten. Stash, switch, come back, pop:

$ git status --short
 M app.py
$ git switch login
error: Your local changes to the following files would be overwritten by checkout:
	app.py
Please commit your changes or stash them before you switch branches.
Aborting
$ git stash
Saved working directory and index state WIP on main: ce12668 add app
$ git status --short
$ git switch login
Switched to branch 'login'
$ git switch main
Switched to branch 'main'
$ git stash pop
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   app.py

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (3ee8463d436088f8e8b3b45c8a3af65fc25a6514)

Before and after. Before the stash, git status --short shows M app.py. After it, the status is empty and the change sits on the stack as stash@{0}. After the pop, M app.py is back and the stack is empty.

What bites. New files. A plain git stash only takes changes to files git already tracks, so a new file stays behind in your working tree:

$ git status --short
 M app.py
?? notes.txt
$ git stash
Saved working directory and index state WIP on main: ce12668 add app
$ git status --short
?? notes.txt

-u takes untracked files too. -m gives the stash a name, which helps once there's more than one:

$ git stash push -u -m "app v2 and notes"
Saved working directory and index state On main: app v2 and notes
$ git status --short
$ git stash list
stash@{0}: On main: app v2 and notes
$ git stash show stash@{0}
 app.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

git stash show only lists the changes to tracked files, so notes.txt doesn't appear there, even though it's in the stash.

How to back out. pop applies and deletes in one go. apply puts the changes back and leaves them on the stack, so you still have them if something goes wrong. Once you're happy, drop deletes them:

$ git stash apply
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   app.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	notes.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ git stash drop
Dropped refs/stash@{0} (063eeef78679d54c7405f6b827d445d533c3a0e0)
$ git stash list

4. reset vs revert

When you reach for it. A commit was a mistake and you want it gone.

The idea. Reset moves the branch back, so the commit isn't on it any more. Revert leaves the commit where it is and adds a new commit that does the opposite. The rule of thumb: if the commit is only on your machine, reset is fine. If you've pushed it, revert, because reset rewrites history other people already have.

Before.

$ git log --oneline
c6ba5f9 add debug logging
ce12668 add app
6b40555 add readme

Reset. HEAD~1 means one commit before the one you're on. Reset has three modes, and they differ in what happens to the changes from the commits you remove:

$ git reset --soft HEAD~1
$ git status --short
A  debug.py
$ git reset HEAD~1
$ git status --short
?? debug.py
$ git reset --hard HEAD~1
HEAD is now at ce12668 add app
$ git status --short
$ git log --oneline
ce12668 add app
6b40555 add readme
mode the commit its changes
--soft gone from the branch still staged, ready to commit again
no flag (--mixed) gone from the branch still in your files, not staged
--hard gone from the branch gone

(Between each of those runs the commit was made again, so each one starts from c6ba5f9.)

Revert. Same starting point:

$ git revert HEAD
[main 1c4c2e0] Revert "add debug logging"
 Date: Mon Nov 30 10:00:00 2026 +1300
 1 file changed, 1 deletion(-)
 delete mode 100644 debug.py
$ git log --oneline
1c4c2e0 Revert "add debug logging"
c6ba5f9 add debug logging
ce12668 add app
6b40555 add readme

Git opens your editor for the message; save and close it to keep the default, or add --no-edit to skip the editor. The new commit's message says which commit it undoes:

$ git show --stat HEAD
commit 1c4c2e030fdd59d2643e43629e3f2563447b6c6e
Author: You <you@example.com>
Date:   Mon Nov 30 10:00:00 2026 +1300

    Revert "add debug logging"
    
    This reverts commit c6ba5f9dfa501aea2bc4ba23d341028de7ecac54.

 debug.py | 1 -
 1 file changed, 1 deletion(-)

After a reset the history reads add readme, add app. After a revert it reads add readme, add app, add debug logging, Revert "add debug logging", and the files are back to how they were after add app.

What bites. reset --hard with changes you haven't committed. It resets your files too, and those changes were never in a commit, so nothing can bring them back:

$ git status --short
 M app.py
$ git reset --hard HEAD~1
HEAD is now at ce12668 add app
$ git status --short
$ cat app.py
print("app")

Run git status before any --hard, and stash anything you want to keep.

How to back out. A reset only moves the branch. The commit is still there, and HEAD@{1} means "where HEAD was one move ago":

$ git reset --hard HEAD@{1}
HEAD is now at c6ba5f9 add debug logging
$ git log --oneline
c6ba5f9 add debug logging
ce12668 add app
6b40555 add readme

5. cherry-pick

When you reach for it. You need one commit from another branch, not the whole branch. Usually a fix that was made on a feature branch and that main needs now.

The idea. Cherry-pick takes the change one commit made and makes it again on the branch you're on, as a new commit with a new hash. The original stays where it was.

Before. Login has three commits. Main wants only the readme fix.

$ git log --oneline login
681f2fd fix crash on empty email
1a4d4a0 fix typo in readme
83514e4 add login form
ce12668 add app
6b40555 add readme

The commands. From main:

$ git cherry-pick 1a4d4a0
[main 66ee30c] fix typo in readme
 Date: Mon Nov 30 10:00:00 2026 +1300
 1 file changed, 2 insertions(+)

After.

$ git log --oneline --graph --all --decorate
* 681f2fd (login) fix crash on empty email
* 1a4d4a0 fix typo in readme
* 83514e4 add login form
| * 66ee30c (HEAD -> main) fix typo in readme
|/  
* ce12668 add app
* 6b40555 add readme

Same message, new hash, and login hasn't changed. Add -x and git writes down where the copy came from, which helps when someone later finds the same change twice:

$ git cherry-pick -x 1a4d4a0
[main bfaeb21] fix typo in readme
 Date: Mon Nov 30 10:00:00 2026 +1300
 1 file changed, 2 insertions(+)
$ git log -1 --format=%B
fix typo in readme

(cherry picked from commit 1a4d4a0e2b6beda405324815e1f3505c9e6cb31c)

What bites. Picking a commit that depends on earlier commits from its branch. fix crash on empty email edits login.py, which main doesn't have, because login.py came from add login form:

$ git cherry-pick 681f2fd
CONFLICT (modify/delete): login.py deleted in HEAD and modified in 681f2fd (fix crash on empty email).  Version 681f2fd (fix crash on empty email) of login.py left in tree.
error: could not apply 681f2fd... fix crash on empty email
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
$ git status --short
DU login.py

If you need a commit and the commits it builds on, you probably want a merge, not a pick.

How to back out. Mid-conflict:

$ git cherry-pick --abort
$ git status --short

A pick that went through is an ordinary commit, so section 4 applies: reset it if you haven't pushed, revert it if you have.

undo almost anything

Every time HEAD moves (a commit, a switch, a reset, a rebase, a merge), git writes a line in the reflog. Commits that seem lost after a reset or a rebase are still in the repo, and the reflog has their hashes.

Here main had three commits, and a reset went two too far:

$ git reset --hard HEAD~2
HEAD is now at 6b40555 add readme
$ git log --oneline
6b40555 add readme
$ git reflog
6b40555 HEAD@{0}: reset: moving to HEAD~2
c53b34e HEAD@{1}: checkout: moving from login to main
fa2c142 HEAD@{2}: commit: add login form
c53b34e HEAD@{3}: checkout: moving from main to login
c53b34e HEAD@{4}: commit: update readme
ce12668 HEAD@{5}: commit: add app
6b40555 HEAD@{6}: commit (initial): add readme
$ git reset --hard HEAD@{1}
HEAD is now at c53b34e update readme
$ git log --oneline
c53b34e update readme
ce12668 add app
6b40555 add readme

Read it from the top: HEAD@{0} is the reset, and HEAD@{1} is where HEAD was just before it. The same move undoes a merge or a rebase you regret: find the line from before it and reset to that.

A deleted branch comes back the same way. git branch -D prints the hash the branch pointed at, and the reflog has it too:

$ git branch -D login
Deleted branch login (was fa2c142).
$ git branch login fa2c142
$ git log --oneline login
fa2c142 add login form
c53b34e update readme
ce12668 add app
6b40555 add readme

The reflog only covers commits. Changes you never committed aren't in it (that's the reset --hard trap in section 4), and it only exists in your copy of the repo, not on the remote.

Two smaller undos come up constantly. git restore --staged takes a file out of the next commit, and git restore throws away your changes to a file:

$ git add .
$ git status --short
M  app.py
A  notes.txt
$ git restore --staged notes.txt
$ git status --short
M  app.py
?? notes.txt
$ git restore --staged app.py
$ git restore app.py
$ git status --short
?? notes.txt

honest limitations

Five things isn't all of git. Remotes, fetch vs pull, tags, interactive rebase (git rebase -i) and git bisect all come up too, and the worktrees guide covers having several branches checked out at once.

Conflicts only go as far as aborting here. Resolving one means editing the marked lines, git add on the file, then --continue on whatever was running, the way git's hints describe.

All the output is from git 2.55.0. Older versions word some messages and hints differently, and git switch and git restore need git 2.23 or newer. On an older git, git checkout -b and git checkout -- <file> do the same jobs.

← all guides

Done nicnonac.com 56.6k