Practical Git guide

Undo the last Git commit while keeping your changes

Use a soft reset to revise an unpublished last commit without losing its files or staged changes.

Before you start

You committed an incomplete change locally and want to redo the commit. This exercise applies to an unpublished tip with a parent commit. For a commit already shared, use the revert guide instead so collaborators’ history is preserved.

Use Git 2.28 or newer in a POSIX shell on macOS/Linux, or Git Bash on Windows. Check git --version; Git's installation page covers setup. Each example starts in a new temporary folder. Keep that folder if you want to revisit your work; none of these commands removes your existing projects. The identity and signing settings below apply only to the practice repository.

Work through the example

practice=$(mktemp -d "${TMPDIR:-/tmp}/gitglow-learn.XXXXXX")
sandbox="$practice/project"
mkdir "$sandbox"
cd "$sandbox"
git init -b main
git config --local user.name "Git Learner"
git config --local user.email "learner@example.invalid"
git config --local commit.gpgsign false
printf 'A small project\n' > notes.txt
git add notes.txt
git commit -m "Start notes"
printf 'Incomplete addition\n' >> notes.txt
git add notes.txt
git commit -m "Work in progress"
git log -2 --oneline
git branch backup/before-undo
git reset --soft HEAD~1
git status --short
git diff --cached
cat notes.txt

Check the result

main now points to Start notes; the added line is unchanged on disk and remains staged (M in short status). backup/before-undo still points to the original second commit. --soft moves the branch tip while preserving the index and working files. A mixed reset keeps working files but unstages changes; --hard can discard work and is unnecessary here.

Try it yourself

Finish the addition, stage the updated file, and create a replacement commit with a better message. Compare main with backup/before-undo in the graph.

If something goes wrong

If HEAD~1 is invalid, this may be the first commit and the example does not apply. Inspect the graph first. Git alone cannot tell you every place a commit was shared; if publication is uncertain, fetch and check your team’s workflow before rewriting.

Use this workflow in GitGlow

Inspect the tip and publication context in GitGlow History, then run the soft-reset command in its Free terminal; this guide does not imply a dedicated graphical soft-reset action. GitGlow’s guarded history-editing actions require Pro and have their own publication checks.

Download GitGlow Free

Go deeper with the official Git docs

These references cover command options and behavior beyond this example.