Undoing in Git
Undoing in Git means doing undo just like when we type something in any text editor and delete the same. After that, we think the text that we just deleted is needed, and then we use the undo operation to get back the old text. The same undoing in git is like doing undo in git.
Common Scenarios for Undoing the Changes
- Unstaging Changes: Removing changes from the staging area.
- Discarding Local Changes: Reverting changes in the working directory.
- Amending the Last Commit: Modifying the most recent commit.
- Reverting Commits: Creating a new commit that undoes changes from a previous commit.
- Resetting Commits: Moving the current branch to a different commit and optionally modifying the working directory and staging area.
1. 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
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.
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.
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
So to roll back to the last commit we can use the below command as follows:
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
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~
Here, we can see that all the commits made after this last commit are discarded.
2. 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
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 -- .
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
3. 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
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.
We can also reset back directly to a point in our reflog by using the below command as follows:
git reset --hard Rebase_name
4. 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
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.
5. 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.
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
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.
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.
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.
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.
After, this you need to create a new commit because it records these new commits to revert the previous commits
After this, push these changes to the remote repository type in the command git push remote_name
Contact Us