DEV Community

Cover image for Learn git revert Step by Step (to revert commit in GitHub)
Krushna Chandra Dash
Krushna Chandra Dash

Posted on

Learn git revert Step by Step (to revert commit in GitHub)

Let say, you have Committed some change and pushed the changes to your git repo.
But now you want to revert/remove the changes from your git repo as well as from your local machine, then git revert can help you.

Note-> This command won't remove the commit from commit history, it only revert the change from files.

As per Google
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

to read from git documentation https://git-scm.com/docs/git-revert.

Step-1

We need commit id to revert the specific commit. So to get commit id enter below command in your code editor terminal or command prompt.

git log
Enter fullscreen mode Exit fullscreen mode

this will show list of commit with commit id, author, Date and commit Name

IMAGE 1->
git log

so the top one will show the latest commit. so copy the id from the log and close the process by pressing semicolon + q button.

Step-2

Now lets use git revert command.

git revert <commit-id>
Enter fullscreen mode Exit fullscreen mode

IMAGE 2 ->
git revert

and press Enter.
after enter we will see screen like this :

IMAGE 3 ->
git revert

you can see your commit name and commit id with files that have changes.

Step-3

So now we have to exit vim and commit the changes.
To do that press.

  1. Ctrl+C

after pressing Ctrl+C it will ask to Type: qa and press enter.

IMAGE 4 ->
git revert

2.Then type qa and press Enter.

And you will exit of vim ui. and screen will look like this.
And you will see that you have created one commit and that is the reverted commit.

IMAGE 5 ->
git revert

Step-4

Now we have to push the reverted commit by using git push.

git push
Enter fullscreen mode Exit fullscreen mode

and Done.

if you see the commit list you will see you have added one new commit starting with Revert key.

IMAGE 6 ->
commit List

That's all guys.. And All the best.

Top comments (0)