Before we begin talking about how to undo the last Git command, let’s briefly understand - What is Git?
Git is a popular open-source distributed version control system that can handle a number of things right from small to very large projects with great speed and efficiency. The fact that it is user-friendly and easy to learn makes it stand out.
If you're a web developer or software engineer, you're probably pushing a lot of commits to your Git repository on a daily basis.
However, you may have committed certain files to your Git repository that should not have been. It's possible that you'll want to make more modifications before committing. As a result, you'll need to undo the most recent commit in your Git repository.
In this tutorial, you will undo the last git
command. We will also address a few FAQs on how to undo the last git
command. Check out this article if you only want to update the commit message.
Git Three-Tree Architecture
Using the git reset command followed by the commit identifier, you can undo changes in Git.
Additional arguments to git reset
allow you to alter the command's behavior. Let's look at Git's three separate trees to better understand how reset works. The Git management system's fundamental concept is three-tree architecture because they represent groups of files, they are referred to as trees.
Git is in charge of the following three trees:
- The Working Directory - A directory on the local filesystem that is associated with the repository, which includes all subdirectories and files. It's commonly known as a "working tree." The working directory functions as a sandbox for testing changes before committing them to the staging index.
- The Index - This tree keeps track of new or altered files that have been added to the index with git add and are scheduled to be committed in the next commit. It's also known as "staging area" or "staging index."
- The HEAD - This is a pointer to your most recent commit on the current branch.
The three trees are represented by three arguments in the git reset command:
--soft
- Sets theHEAD
pointer to the commit specified. The index and working directory remain unchanged.--mixed
- Resets the Index and updates theHEAD
pointer to the specified commit. The Working directory isn't changed. Thereset
command's default operating mode is this.--hard
- Resets the Index and Working directory to the specified commit and updates theHEAD
pointer. When you use this option, be extremely cautious because any local modifications you haven't committed will be overwritten and lost.
Undo Last Git Commit with reset
The simplest approach to undo the most recent Git commit is to use the "git reset" command with the --soft
option, which will keep your files' changes. You must provide the commit to undo, which in this case is "HEAD~1."
Your Git history will be cleared of the most recent commit.
git reset --soft HEAD~1
If you're not familiar with this notation, "HEAD~1" signifies that you wish to go back one commit in the log history and reset the HEAD (the most recent commit).
git log --oneline
3fad532 Last commit (HEAD)
3bnaj03 Commit before HEAD (HEAD~1)
vcn3ed5 Two commits before HEAD (HEAD~2)
The "git reset" command is the inverse of the "git add" command, which is used to add files to the Git index.
When the "--soft" option is used, Git is told not to make any changes to the files in the working directory or index.
Hard Reset Git Commit
We saw in the last section how to simply revert the last commit by maintaining the modifications made to the index files.
In other circumstances, you may just want to remove the commit as well as the changes made to the files. This is what the "--hard" option is for.
Execute the "git reset" command with the "--hard" option and specify the commit preceding HEAD ("HEAD~1") to undo the last commit and remove any changes in the working directory and index.
git reset --hard HEAD~1
Mixed Reset Git Commit
You must use the "git reset" command with the "--mixed" option to undo the last Git commit while keeping changes in the working directory but not in the index. Simply append "HEAD~1" to this command for the most recent commit.
git reset --mixed HEAD~1
The file will be removed from the Git index but not from the working directory if you use the "--mixed" option.
As a result, the "--mixed" is a "mix" of the soft and hard resets, thus the name.
Undo Last Commit with Revert
To revert the most recent Git commit, type "git revert" and specify the commit to be reverted, which is "HEAD" for your history's most recent commit.
git revert HEAD
The "git revert" command differs from the "git reset" command in that it creates a new commit that includes the changes introduced by reverting the previous commit.
Also, you specified "HEAD1" with "git reset," because the reset command sets a new HEAD position, whereas reverting really reverts the commit you specified.
As a result, you'll have to commit the modifications again in order to reverse the files and undo the commit.
Undoing Multiple Commits
You can use git reset to go back to any prior commit.
To move the current branch back three commits, for example, you'd use:
git reset --hard HEAD~3
Because we're using --hard
, the command above will delete the commit history of the last three snapshots.
Passing the commit ID to the git reset command is another option to go back to a specific commit.
To find the commit IDs, run git log --oneline
:
git log --oneline
The command displays a list of all commits, together with the commit ID and the first line of the commit message:
Output
32621512 (HEAD -> master) Update changelog
7578724c adding new tests
757862ce new blog post
94d63417 sort configuration file
257232e4 Refactor User class
...
FAQs to Undo Last Git Commit
Can I undo the last Git commit without losing my changes?
Yes, by using git reset HEAD~
, you can undo the last commit while preserving your changes in the working directory.
What happens to the commits that were undone?
When you undo the last Git commit, the commit itself and any subsequent commits are removed from the commit history. These undone commits are not permanently deleted and can be accessed until garbage collection occurs.
Does undoing the last commit revert all changes made in that commit?
Undoing the last commit does not automatically undo the changes made in that commit. It only removes the commit itself from the Git history. If you want to revert the changes made in the commit, you can use the git revert
command.
Can I undo multiple commits at once?
Yes, you can undo multiple commits at once by specifying the number of commits to undo. For example, git reset HEAD~3
will undo the last three commits.
Is it possible to undo a commit after pushing it to a remote repository?
Yes, it is possible to undo a commit after pushing it to a remote repository. However, it is generally not recommended to rewrite Git history if others have already based their work on your pushed commits.
Can I undo a commit and keep it as uncommitted changes?
Yes, you can undo a commit and keep it as uncommitted changes using the command git reset HEAD~ --soft
. This retains the changes in the staging area, allowing you to make further adjustments before committing.
Can I undo a commit that introduced merge conflicts?
Yes, you can undo a commit that introduced merge conflicts by using git reset
or other techniques to modify the commit history. However, resolving the merge conflicts themselves requires manually modifying the affected files.
Conclusion
Use the git reset command to undo the previous commit. Resetting pushed commits could generate a slew of issues for your coworkers.
Leave a comment below if you run into any issues or have any feedback.