The git revert command is an “undo” operation however it is not the appropriate one. The git revert command reverts the changes introduced by the commit and appends a new commit with resulting reversed content. This does not allow Git to lose history which is essential for revision history integrity and proper collaboration. Reverting is used for applying the inverse commit from the project history. You can use git revert for automatically going back and making fixes.
|Opens up the configured system editor and prompts you to edit the commit message before committing the revert. Default option.|
|--no-edit||Causes the revert not to open the editor(the opposite of the -e option).|
|Adds the inverse modifications to the Staging Index and Working Directory instead of creating a new commit.|
How it works¶
Like git checkout and git reset, git revert also takes a specified commit but it does not move reference pointers to this commit. The revert operation takes the specified commit, inverses the modifications from that commit and creates a new revert commit.
Here is an example of creating a repository:¶
mkdir git_revert_example cd git_revert_example/ git init . #Initialized empty Git repository in /git_revert_example/.git/ touch w3docs_file git add w3docs_file git commit -am "original commit" #[master (root-commit) 299b15f] original commit #1 file changed, 0 insertions(+), 0 deletions(-) #create mode 100644 w3docs_file echo "original content" >> w3docs_file git commit -am "add new content to w3docs_file" #[master 3602d88] add new content to w3docs_file #1 file changed, 1 insertion(+) echo "prepended line content" >> w3docs_file git commit -am"prepend content to w3docs file" #[master 86bb32e] prepend content to w3docs file #1 file changed, 1 insertion(+) git log --oneline #86bb32e prepend content to w3docs file #3602d88 add new content to w3docs file #299b15f original commit
In the above-mentioned example, a repository is initialized in a newly created directory named git_revert_example. There are 3 commits to the repository in which a file named w3docs_file is added. Its content has been changed twice. We use git log at the end of the repository set up to show all 3 commits in the commit history. Now we can invoke git revert:
git revert HEAD #[master b9cd081] Revert "prepend content to w3docs file" #1 file changed, 1 deletion(-)
Git revert will not work without passing commit reference. In the given example, it has been passed in the HEAD reference to revert the last commit. A revert creates a new commit which opens up the configured system editor creating a new commit message. We can use git log and see the new commit added to the previous log:
git log --oneline #2365e21 Revert "prepend content to w3docs_file" #23ab21e prepend content to w3docs_file #3602d88 add new content to w3docs_file #345b23f original commit
After the revert the 3rd commit is still in the project history. The git revert added a new commit to undo its changes instead of deleting.
Resetting vs. reverting¶
The git revert undoes only one commit while git reset reverts back to the previous project state by deleting succeeding commits.
Reverting is considered as safe operation for the commits that have been published to the shared repository. Another advantage of reverting is targeting a specific commit at a random point in the history. Find detailed information about git reset on our next page.