Data report"State of code review 2024" is now liveRead the full report

Git undo last commit

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


If you're using Git for version control, you might occasionally make a commit that you later want to undo. This is a common issue that can disrupt your Git operations if not handled correctly. Fortunately, Git provides several ways to undo the last commit, allowing you to keep your repository clean and your history accurate.

Follow these steps to undo your last Git commit:

1. Check the commit you want to undo: Before you undo a commit, ensure you're undoing the correct one. Use the command git log to display the commit history. The most recent commit will be at the top.

Terminal
git log
Stop wrestling with Git commands

The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop Googling Git commands.

Learn more

2. Undo the last commit but keep the changes: If you want to undo the commit but keep the changes in your working directory, use the git reset command with the --soft flag, followed by HEAD~.

Terminal
git reset --soft HEAD~

This command moves the HEAD pointer back by one commit, but it leaves your working directory and staging index as they were, so you can make additional changes and re-commit when ready.

3. Completely undo the last commit: If you want to completely discard the last commit—changes and all—use the git reset command with the --hard flag.

Terminal
git reset --hard HEAD~

Use this command with caution, as it permanently discards the last commit's changes.

While Git is an incredibly useful tool, it has many shortcomings, particularly with rebasing, and managing stacked pull requests.

The Graphite CLI simplifies git, handles rebasing automatically, and allows you to create, submit, and stack pull requests right from the command line.

Under the hood, the CLI runs Git to create branches, commits, and metadata, which means you can still use Git in your scripts, tooling, or whenever you feel like it. Read more about installing the Graphite CLI in our docs.

screenshot of the Graphite CLI

While undoing a Git commit is usually straightforward, there are a few common pitfalls to be aware of:

  • Be sure you're undoing the right commit: Before running any git reset command, always check the commit history with git log. This will help you avoid undoing a commit you didn't intend to.

  • Be careful with git reset --hard: This command permanently discards changes, so use it with caution. If you only want to remove the commit but keep the changes in your working directory, use git reset --soft.

  • Don't try to undo a pushed commit: If you've already pushed your commit to a remote repository, undoing it locally won't remove it from the remote repository. Instead, you'll need to force push the undo, which can cause problems for other users. If you need to undo a pushed commit, consider using git revert instead, which creates a new commit that undoes the changes of the commit you want to discard.

Q: What's the difference between git reset --soft HEAD~ and git reset --hard HEAD~?

A: git reset --soft HEAD~ undoes the last commit but leaves your changes in your working directory, so you can modify them and re-commit. git reset --hard HEAD~ discards the last commit and all associated changes permanently.

Q: Can I undo more than one commit?

A: Yes, you can undo multiple commits by adding a number after the ~ in the git reset command. For example, git reset --hard HEAD~3 will undo the last three commits.

Q: I've already pushed my commit to the remote repository. Can I still undo it?

A: Yes, but you'll need to force push the undo with git push --force, which can disrupt other users' work. Consider using git revert instead, which undoes a commit by creating a new commit with the opposite changes.

The best engineers use Graphite to simplify Git

Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.

Git started for free

Undoing a Git commit is a common operation that's essential to maintaining a clean and accurate commit history. Whether you need to discard a commit entirely or just undo the commit while keeping the changes, Git gives you the tools to do it. Just remember to use these commands with care, especially if you're working in a shared repository. If

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Learn more

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2