Git is a powerful version control system (used stand-alone, with GitHub, and Azure DevOps) that allows developers to keep track of changes in their codebase. However, mistakes can happen, and there may be times when you accidentally commit the wrong changes to your local Git repository. Fortunately, Git provides a way to undo the most recent local commits without affecting your working directory. In this article, we will explore how to undo those commits and get your project back on track.
Problem: Need to Undo Recent Git Commit
Imagine the scenario where you’ve been working diligently on your project, making a series of commits along the way. However, in your haste, you accidentally committed some changes that you shouldn’t have. These changes could be incorrect code, sensitive information, or simply changes that you want to revise before they are permanently recorded in your Git history. The key point is that you haven’t pushed these commits to the remote server yet.
So, the question is: How do you undo those commits from your local repository without affecting your working directory?
Solution: Undo Recent Git Commit
Git provides a straightforward solution to undo the most recent local commits using the
git reset command. Here are the steps to follow:
Step 1: Identify the Mistaken Git Commits
Before you can undo the commits, you need to identify which commits you want to remove. You can use
git log to view the commit history and determine the SHA-1 hash of the commit you want to undo. Make a note of it.
The output of
git log will look similar to the following:
chris@b59pc local-b59-repo % git log commit 73c0ade664dc5fa594b748fdf1420ac0201bf7ef (HEAD -> main, origin/main, origin/HEAD) Author: Chris Pietschmann <firstname.lastname@example.org> Date: Thu Mar 2 17:24:26 2023 -0500 Update main.tf commit feeb5f2f49819a2fa6abfc878ecfb8d2cf2ff5f5 Author: Chris Pietschmann <email@example.com> Date: Thu Mar 2 13:48:29 2023 -0500 update
Copy the SHA-1 hash of the particular commit you want to rollback. For the latest, this would be the first one.
Step 2: Undo the Git Commits
Now, let’s undo those commits using
git reset. Open your terminal and execute the following command, replacing the
<commit-hash> placeholder with the actual SHA-1 hash of the commit you want to undo:
$ git reset <commit-hash>
This command will reset your local branch’s HEAD to the specified commit while keeping your working directory unchanged. The changes from the undone commits will now be in an uncommitted state.
It’s worth nothing that, for the latest commit, that
HEAD~is the same as
HEAD~1, which refers to the commit one step back in the history. Such as in
$ git reset HEAD~
git reset command can also be used to perform a hard reset of the previous commit, as well as throw away any uncommitted changes using the following:
// perform hard reset of previous commit, // and throw away uncommitted changes git reset --hard HEAD~1
Step 3: Make Corrections (If Needed)
At this point, you may want to make any necessary corrections to your working directory. Review your code and files to ensure everything is as it should be.
Step 4: Git Add and Commit Again
Once you’ve made the required changes, add them to your staging area using
git add. This command stages your changes for the next commit:
$ git add .
Finally, commit your changes with the same commit message as the undone commit using the following command:
$ git commit -c ORIG_HEAD
-c ORIG_HEAD flag tells Git to use the original commit message from the undone commit. If you need to modify the commit message, you can omit the
-c flag and provide a new message.
Congratulations! You have successfully undone the most recent local commits in your Git repository while preserving your working directory.
A Note on Pushing Git Commits
If you have already pushed the mistaken commits to a remote server, you should be cautious about rewriting history. Rewriting history can cause issues for collaborators who have based their work on your previous commits. In this case, you may need to use
git push with the
--force-with-lease option, but it’s important to understand the implications of doing so, as it can disrupt the work of others.
git push --force-with-lease
Beware that using
--force-with-leasewill push local commit history to the
remoteand unconditionally overwrite the
remotehistory. This can be destructive as it will erase any reset git repository history on the
Undoing commits in Git can be a lifesaver when mistakes happen, but it’s essential to use these commands with care, especially when working in a collaborative environment. Always communicate with your team if you need to rewrite history to avoid any unintended consequences.