The purpose of the git revert command is to remove all the changes a single commit made to our source code repository. For example, if a past commit added a file named index.html to the repo, a git revert on that commit will remove the index.html file from the repo.

When we revert a Git commit, the changes from the targeted commit are removed from our local workspace. A new commit is also created to reflect the new state of our repository.

Let’s walk through an example of how to revert a Git commit, and differentiate the git reset and git revert commands.

git revert

The syntax to revert a Git commit and undo unwanted changes is simple: We just need to issue the git revert command and provide the ID of the commit to undo:

git revert 4945db2

example: Let’s assume, we’ll add five files to the repo. Each time a new file is created, we add it to the Git index and create a new commit with a meaningful message.

$ touch alpha.html
$ git add . && git commit -m "1st git commit: 1 file"

$ touch beta.html
$ git add . && git commit -m "2nd git commit: 2 files"

$ touch charlie.html
$ git add . && git commit -m "3rd git commit: 3 files"

$ touch delta.html
$ git add . && git commit -m "4th git commit: 4 files"

$ touch edison.html
$ git add . && git commit -m "5th git commit: 5 files"

A quick directory listing with the “ls command” shows five files in the current folder:

$ ls
alpha.html  beta.html  charlie.html delta.html  edison.html

A call to the “git reflog command” will show us our current commit history:

git reflog
(HEAD -> master)
d846aa8 HEAD@{0}: commit: 5th git commit: 5 files
0c59891 HEAD@{1}: commit: 4th git commit: 4 files
4945db2 HEAD@{2}: commit: 3rd git commit: 3 files
defc4eb HEAD@{3}: commit: 2nd git commit: 2 files
2938ee3 HEAD@{4}: commit: 1st git commit: 1 file

what would happen If we did a git revert on the third commit with ID 4945db2 ? (This was the git commit that added the charlie.html file)

git revert 4945db2

 this git revert example will leave four files in the local workspace and remove only the charlie.html file

PS: The git revert command will undo only the changes associated with a specific commit. In this git revert example, the third commit added the charlie.html file. So,  the only file removed from our repository is charlie.html.

ls
alpha.html  beta.html  delta.html  edison.html

PS: We should also notice that when we git revert a commit, the reverted commit is deleted from our local workspace, but not deleted from the local repository. The code associated with the reverted Git commit remains stored in the repository’s history of changes, which means reverted code is still referenceable if it ever needs to be accessed or reviewed in the future.

git reset

When we git revert a commit, only the changes associated with that commit are undone. Cumulative changes from subsequent commits aren’t affected. If we wish to undo every change since a given commit occurred, we’d want to issue a  hard git reset, not revert .

The git reset and revert commands are commonly confused, but they apply to distinct use cases. To undo changes associated with a specific commit, developers should use the git revert command. To undo every change that has happened since a given commit occurred, we should use git reset

Referebce:

https://www.theserverside.com/

By Shabazz

Software Engineer, MCSD, Web developer & Angular specialist

Leave a Reply

Your email address will not be published. Required fields are marked *