1. Undo the Last Commit but Keep Changes in Your Working Directory**
If you want to undo the last commit but keep all the changes in your working directory (so you can review or modify them before recommitting), use the following command:
bash
Code: Select all
git reset --soft HEAD~1
- What it does: This command moves the `HEAD` pointer back to the previous commit, but it leaves your changes staged and your working directory untouched.
- When to use it: Use this when you want to rework your commit message or add/remove files before committing again.
2. Undo the Last Commit and Unstage Changes**
If you want to undo the last commit and unstage all the changes (but still keep them in your working directory), use:
bash
Code: Select all
git reset HEAD~1
- When to use it: Use this when you want to completely rework your changes before committing again.
3. Undo the Last Commit and Discard All Changes**
If you want to completely undo the last commit and discard all changes in your working directory, use:
bash
Code: Select all
git reset --hard HEAD~1
- When to use it: Use this when you want to completely remove the last commit and all associated changes.
4. Undo Multiple Commits
If you need to undo multiple commits, you can specify how many commits to undo by replacing `1` with the number of commits you want to revert. For example, to undo the last 3 commits:
bash
Code: Select all
git reset --soft HEAD~3
- When to use it: Use this when you need to undo multiple commits but keep the changes for further editing.
5. Create a New Commit That Reverts the Changes**
If you’ve already pushed the commit to the server or want to preserve the commit history, you can create a new commit that reverts the changes:
bash
Code: Select all
git revert HEAD
- When to use it: Use this when you want to maintain a clean commit history and avoid rewriting it.
6. Check the Commit History
Before undoing any commits, it’s a good idea to review your commit history to ensure you’re targeting the correct commit. Use the following command to view the commit history:
bash
Code: Select all
git log --oneline
- When to use it: Use this to confirm which commit(s) you want to undo.
7. Recover a Deleted Commit
If you accidentally reset a commit and want to recover it, you can use the `git reflog` command to find the commit hash and reset back to it:
bash
Code: Select all
git reflog
git reset --hard <commit-hash>
- When to use it: Use this if you’ve lost a commit and need to restore it.
Best Practices
- Always double-check: Before running any reset or revert commands, review your commit history using `git log` or `git reflog`.
- Use `--soft` or `--mixed` first: These options are safer than `--hard` because they don’t discard your changes.
- Avoid rewriting pushed history: If you’ve already pushed the commit, prefer `git revert` to avoid conflicts for other collaborators.
Summary
Here’s a quick reference table for undoing commits:
| Command | Effect |
|-----------------------------|------------------------------------------------------------------------|
| `git reset --soft HEAD~1` | Undo commit, keep changes staged |
| `git reset HEAD~1` | Undo commit, unstage changes |
| `git reset --hard HEAD~1` | Undo commit, discard all changes |
| `git revert HEAD` | Create a new commit that undoes the last commit |
| `git log --oneline` | View commit history |
| `git reflog` | Recover lost commits |
By following these steps, you can confidently undo local commits in Git and keep your repository clean and organized. Whether you need to rework a commit, discard changes, or revert a mistake, Git provides the tools to help you fix errors efficiently.