How to undo the most recent local commits in Git?
If you need to undo the most recent local commits in Git, there are different approaches depending on whether or not you want to keep the changes made in those commits. Here’s a detailed explanation with examples:
1. Undo the Last Commit but Keep Changes in the Working Directory
Use git reset with the --soft option. This undoes the last commit but keeps all changes staged.
Command:
git reset --soft HEAD~1
Example:
git reset --soft HEAD~1
- Effect: The changes in the last commit remain in the staging area, ready to be committed again or modified.
2. Undo the Last Commit and Unstage the Changes
Use git reset with the --mixed option. This undoes the last commit and unstages the changes, leaving them in your working directory.
Command:
git reset --mixed HEAD~1
Example:
git reset --mixed HEAD~1
- Effect: The changes in the last commit are no longer staged but remain in your working directory for further edits or staging.
3. Undo the Last Commit and Discard Changes
Use git reset with the --hard option. This undoes the last commit and discards all changes in the working directory.
Command:
git reset --hard HEAD~1
Example:
git reset --hard HEAD~1
- Effect: The last commit is removed, and all changes are permanently lost (unless backed up elsewhere).
4. Undo Multiple Commits
If you want to undo multiple commits, specify the number of commits to reset using HEAD~n where n is the number of commits.
Command:
git reset --soft HEAD~n
git reset --mixed HEAD~n
git reset --hard HEAD~n
Example:
Undo the last 3 commits but keep changes in the working directory:
git reset --mixed HEAD~3
5. Undo a Specific Commit Without Affecting Others
If you want to undo a specific commit but keep subsequent commits intact, use git revert.
Command:
git revert <commit-hash>
Example:
git revert abc1234
- Effect: Creates a new commit that negates the changes introduced by the specified commit.
6. Undo a Commit That Has Been Pushed to a Remote Repository
If you’ve already pushed the commit to a remote repository and need to undo it:
Option 1: Revert the Commit
git revert <commit-hash>
git push
- Effect: This is a safe way to undo a commit that has already been shared.
Option 2: Force Push After Reset
git reset --hard HEAD~1
git push --force
- Effect: This rewrites history but can cause problems for collaborators. Use with caution.
7. Check the Commit History
Before undoing anything, review your commit history using:
git log --oneline
Example Scenario
Current Commit History:
abc1234 (HEAD -> main) Added feature X
def5678 Improved performance
ghi9012 Fixed bug Y
Undo the last commit (Added feature X) and keep changes staged:
git reset --soft HEAD~1
Undo the last commit and discard all changes:
git reset --hard HEAD~1
Revert a specific commit (def5678):
git revert def5678
Summary of Commands
| Command | Effect |
|---|---|
git reset --soft HEAD~1 | Undo the last commit, keep changes staged. |
git reset --mixed HEAD~1 | Undo the last commit, keep changes unstaged in the working directory. |
git reset --hard HEAD~1 | Undo the last commit, discard changes. |
git revert <commit-hash> | Create a new commit that undoes a specific commit. |
git reset --hard HEAD~n | Undo the last n commits and discard changes. |
git push --force | Force push after rewriting history (use with caution). |
No images available.