How to use
- Choose the operation from the list.
- Fill in the parameters (branch, commit, message).
- Read the explanation and risk warnings.
- Copy the commands and run them in your terminal.
Use cases
- Undoing a commit made on the wrong branch.
- Cleaning up history before opening a pull request.
- Renaming local and remote branches without missing a step.
- Removing a file from the repository that should have been in .gitignore.
Technical concepts
reset --soft, --mixed and --hard
All three move HEAD. --soft keeps the changes staged, --mixed (the default) keeps them in the working tree unstaged and --hard discards everything. Discarded commits can still be recovered with git reflog for a while.
Rewriting history
amend, reset and rebase create new commits with new hashes. On branches that were already pushed, this requires a force push and breaks the history for anyone who pulled it. Prefer git revert on shared branches and --force-with-lease over --force.
switch and restore
Since Git 2.23, git switch changes branches and git restore restores files, splitting the responsibilities of the old git checkout and reducing mistakes.
Frequently asked questions
How do I recover a commit lost after reset --hard?
Run git reflog, find the hash from before the reset and use git reset --hard <hash> or create a branch from it.
What is the difference between --force and --force-with-lease?
--force overwrites the remote unconditionally. --force-with-lease refuses the push if someone pushed commits you do not have locally yet.