How do I undo the most recent local commits in Git?

Learn how do i undo the most recent local commits in git? with practical examples, diagrams, and best practices. Covers git, version-control, git-commit development techniques with visual explanati...

How to Undo Your Most Recent Local Git Commits

Hero image for How do I undo the most recent local commits in Git?

Learn various methods to revert or reset your latest local Git commits, understanding the implications of each approach for your project history.

Accidentally committing changes or realizing a mistake immediately after a commit is a common scenario in Git. Fortunately, Git provides powerful tools to undo or modify your commit history. This article will guide you through the most common and safest ways to undo your most recent local commits, focusing on scenarios where changes have not yet been pushed to a remote repository.

Understanding Git's Undo Mechanisms

Git offers several commands to undo changes, each with different effects on your working directory, staging area (index), and commit history. The primary commands we'll explore are git reset and git revert. While git reset is often used for local changes, git revert is generally preferred for changes that have already been pushed to a shared remote repository because it creates a new commit that undoes the previous one, preserving history.

flowchart TD
    A[Start: Recent Commit] --> B{Undo Type?}
    B -->|Modify last commit| C[git commit --amend]
    B -->|Discard commit, keep changes| D[git reset --soft HEAD~1]
    B -->|Discard commit, unstage changes| E[git reset --mixed HEAD~1]
    B -->|Discard commit, discard changes| F[git reset --hard HEAD~1]
    B -->|Create undo commit| G[git revert HEAD]
    C --> H[End: Last commit modified]
    D --> I[End: Changes staged]
    E --> J[End: Changes unstaged]
    F --> K[End: Changes lost]
    G --> L[End: New revert commit created]

Decision flow for undoing Git commits

Method 1: Modifying the Last Commit (git commit --amend)

If you've just made a commit and immediately realize you forgot to include a file, made a typo in the commit message, or want to add a small fix, git commit --amend is your best friend. This command allows you to combine staged changes with the previous commit, effectively replacing the old commit with a new one. It's important to note that this rewrites history, so it should only be used on local commits that haven't been pushed.

# Stage any additional changes you want to include
git add forgotten_file.txt

# Amend the last commit
git commit --amend -m "New, corrected commit message"

Amending the last commit with new changes and a new message

Method 2: Undoing the Last Commit with git reset

git reset is a powerful command for undoing commits by moving the HEAD pointer to a different commit. It has three main modes: --soft, --mixed (default), and --hard, each affecting your working directory and staging area differently. This command also rewrites history and should only be used on local, unpushed commits.

Soft Reset

git reset --soft HEAD~1

This command moves HEAD back one commit, but keeps all changes from the undone commit in your staging area (index). Your working directory remains unchanged. This is useful if you want to undo the commit but keep the changes staged, ready to be re-committed or modified.

git reset --soft HEAD~1

Mixed Reset (Default)

git reset --mixed HEAD~1

This is the default behavior of git reset. It moves HEAD back one commit and unstages the changes from the undone commit, but keeps them in your working directory. This is ideal if you want to undo the commit and then re-evaluate which changes to stage and commit.

git reset --mixed HEAD~1
# Or simply:
git reset HEAD~1

Hard Reset

git reset --hard HEAD~1

This is the most destructive option. It moves HEAD back one commit, discards all changes from the undone commit from both your staging area and your working directory. Use this with extreme caution, as it will permanently delete any uncommitted changes and the changes introduced by the undone commit.

git reset --hard HEAD~1

Method 3: Reverting a Commit (git revert)

git revert is different from git reset because it doesn't rewrite history. Instead, it creates a new commit that undoes the changes introduced by a previous commit. This makes it a safe option for commits that have already been pushed to a shared remote repository, as it maintains a linear history and doesn't force others to rebase their work.

# Revert the most recent commit
git revert HEAD

# Git will open your editor to confirm the revert commit message.
# Save and close the editor to complete the revert.

# To revert a specific commit by its hash (e.g., abcdefg)
git revert abcdefg

Reverting the last commit or a specific commit