Git Alias to Undo the Last Commit

Create a git alias that undoes the most recent commit while keeping all changes staged. Safe way to uncommit without losing work.

Workflow Aliases

Detailed Explanation

Undo the Last Commit Safely

The undo alias performs a soft reset that removes the last commit but keeps all its changes in the staging area:

[alias]
    undo = reset --soft HEAD~1

Reset Modes Compared

Understanding the three reset modes is essential:

Mode Commit Staging Area Working Directory
--soft Removed Preserved Preserved
--mixed (default) Removed Cleared Preserved
--hard Removed Cleared Cleared

The --soft mode is the safest because it preserves everything. Your changes remain staged, ready to be recommitted with a different message, split into multiple commits, or further modified.

Common Use Cases

Fix a commit message:

git undo
git commit -m "Better commit message"

Split a commit into two:

git undo
git add src/auth/
git commit -m "Add authentication module"
git add src/api/
git commit -m "Add API endpoints"

Remove a file from the commit:

git undo
git reset HEAD secret-config.yml
git commit -m "Add feature (without secrets)"

Multiple Undos

To undo the last N commits, change the number:

[alias]
    undo2 = reset --soft HEAD~2
    undo3 = reset --soft HEAD~3

Safety Net

Unlike --hard reset, the --soft undo is completely non-destructive. No work is lost. If you change your mind, simply run git commit again and your original commit is effectively restored (with a new hash).

Use Case

Use this alias when you committed too early, made a typo in the commit message, accidentally included a file that should not have been committed, or want to restructure recent commits before pushing. It is one of the most frequently used git aliases in practice.

Try It — Git Alias Builder

Open full tool