How to un-commit last un-pushed git commit without losing the changes
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with Git, a common scenario you might encounter is the need to undo your last commit while retaining all the changes you made in your working directory. This can be useful in various contexts, such as when you realize you've committed the wrong changes, included undesired files, or simply need to modify your last commit message. Here, we will explore a method to achieve this without losing any of your hard work.
Using git reset
One of the most straightforward methods to un-commit changes is by using the git reset command. git reset moves the current branch backward to a specific state, and there are several ways in which it can be wielded to influence both your staging area (index) and working directory.
Soft Reset
The soft reset is perhaps the easiest way to undo your last commit while keeping all affected files in your staging area:
HEAD~1is a shorthand for the commit before head.- The
--softoption tells Git to keep all of the changes in the staging area.
Mixed Reset
By default, if no flag is provided, Git performs a 'mixed' reset:
- This command will undo the last commit and unstage the changes, but the changes themselves will remain in your working directory.
Detailed Steps and Explanation
- Check the commit log: Before resetting, it's often a good idea to check the commit log to ensure you are resetting the correct commit. Use
git logto see the recent commits.
- Reset the commit: According to your preference (keeping changes staged or unstaged), run the appropriate reset command:
- Reassess the code: At this point, your code will be in the desired state (either staged or not). You can modify, redo, or refine your changes as needed.
- Amend if necessary: If you only intended to change the commit message, you can now use
git commit --amendto fix the message without altering the content of the commit. - Continue working or re-commit: Once your changes are ready and in the desired state (either in the working directory or still staged), you can proceed to commit them again, if necessary.
Table Summary
| Command | Effect on Index | Effect on Working Directory |
git reset --soft HEAD~1 | Leaves changes staged | No changes |
git reset HEAD~1 | Unstages changes | No changes |
git reset --hard HEAD~1 | Resets index | Discards all changes (Warning: Potential data loss) |
Additional Considerations
- Avoiding Data Loss: Use
--hardwith caution. It will permanently remove all changes to tracked files since the last commit. - Working with Remote Repositories: These commands only affect your local repository. If you have already pushed your commit, you will need to use
git push --forceafter your reset to update the remote repository. However, this can disrupt workflows for others if you're in a collaborative environment. - Best Practices: Frequently commit and push smaller units of work to minimize disruption and decrease the necessity for large undos.
By integrating these commands into your workflow judiciously, you can leverage Git's powerful features for source code management without fear of losing data.

