Revert or Undo a commit using git amend

In the previous post we have seen that how we can revert or undo a local or public git commit using git revert. In this post we are going to see some other ways in which we can Revert or Undo a commit using git. Yes, you have read it right only we are talking about only local commit.

Note : There are lot of things in life and git can’t be undone always .

Undo a commit using git amend :
Let’s consider a scenario where you have just started development of your application , and now you have lot of files to checkin into the git repository so for doing that you have to do a local commit first, of all those files. After doing this commit you have realized that , few files have been missed that was also supposed to go in the same commit or you just want to change the commit message only .Constraint is that all these changes should belong to a single commit and not to multiple commits. You can achieve the same by using git commit with amend.

Let’s understand the whole process step by step.

First I have changed two files file1.txt and file2.txt in my system , as you can see the same with git status.

git status

Undo a local commit using git amendNow I have made a commit in which I have intentionally missed out 2nd file.

git commit –m “Intentionally missing file2 in the commit” file1.txt

Undo a local commit using git amendAfter that I executed git log to confirm that my commit is successfully registered.

git log

Undo a local commit using git amendNow I have added all the remaining files that was supposed to go in the first commit.

Git add file2.txt

Undo a local commit using git amendFinally I have executed git commit with amend to merge my previous commit.

git commit -–amend

Undo a local commit using git amendOnce you press enter it will open the editor window for you to edit the commit message and it will also show you the all the files present in the staging area.

Undo a local commit using git amendUndo a local commit using git amendUndo a local commit using git amendAnd finally we have executed the git log command again to confirm that our last commit has been replaced successfully after executing git commit –amend.

Undo a local commit using git amendNote : git commit –amend works only in the case of local commits and there should not exist any other commit between wrong commit and git commit –amend.

[contact-form-7 404 "Not Found"]
Let'sConnect

Saurabh Jain

A Developer working on Enterprise applications ,Distributed Systems, Hadoop and BigData.This blog is about my experience working mostly on Java technologies ,NoSQL ,git , maven and Hadoop ecosystem.
Let'sConnect
Add Comment Register



Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>