My scrapbook about anything which I learned or want to remember, Sometime about tech tips, thoughts and rambling. If you find anything useful don't forget to give thumbs-up :)

Breaking

Monday, February 10, 2020

Undo changes with Git revert - Reverse the committed change

In previous posts, Modify last (recent) commit in Git and Rewrite history with Git Reset, we have learn how to reset/revert the changes when commits are local (not being pushed to remote repository). In this post, we will see how we can revert the changes which already been pushed to remote repository and why this method is advocated by git. 

The git revert command simply create an another commit with the reverse change of commit which you want to revert, the reverse commit will neutralize the previous commit. 

Suppose we have the below git log - 

$ git log --oneline
1828a2c (HEAD -> atul, origin/master, origin/HEAD, master) fixed the line 4
aa9006c updated 3rd line
b234a71 com3
6f30710 bad
b52159d com2
3433afb com1
050082d Initial commit

If we want to revert the change 6f30710 bad, we need to simply run the git revert like below - 

$ git revert 6f30710

This will open editor to change the commit message if you want to, by default it shows Revert "bad" which is good enough for me so save the file in editor, as soon as you save the comment file, it will create the another commit with reverse changes. 

Your git log will be look like this - 
$ git log --oneline
9055ed1 (HEAD -> atul, origin/master, origin/HEAD, master) Revert "bad"
1828a2c fixed the line 4
aa9006c updated 3rd line
b234a71 com3
6f30710 bad
b52159d com2
3433afb com1
050082d Initial commit


Git revert is the simplest and safest way to revert the public changes (which has been pushed to remote repository). As we can see, git revert doesn't alter the change history, so if you want to revert the change 9055ed1, you can do it by running another revert command. All the revert operations will be maintained by git in log which tell you what you have did with each commit and it is visible to all the git repo contributor. Hence, git revert is the best tool for reverting the public changes. 

Like the below page to get the update  
Facebook Page      Facebook Group      Twitter Feed      Telegram Group


No comments:

Post a Comment

Disclaimer

The postings on this site are my own and don't necessarily represent IBM's or other companies positions, strategies or opinions. All content provided on this blog is for informational purposes and knowledge sharing only.
The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of his information.