Advertisement
Guide
About the Git Command Builder
Git can undo almost anything, but the right command depends on details: whether the commit was pushed, whether you want to keep the changes, whether others share the branch. Getting it wrong can mean lost work or a messy history for your team.
Find the task, fill in names where asked, and copy the commands. Each one comes with a short explanation and a warning when it rewrites history or deletes work.
Reset, revert or restore?
git reset moves a branch back, rewriting history — fine for local commits. git revert adds a new commit that undoes an old one, so it is safe for pushed commits. git restore works on files in your working tree and staging area, not on commits.
Almost nothing is truly lost
Even after a hard reset or a deleted branch, Git keeps the old commits for weeks and records where HEAD has been in the reflog. Uncommitted changes are the exception: once discarded, they are gone. When in doubt, commit or stash first.
How to use it
- 1Search or browse for what you want to do.
- 2Read the explanation and any warning.
- 3Fill in branch names, hashes or messages.
- 4Copy the commands into your terminal.
Frequently asked questions
How do I undo the last commit but keep my changes?
git reset --soft HEAD~1 keeps the changes staged; git reset HEAD~1 keeps them unstaged. Neither loses your work.
How do I undo a commit I already pushed?
Use git revert <hash> and push. It adds a new commit that cancels the old one without rewriting shared history.
What is the difference between --force and --force-with-lease?
--force overwrites the remote branch no matter what. --force-with-lease refuses if someone else pushed in the meantime, so you cannot wipe out their work by accident.
Should I use git checkout or git switch?
git switch (branches) and git restore (files) split the many jobs of git checkout into clearer commands. Both have been stable since Git 2.23.
How do I find a commit hash?
Run git log --oneline. The short code at the start of each line is the hash; the first 7 characters are enough.
