About Git Command Reference
Find the git command for what you are trying to do: start a repository, stage part of a file, rename a branch, publish a tag, or put work aside for a moment. Type what you want in plain words (undo commit, delete branch, unstage) or pick a task, and copy the command with one click.
Commands that rewrite history or throw work away are labelled, with a word on when to avoid them, and the undo section covers the classic rescues: amending, resetting, reverting a pushed commit, recovering a lost one from the reflog and moving a commit made on the wrong branch.
How it works
- Every word you type must appear in the command, its description or its task. Common synonyms count too, so delete finds commands that remove, and a leading git is ignored.
- Placeholders are written in angle brackets, such as <branch> or <commit>. Replace them, brackets included, before running the command.
- Rewrites history marks commands that replace commits. They are fine on your own branch, but force others who pulled the old commits to sort out the difference. Cannot be undone marks commands that delete uncommitted work.
Questions
How do I undo a commit I have already pushed?
Use git revert with the commit’s hash. It adds a new commit that reverses the old one, so nobody else’s history changes. Resetting and force-pushing also works, but only on a branch nobody else uses.
Is a reset --hard really permanent?
Committed work usually is not lost: git reflog lists recent commits for a few weeks, and you can bring one back by giving it a branch. Uncommitted changes that a hard reset or git restore throws away are gone for good.
Why git switch and git restore instead of git checkout?
git checkout does both jobs, which makes mistakes easy. git switch (branches) and git restore (files) split them and have been available since git 2.23. git checkout still works if you prefer it.
Which version of git do these commands need?
All of them work with git 2.23 or later, which covers every supported operating system release. Run git --version to check yours.
Everything you enter here is processed in this browser tab. Nothing is uploaded, logged or shared. How privacy works here.