Git
version control
commit
undo commit
git reset

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

When working with Git, it's common to make a commit and then realize that it needs to be undone for various reasons. Whether the commit contains an error, was made on the wrong branch, or needs modifications, knowing how to "uncommit" can be invaluable. This guide explores methods to undo your last Git commit, explaining the concepts and demonstrating the steps through examples.

Understanding Git Commits

Before diving into the technical process, let's briefly understand what a commit is. In Git, a commit is a snapshot of your changes. Commits have unique SHA-1 hash identifiers, tracking alterations over time. They're essential for version control, as they enable you to revert, merge, and manipulate changes within a repository.

Method 1: Using git reset

One way to uncommit your last commit is by using git reset. This command can alter the commit history, which is useful for undoing the most recent commit while preserving changes in a working directory.

Command Syntax

bash
git reset [option] HEAD~1

Options:

  • --soft: Moves HEAD to a previous commit, keeping changes staged.
  • --mixed: Resets to a previous commit, unstaging changes without removing them.
  • --hard: Discards all changes and moves HEAD to a specific commit.

Example:

bash
1# Reset but keep changes staged
2git reset --soft HEAD~1
3
4# Reset and unstage changes without removing them
5git reset --mixed HEAD~1
6
7# Completely remove all changes
8git reset --hard HEAD~1

Considerations

  • --soft: Use if you simply want to adjust the commit message or make minor changes.
  • --mixed: Ideal for unstaging changes to work on them further without losing them.
  • --hard: Be cautious, as it will remove all changes permanently.

Method 2: Using git revert

If you want to uncommit but maintain a record of the changes, git revert is the apt command, especially for shared branches.

Command Syntax

bash
git revert [commit_hash]

Example:

bash
1# Identify the commit hash of the commit to revert
2git log
3
4# Revert the last commit
5git revert HEAD

Considerations

  • Reverting creates a new commit that undoes the changes from a specified commit.
  • Suitable for public repositories where altering history can be problematic.
  • Commit history remains clean and traceable.

Method 3: Amending Commits

If your aim is to fix the last commit, such as correcting a commit message, git commit --amend might be what you need.

Command Syntax

bash
git commit --amend

Example:

bash
1# Make changes to files if needed
2# Stage changes
3git add .
4
5# Amend the last commit
6git commit --amend

Considerations

  • Beneficial for correcting recent commit mistakes.
  • Use when commit changes aren't public, as it alters history.
  • Avoid using on pushed commits to prevent confusion.

Summary Table

MethodDescriptionUse CaseCommands
git resetMoves HEAD to a specific commitIdeal for local, unpushed changesgit reset --soft HEAD~1 git reset --mixed HEAD~1 git reset --hard HEAD~1
git revertReverts a specific commit by creating a new oneMaintains history, suitable for publicgit revert HEAD
AmendEdits the last commitCorrects recent commit messages/changesgit commit --amend

Conclusion

Choosing the right method depends on your project's context and whether the changes are shared with collaborators. git reset is effective for local adjustments, while git revert maintains historical integrity on shared branches. Amending commits is best reserved for minor corrections before sharing.

Understanding these techniques will enhance your proficiency and flexibility when managing projects in Git, aligning with the ethos of version control: iterative, collaborative, and reversible by nature.



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.