If you have missed out my previous posts on git repositories then you can read it here.
While working on GIT distributed version control system , you might have found yourself in a scenario where you have to revert or undo last git commit. Reason for revert can be anything like wrong file checked in , some junk code has been checked in that can impact your well developed application etc.
Now here two scenario exists depends on the level of your commit–
1. If the commit has been already pushed to central remote repository that means now it is a public commit that means other developers can see these changes.
2. Commit is only made in your local cloned repository and push is pending that means it is a local commit that means nobody can see these changes except you.
Here , we will see the all the possible ways in which we can revert or undo a local commit and a public commit.
For better demonstration we will see it with the help of an example :-
I have cloned a remote repository into my local system and I made a local commit that I was not supposed to make. So, when I did git status it showed me the file that I edited by mistake and committed it in image 1 and image 2 respectively. And after executing git log you can see the that our previous commit is successfully registered(image 3).
Now after doing commit I realized that there is a problem with my commit and I have to revert it immediately. For undoing or revert the commit ,git provides multiple solutions.
Solution 1 : Change all the files with the opposite changes that you made in previous commit and make one more new commit . This is most straightforward and conventional way of doing this , if you are a git beginner and not very familiar with the git commands then this is for you.
Solution 2 : You can execute the git revert command by passing the commit id as an arguments and git will undo or revert your commit identified by the id provided.
git revert 5cf76aa793c886084cccfce763a4ed0811b482af
So, now when you execute the above mentioned command it will revert your last commit changes and will make one more commit that will overwrite the changes made by you in the last commit and will bring the head to the previous position. You can also see , what happened internally by executing git log command.
git push origin
Note : Above mentioned revert command works in the case of both local and public commit.
There are other solution also available to achieve the same thing , but i found git revert as the most convenient and easy to use.
Latest posts by Saurabh Jain (see all)
- java.lang.IncompatibleClassChangeError: Found interface org.apache.hadoop.mapreduce.TaskInputOutputContext, but class was expected - August 8, 2014
- org.datanucleus.store.rdbms.exceptions.MappedDatastoreException: INSERT INTO “TABLE_PARAMS” – Hive with Kite Morphlines - July 17, 2014
- java.io.IOException: can not read class parquet.format.PageHeader: null – Hive - July 12, 2014