{"id":136920,"date":"2020-12-29T14:54:03","date_gmt":"2020-12-29T14:54:03","guid":{"rendered":"https:\/\/onet.com.vn\/how-to-undo-last-commit-in-git.html"},"modified":"2020-12-29T14:54:03","modified_gmt":"2020-12-29T14:54:03","slug":"how-to-undo-last-commit-in-git","status":"publish","type":"post","link":"https:\/\/onet.com.vn\/how-to-undo-last-commit-in-git\/","title":{"rendered":"How to Undo Last Commit in Git"},"content":{"rendered":"\n
A common workflow in Git is that you make changes to your project, add the changes to the staging area, commit the changes, make new changes, stage the changes, commit the changes, and it goes on and on. But what if you mistakenly committed your changes? Well, don\u2019t fear. You can always undo your last commit in Git. In this article, I am going to show you how to undo the last commit in Git. So, let\u2019s get started.<\/p>\n

Setting Up a Test Git Repository:<\/h2>\n

In this section, I am going to clone one of my GitHub repository on my computer to set up a test repository for this article. If you want to follow along, then you may do so as well. If you have your own Git repository to work with, then great! You can use it as well.<\/p>\n

To clone one of my GitHub repository (https:\/\/github.com\/dev-shovon\/my-project), run the following command:<\/p>\n

\n
$ <\/span>git clone<\/span> https:\/\/<\/span>github.com\/<\/span>dev-shovon\/<\/span>my-project undo_commit_demo<\/div>\n<\/div>\n

<\/p>\n

The GitHub repository should be cloned into undo_commit_demo\/<\/strong> directory.<\/p>\n

<\/p>\n

Now, navigate to the undo_commit_demo\/<\/strong> directory as follows:<\/p>\n

<\/p>\n

The commit you want to undo may be in a different branch as mine. I want to undo the last commit of the ie-fix<\/strong> branch. So, I have to pull the branch ie-fix <\/strong>from my GitHub repository.<\/p>\n

You can pull the ie-fix<\/strong> branch from GitHub as follows:<\/p>\n

\n
$ <\/span>git pull<\/span> origin ie-fix<\/div>\n<\/div>\n

NOTE:<\/u><\/strong> Here, ie-fix<\/strong> is the branch name.<\/p>\n

<\/p>\n

The ie-fix<\/strong> branch should be pulled.<\/p>\n

<\/p>\n

Now, checkout to the branch (in my case, ie-fix<\/strong>) from which you want to undo last commit as follows:<\/p>\n

\n
$ <\/span>git checkout<\/span> ie-fix<\/div>\n<\/div>\n

<\/p>\n

As you can see, the last commit of the ie-fix<\/strong> branch is aec00f3<\/strong>. In the next sections, I am going to show you ways of undoing the last commit on your Git repository.<\/p>\n

<\/p>\n

Undo Last Commit and Keep Changes:<\/h3>\n

One way to undo the last commit is the use soft reset on your Git repository. What this does is, the last commit is removed and the changes you\u2019ve made in that commit is added to the staging area of your Git repository. This way, if you want to fix anything, you can modify the files and add a new commit.<\/p>\n

As you can see, the last commit in my ie-fix<\/strong> branch is aec00f3<\/strong>.<\/p>\n

\n
$ <\/span>git log<\/span> –oneline<\/span><\/div>\n<\/div>\n

<\/p>\n

Also, my staging area is clean.<\/p>\n

\n
$ <\/span>git status<\/span><\/div>\n<\/div>\n

<\/p>\n

Now, to remove or undo the last commit, run the following command:<\/p>\n

\n
$ <\/span>git reset<\/span> –soft<\/span> HEAD~1<\/span><\/div>\n<\/div>\n

<\/p>\n

As you can see, the commit aec00f3<\/strong> is gone.<\/p>\n

\n
$ <\/span>git log<\/span> –oneline<\/span><\/div>\n<\/div>\n

<\/p>\n

Also, the file I modified in the last commit is in my staging area.<\/p>\n

\n
$ <\/span>git status<\/span><\/div>\n<\/div>\n

<\/p>\n

Now, you can further modify the files, fix the mistakes you\u2019ve made and commit the changes again.<\/p>\n

Undo Last Commit and Remove Changes:<\/h3>\n

If the last commit which you want to remove is useless to you, then you may consider a hard reset on your Git repository. What hard reset does is, it removes the last commit as before. But, it also removes all the changes you\u2019ve made in the last commit. Do a hard reset only when you\u2019re sure that you no longer need anything of the last commit.<\/p>\n

I am going to pull the ie-fix <\/strong>branch from my GitHub repository again to recover the last commit aec00f3<\/strong> and remove it again in this section.<\/p>\n

\n
$ <\/span>git pull<\/span> origin ie-fix<\/div>\n<\/div>\n

<\/p>\n

As you can see, the last commit is back. If your Git repository is uploaded to a Git cloud service such as GitHub, then you can restore any commit you remove mistakenly as well.<\/p>\n

<\/p>\n

As you can see, my staging area is clean.<\/p>\n

\n
$ <\/span>git status<\/span><\/div>\n<\/div>\n

<\/p>\n

Now, to remove the last commit and remove the changes in that commit, run the following command:<\/p>\n

\n
$ <\/span>git reset<\/span> –hard<\/span> HEAD~1<\/span><\/div>\n<\/div>\n

<\/p>\n

The last commit should be removed and the HEAD<\/strong> pointer should be updated.<\/p>\n

<\/p>\n

As you can see, the commit aec00f3<\/strong> is removed and the commit immediately before that (3fffdee<\/strong>) is the current the last commit.<\/p>\n

\n
$ <\/span>git log<\/span> –oneline<\/span><\/div>\n<\/div>\n

<\/p>\n

The staging area is also clean. So, changes files from the removed commit exist.<\/p>\n

\n
$ <\/span>git status<\/span><\/div>\n<\/div>\n

<\/p>\n

Updating Remote Git Repository:<\/h3>\n

Now that you\u2019ve successfully removed the faulty commit from your Git repository, you may want to update your GitHub repository as well. This is the topic of this section.<\/p>\n

As you can see, git status<\/strong> also shows that I am 1 commit behind from the remote repository.<\/p>\n

<\/p>\n

Before I updated my GitHub repository, the commit aec00f3<\/strong> exists even though I removed it from the local Git repository as you can see.<\/p>\n

<\/p>\n

To sync the local Git repository with the GitHub repository, run the following command:<\/p>\n

\n
$ <\/span>git push<\/span> –force<\/span> origin ie-fix<\/div>\n<\/div>\n

The changes of the local Git repository should be synced with the GitHub repository.<\/p>\n

<\/p>\n

The GitHub repository should be updated. As you can see, the commit aec00f3<\/strong> is no longer listed. The commit immediately before aec00f3<\/strong>, which is 3fffdee<\/strong> is now the last commit.<\/p>\n

<\/p>\n

So, that\u2019s how you undo last commit in Git. Thanks for reading this article. <\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"

A common workflow in Git is that you make changes to your project, add the changes to the staging area, commit the changes, make new changes, stage the changes, commit the changes, and it goes on and on. But what if you mistakenly committed your changes? Well, don\u2019t fear. You can always undo your last […]<\/p>\n","protected":false},"author":1,"featured_media":136921,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135],"tags":[],"_links":{"self":[{"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/posts\/136920"}],"collection":[{"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/comments?post=136920"}],"version-history":[{"count":0,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/posts\/136920\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/media\/136921"}],"wp:attachment":[{"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/media?parent=136920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/categories?post=136920"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/onet.com.vn\/wp-json\/wp\/v2\/tags?post=136920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}