cmds.fun
Git Cheat Sh*t

The no-panic Git cheat sheet. These are the recovery commands and fixes you reach for when you messed up a commit, the wrong branch, or a botched merge. Copy the snippet that matches your situation.

sections
oh no, i need a time machine

Git has a built-in recovery log. Reflog shows every place HEAD has been, even across branches. Use it to jump back to a point before things broke.

git reflog
# find the entry before everything went wrong
git reset HEAD@{index}

Use reflog to recover deleted commits, undo bad merges, or roll back experiments. It is the closest thing Git has to a magic undo.

small change after commit

You committed, then realized you need one tiny fix. Amend the last commit. Do not amend commits that are already pushed to shared branches.

# make your change
git add .
git commit --amend --no-edit

Warning: only amend local commits. Amending published history will break other clones.

change last commit message

Fix a messy message without changing the content.

git commit --amend
# edit the message in your editor
committed to the wrong branch

Move the commit to a new branch, then reset the original branch.

# create a new branch from the current state
git branch fix-this-branch
# remove the last commit from the current branch
git reset HEAD~ --hard
git checkout fix-this-branch

If you already pushed, use a safer strategy like revert instead of rewriting history.

commit should be on another branch

Undo the commit locally, stash changes, then apply them on the correct branch.

git reset HEAD~ --soft
git stash
git checkout correct-branch
git stash pop
git add .
git commit -m "your message here"

Alternative: use cherry-pick to move the commit, then reset the old branch.

git checkout correct-branch
git cherry-pick main
git checkout main
git reset HEAD~ --hard
diff shows nothing

If you already staged files, regular diff will be empty. Use the staged diff.

git diff --staged
undo a commit from a while ago

Revert creates a new commit that reverses a past commit without rewriting history.

git log
git revert <commit-hash>
undo changes to a single file

Restore one file from a previous commit without touching the rest of the repo.

git log -- path/to/file
git checkout <commit-hash> -- path/to/file
git commit -m "restore file"
nuke and reset to remote

Destructive reset to match the remote state. This will delete local changes and untracked files. Use with extreme caution.

git fetch origin
git checkout main
git reset --hard origin/main
git clean -d --force
why this cheat sheet exists

Git is powerful, but its recovery commands are easy to forget in the moment. This page collects the most common "oh no" fixes in one place. It is meant for fast, practical recovery, not for learning every Git concept.

If you are searching for a Git cheat sheet, Git recovery commands, or "undo a commit" guidance, this command gives you a quick reference without leaving the terminal flow.