Git
Uncommit
Version Control System
Coding
Programming Tips

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.

Browse interview questions

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:

  1. Soft Reset (--soft)
  2. Mixed Reset (--mixed, which is the default)
  3. 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.
bash
  git reset --soft HEAD~1

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).
bash
  git reset --mixed HEAD~1

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

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.

bash
git revert HEAD

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:

bash
git commit --amend

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

CommandEffect on HEADEffect on IndexEffect on Working DirUse Case
git reset --soft HEAD~1MovedUnchangedUnchangedUncommit but keep changes staged
git reset --mixed HEAD~1MovedUnstagedUnchangedUncommit and unstage changes
git reset --hard HEAD~1MovedClearedDiscardedDiscard all work
git revert HEADNew commitUnchangedUnchangedUndo commit with history intact
git commit --amendModifiedCan be modifiedCan be modifiedCorrect 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.