I am an expert in Git, and I understand that hitting ‘commit’ too soon is a mistake we all make. Thankfully, Git has tools to undo those hasty decisions. Let’s explore how to correct commits and maintain smooth project progress with ease.
1. Undo the Last Commit & Keep Changes
If you want to undo the last commit but keep the changes in your working directory, you can use:
git reset HEAD~1
This command moves the HEAD pointer back one commit, but leaves your working directory as it was before you ran the command.
2. Undo the Last Commit & Discard Changes
If you want to completely discard the changes made in the last commit, use:
git reset --hard HEAD~1
Be cautious with this command, as it will remove all changes made in the last commit from your working directory.
3. Undo a Commit Made Some Time Ago
If you need to undo a commit that wasn’t the last one, you’ll need to find the commit ID of the commit you want to revert to. You can view the commit history with:
git log
Once you’ve identified the commit you want to revert to, use:
git reset --hard <commit-id>
Replace <commit-id>
with the actual commit ID. This will move your HEAD pointer to the specified commit and discard all changes made after it.
4. Reverting a Commit
Another way to undo changes without altering the history is using git revert
. This creates a new commit that undoes the changes made by a previous commit:
git revert <commit-id>
Warning
Be careful when using commands that alter the history, especially git reset --hard
and git revert
on commits that have been pushed to a shared repository. These actions can lead to conflicts with other collaborators’ work. If a commit has been pushed, it’s usually safer to use git revert
because it keeps the history and adds a new commit that reverses the changes.
Example 1: Undoing the Last Commit, Keeping Changes in Staging Area
Suppose you have made a commit but realize you need to make further changes before committing again. Here’s how you can undo the last commit while keeping the changes staged:
# Make necessary changes to your files
# Stage the changes
git add .
# Commit the changes
git commit -m "Commit message"
# After committing, you realize you need to make further changes
# Undo the last commit while keeping changes staged
git reset --soft HEAD~1
In this example, the git reset --soft HEAD~1
command undoes the last commit while keeping your changes staged, allowing you to make further modifications before committing again.
Example 2: Undoing the Last Commit and Discarding Changes
Suppose you accidentally made a commit with incorrect changes and want to completely discard those changes. Here’s how you can undo the last commit and discard the changes:
# Make necessary changes to your files
# Stage the changes
git add .
# Commit the changes
git commit -m "Incorrect commit"
# Realize the commit was incorrect and need to undo it
# Undo the last commit and discard changes
git reset --hard HEAD~1
In this example, the git reset --hard HEAD~1
command not only undoes the last commit but also discards any changes made after that commit, effectively reverting your working directory to the state it was in before the last commit.
Remember to use these commands with caution, especially git reset --hard
, as it can result in the loss of uncommitted changes. Always ensure you have backups or are willing to discard the changes before using it.
Can I undo a commit that’s already been pushed?
git revert
for commits that have been pushed to avoid disrupting the project for others.What’s the difference between git reset
and git revert
?
git reset
changes the history of your branch, while git revert
creates a new commit to undo changes without altering history.How can I undo multiple commits?
git reset
or git rebase -i
can be used, depending on whether you want to keep the changes or not.