Git ~ Revert - rohit120582sharma/Documentation GitHub Wiki

The git revert command is a forward-moving undo operation that offers a safe method of undoing changes. Instead of deleting or orphaning commits in the commit history, a revert will create a new commit that inverses the changes specified.

The git revert takes a specified commit, however, it does not move ref pointers to this commit. A revert operation will take the specified commit, inverse the changes from that commit, and create a new "revert commit". The ref pointers are then updated to point at the new revert commit making it the tip of the branch.

It's important to understand that git revert undoes a single commit. It does not "revert" back to the previous state of a project by removing all subsequent commits. This is a safer alternative to git reset in regards to losing work.

Reverting has two important advantages over resetting:

  • It doesn’t change the project history, which makes it a “safe” operation for commits that have already been published to a shared repository.
  • A git revert is able to target an individual commit at an arbitrary point in the history, whereas git reset can only work backward from the current commit.
git log --oneline
86bb32e prepend content to demo file
3602d88 add new content to demo file
299b15f initial commit

git revert HEAD
[master b9cd081] Revert "prepend content to demo file"
1 file changed, 1 deletion(-)

Git revert expects a commit ref was passed in and will not execute without one. Here we have passed in the HEAD ref. This will revert the latest commit. This is the same behaviour as if we reverted to commit 3602d8815dbfa78cd37cd4d189552764b5e96c58.

Similar to a merge, a revert will create a new commit.

git log --oneline
1061e79 Revert "prepend content to demo file"
86bb32e prepend content to demo file
3602d88 add new content to demo file
299b15f initial commit