git revert
git
Usage
git revert [-n] <commit>Undoes a commit by adding a new opposite commit; it leaves history intact, unlike reset which moves the branch pointer, so anything already pushed should be reverted.
Common flags
| Flag | Meaning |
|---|---|
| -n | stage the inverse change without committing |
| -m <parent> | which side to keep when reverting a merge commit |
| --no-edit | accept the generated message |
| --continue | finish after resolving conflicts |
| --abort | cancel and go back |
Examples
git revert a1b2c3dadds a new commit that undoes that one
git revert --no-edit HEADundoes the last commit without opening an editor
git revert -m 1 <merge-commit>undoes a merge, keeping the first parent
The confusion is that undo has several shapes — moving where a branch points is not the same as adding a commit that reverses it.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does git revert do?
Undoes a commit by adding a new opposite commit; it leaves history intact, unlike reset which moves the branch pointer, so anything already pushed should be reverted.
Q. How do I type it?
git revert [-n] <commit> — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
5 are listed here; the full set is in man git. This command sits under git.
Related commands
man page: man git-revert