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

How to use git soft reset

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

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


A soft reset in Git allows you to undo changes to your commit history while keeping the working directory intact. This guide will explain how to use the soft reset command effectively, including various use cases and practical examples.

Git offers three main types of resets: soft, mixed, and hard. Each affects the current branch and staging area differently:

  • Soft reset (--soft): This type of reset moves the current branch's HEAD to a specified commit but does not alter the index (staging area) or the working directory. It only changes where the current branch is currently pointing. This is useful when you want to undo commits but keep all your files and changes exactly as they are for recommitting.

  • Mixed reset (--mixed): This is the default mode for git reset. It moves the HEAD to a specified commit and also updates the index (staging area) to match this commit. However, it leaves the working directory (your current work) untouched. This means that the changes in your working directory stay as they are, but they are no longer staged.

  • Hard reset (--hard): This reset type moves the HEAD to a specified commit and resets both the index (staging area) and the working directory to match this commit. Any changes in the staging area and the working directory will be discarded. This is a more drastic command that can be used to completely undo changes, ensuring that your current branch exactly reflects the specified commit.

The git reset --soft command is useful when you need to undo commits but want to retain the changes for further modification or to be committed again later.

  1. Identify the commit to which you want to reset.

    • Use git log to view the commit history and find the commit hash or reference you need.
    Terminal
    git log --oneline
  2. Perform a soft reset.

    • Replace <commit-hash> with the hash of the commit you want to reset to:
    Terminal
    git reset --soft <commit-hash>
  • Undo the last commit:

    Terminal
    git reset --soft HEAD~1

    This command moves the HEAD to the commit before the last, effectively undoing the last commit while keeping the changes in your staging area.

  • Prepare a new commit after resetting: After performing a soft reset, your changes from the commits you undid will remain staged:

    • Make any necessary updates or additional changes.
    • Then commit again:
      Terminal
      git commit -m "Revised commit"
  • Check your current state: Always use git status to check your current branch and modifications. This helps you avoid resetting the wrong branch or losing track of your changes.
  • Backup before resetting: Consider creating a backup branch before performing a reset, especially if you're new to the process:
    Terminal
    git branch backup-branch
  • Use reset responsibly: Since git reset can rewrite commit history, use it cautiously in shared branches. Communicate with your team when performing resets that affect shared history.

For further reading on soft resets in Git, see the official Git documentation.

Git gud
"It's the first Git workflow I've used that actually feels good."
–@robboclancy
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2