git undo
all problems
before you panic
all problems
every git undo scenario we cover, organized by category.
PUSHING
pushed bad commits
caution
Revert commits you shouldn't have pushed to the remote.
$ git revert HEAD
pushed secrets
danger
Credentials in your history. Rotate keys and scrub with BFG.
$ bfg --delete-files .env
force-pushed over someone
danger
Overwrote a teammate's work. Recover from their reflog.
$ git reflog
COMMITS & STAGING
undo a local commit
safe
Rewind history to before your commit without losing changes.
$ git reset HEAD^
unstage a file
safe
Remove a file from staging without discarding changes.
$ git reset <file>
fix commit message
safe
Update the message on your most recent unpushed commit.
$ git commit --amend
BRANCHES & MERGING
deleted a branch
caution
Recover a branch you accidentally deleted locally or remotely.
$ git reflog
merged wrong branch
caution
Undo a merge that brought in the wrong changes.
$ git reset --hard HEAD~1
detached HEAD
safe
You're in detached HEAD and don't know how to get back.
$ git checkout <branch>
FILES & CHANGES
discard unstaged changes
danger
Throw away local modifications you don't want anymore.
$ git checkout -- <file>
add file to existing commit
safe
Forgot a file? Amend it into your last commit.
$ git commit --amend
restore a deleted file
caution
Bring back a tracked file you deleted locally.
$ git checkout -- <file>