How to uncommit my last commit in Git
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Uncommitting changes in Git is a common requirement in the workflow of many developers. Whether you need to modify a commit message, delete or add files, or completely undo changes, Git provides several options to handle these tasks. Below, we discuss various methods to "uncommit" the last commit in Git, along with their implications and examples.
Understanding Git Resets
One of the primary methods to uncommit changes is using the git reset command. There are mainly three ways you can reset the HEAD (the pointer to the current branch) in your git repository:
- Soft Reset (
--soft) - Mixed Reset (
--mixed, which is the default) - Hard Reset (
--hard)
Each of these options impacts your index (staging area) and working directory differently. Here are the details:
- Soft Reset: This reset method doesn’t touch the index file or the working tree at all. It simply moves the HEAD to the designated commit. Changes from the previous commit are preserved in the staging area.
Here, HEAD~1 indicates the commit before the last commit.
- Mixed Reset: In this reset, the HEAD is reset to a previous commit, and the changes are preserved but unstaged (moved to the working directory).
While --mixed is the default reset mode, you can still specify it explicitly.
- Hard Reset: This reset method resets the HEAD, index, and working directory. Any changes that were made in the previous commit are discarded completely.
Use this command with caution as it permanently removes all the changes.
Reverting a Commit
An alternative to resetting a commit is using git revert. Unlike reset, revert doesn't alter the history on the public branch. This is particularly useful in collaborative environments. git revert creates a new commit that undoes the changes made by previous commits.
Amending a Commit
If you only need to correct a mistake in the commit message or modify the last commit slightly (for instance, adding a missing file), you can use:
This doesn't uncommit your changes but rather amends the last commit. It replaces the last commit with the amended one.
When to Use Each Method
Here’s a quick guide:
- Git reset is useful for local cleanup, especially before pushing changes.
- Git revert is preferred if the commits are already pushed to a shared repository where rewriting history could cause issues for other collaborators.
- Git amend helps modify the most recent commit for minor adjustments.
Table: Summary of Commands and Their Effects
| Command | Effect on HEAD | Effect on Index | Effect on Working Dir | Use Case |
git reset --soft HEAD~1 | Moved | Unchanged | Unchanged | Uncommit but keep changes staged |
git reset --mixed HEAD~1 | Moved | Unstaged | Unchanged | Uncommit and unstage changes |
git reset --hard HEAD~1 | Moved | Cleared | Discarded | Discard all work |
git revert HEAD | New commit | Unchanged | Unchanged | Undo commit with history intact |
git commit --amend | Modified | Can be modified | Can be modified | Correct the last commit |
Additional Considerations
- Be careful with hard resets and amending in public branches as they rewrite history.
- Always double-check which commit is currently checked out before performing a reset.
- You might consider stashing changes before reset if unsure about discarding changes.
Using these commands responsibly allows maintaining a clean and navigable Git history, crucial for collaboration and effective version control.
Related reading
- How to uncommit my last commit in Git
- How to undo a git merge with conflicts
- How to undo a git merge with conflicts
- How to undo a git pull?
- How to undo git commit --amend done instead of git commit
- How to undo local changes to a specific file
- How to unstage large number of files without deleting the content
- How to unstash only certain files?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.