← Back to the log

Git Commands I Always Forget

A running list of git commands I find useful but can never quite remember.

Undo the last commit (keep changes)

git reset --soft HEAD~1

Interactive rebase for the last N commits

git rebase -i HEAD~3

See what changed between two branches

git diff main..feature-branch --stat

Delete all merged local branches

git branch --merged main | grep -v "main" | xargs git branch -d

Stash with a message

git stash push -m "WIP: working on auth flow"

Find which commit introduced a bug

git bisect start
git bisect bad          # current commit is broken
git bisect good abc123  # this commit was fine
# git will checkout commits for you to test
git bisect reset        # when done

Amend the last commit message

git commit --amend -m "better message"

Cherry-pick a commit from another branch

git cherry-pick <commit-hash>