Skip to content
Related Articles

Related Articles

Undoing in Git

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 01 Apr, 2022

Undoing in git means doing undo just like when we type something in any text editor and deleted the sam. After that we think the text that we just deleted is needed then we use undo operation to get back the old text same undoing in git is like doing undo in git. So will start this article with how can we return back to the previous commit.

Return to a Previous Commit

To jump back to an old commit, we can use the below command as it basically detaches our head with the old commit id.

git checkout commit-id

Using the command git checkout commit-id

So now if we type git branch we can see our head is detached at the commit id which we just used in the checkout command.

Using the command git branch

So we can see that the below command places us at the commit id used in the checkout command and we can make new commits on top of this old commit without affecting our branch where our head is currently present.

git checkout commit-id

We can make any changes to a proper branch using either branch or checkout -b just like we have done a new commit in this dev branch which we can see using the git log command.

Using git log command

So now if you want to roll back to the previous commit while keeping the changes we can use the below command as follows:

git reset --soft commit-id

Using git reset –soft Commit-id

So to roll back to the last commit we can use the below command as follows:

git reset --soft HEAD~

Using the command git –reset –soft HEAD~

If we want to permanently discard any changes made after a specific commit, for that we can use the below command as follows:

git reset --hard commit-id

Using the command git reset –hard commit-id

Here we can see that on using the above command the commit which was made after the particular commit is discarded for example, in this case, we have used the command git reset –hard commit-id-of-2nd-commit is used here and you can see that the commit after the 2nd commit is discarded to confirm that we have used git log command and we can see that it is only showing two commits. All the commit which was after 2nd commit is discarded.

So now if you want to discard any changes made after the last commit for that we can use the below command as follows:

git reset --hard HEAD~

Using the Command git reset –hard HEAD~

Here, we can see that all the commits made after this last commit are discarded.

Undoing Changes

If we want to undo changes in a file or directory we can use the below command as follows:

git checkout -- name_of_the_file

Using the command git checkout — name_of_the_file

Here we can see the file has some content of 3 lines before using this command and when we used this command and then when we just printed the contents of the file it printed the old content back which was written at some time in the file.

If we want to undo all changes in the files present in the current working directory for this we can use the below command as follows:

git checkout -- .

Before using the command 

On using the command git checkout — 

So what this command has done is it has undo all the new content present in the file and on printing the content of the files it returned back the old content and . is representing that in the current directory we have to undo all the changes.

So now if we want to undo the last two commits for that we can use the below command as follows:

git reset HEAD~2

Modifying the files  

Committing the file b.txt

Committing the file c.txt

Using the command git reset HEAD~2

Using reflog

So basically reflog has the history of everything we have done for the last 90 days, we have to use the below command as follows:

git reflog

Using command git reflog

Also, we can check out to the different commit ids by seeing the output from git reflog using the command: 

git checkout HEAD@{30} 

This is a rebase and you can see the commit before it in the git reflog command output so basically this command will place the head at that commit id will understand rebasing in my future articles till then you can consider it as we are switching to the particular commit id.

Also, we can create new branches and delete branches in this commit id only.

Deleting a branch

Creating a branch

We can also reset back directly to a point in our reflog by using the below command as follows:

git reset --hard Rebase_name

Using the command git reset –hard rebase_name

Undoing Merge

Undoing a merge not yet pushed to a remote. So if our use case is to undo the merge commits from another branch for that we can use the following command:

git reset --hard merge-commit-id

Adding some content in the feature 

Merging the changes done in a.txt from feature branch to master branch

Adding some more lines of contents in a.txt in the  feature branch 

Merging the contents present in the feature branch with the master branch

Using the command git reset –hard merge commit-id and here we can see that on printing the contents of the file it is showing the old content 

If we know that our most recent commit was a merge commit then we can undo it using the following command:

git reset HEAD~

So this command will undo any merge commits along with any other commits from the other branch.

Seeing the merge commit by using git log

Using the command git reset HEAD~

Here we can see that the merge commit and any other commits from the master branch are removed from here

Undoing a Merge Pushed to a Remote 

So let’s first push a merge to the remote repository for that you can see a remote repository has been added here in folder5 and then the changes are pushed to the remote repository. Here we can see that the file by the name of bug-fix.txt is merged from the feature branch to the master branch after that we added a remote repo and then pushed these changes to the remote repo on Github.

Seeing the merge Commit

Pushing the changes to the remote repo

Changes pushed to the remote repo

Now if you want to undo the merge commit that is being pushed to the remote repo already for that will use the command as follows:

git revert -m 1 commit-id

Seeing the merge commit

Using the command git revert -m 1 commit-id

Here we can see that the merge is undone and any other files from the feature branch are reverted back and now we are able to see only 2 commits that are from the master branch only

So now we have successfully removed our merge along with other files from the feature branch. Now, if we want to add the merge back to the remote repository that will follow the below procedure. 

Undoing the revert so that we can push this merge back to the remote repository

Pushing the changes to remote repository

Merge is successfully pushed back to the remote repository 

Now if you want again to revert back this merge and any other changes from the feature branch there is one more workflow instead of using this command git revert -m 1 commit-id as it can help us to avoid any merge conflicts between the branches also.

Merging the changes from the  master branch to feature branch

Seeing the merge commit

Reverting the merge commit

On using this command it will open an editor and in that, you have to type up a short commit message so that we can avoid any merge conflicts between branches.

Typing the short message in order to avoid any commits

Merging the changes from the master branch to the feature branch

Pushing Changes to the remote repository

Undo the changes from the  feature branch and here we can see any changes made by feature branch are reverted back now

Revert Some Existing Commits

So now if you want to revert some existing commits from your remote repository especially when those commits are pushed to the remote repository. 

For example,  you just pushed some wrong code to the remote repository which had some bugs and you need to revert it that is to back it out for that use case we can use the command git revert HEAD~1. So now let’s see how to resolve this issue.

Creating a new file and committing it

Pushed changes to the remote repository and then did some modifications in the file and Committed it

After, this you need to create a new commit because it records these new commits to revert the previous commits

Using git revert HEAD~1

On using the command  git revert HEAD~1 it will open an editor for you and then we have a short commit message there

After this, push these changes to the remote repository type in the command git push remote_name

Here we can see the previous commit has been reverted


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!