Git
Un-commit
Version Control
Coding Tips
GitHub

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:

bash
git reset --soft HEAD~1
  • HEAD~1 is a shorthand for the commit before head.
  • The --soft option 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:

bash
git reset HEAD~1
  • 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

  1. 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 log to see the recent commits.
bash
    git log --oneline
  1. Reset the commit: According to your preference (keeping changes staged or unstaged), run the appropriate reset command:
bash
    git reset --soft HEAD~1
    # or
    git reset HEAD~1
  1. 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.
  2. Amend if necessary: If you only intended to change the commit message, you can now use git commit --amend to fix the message without altering the content of the commit.
  3. 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

CommandEffect on IndexEffect on Working Directory
git reset --soft HEAD~1Leaves changes stagedNo changes
git reset HEAD~1Unstages changesNo changes
git reset --hard HEAD~1Resets indexDiscards all changes (Warning: Potential data loss)

Additional Considerations

  • Avoiding Data Loss: Use --hard with 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 --force after 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.


Course illustration
Course illustration

All Rights Reserved.