George Garside Blog

A place of many ramblings about macOS and development. If you find something useful on here, it's probably an accident.

git.day/13 • See all at git.day

It's often the case that git-reset is used to reset changes in the working copy. The --hard option is frequently used to change the files to a previously committed state, such as discussed in Git of the day #6: git reset --hard ORIG_HEAD undoing a complex operation.

As an opposite to --hard, the --soft option keeps the working copy as is. This resets HEAD to a given commit without changing the files, and indeed stages the changes that would have been removed with --hard.

Since --soft keeps the changes staged, it can be thought of as undoing the commit, but keeping the add. That is, the usual flow of operations is

  1. make changes,
  2. git add file,
  3. git commit

and therefore git reset --soft HEAD^, as HEAD^ is the parent commit of HEAD, undoes step 3 without undoing step 2. Therefore, the operations to undo each of the above steps are

  1. git reset --hard HEAD^
  2. git reset --mixed HEAD^ (equivalent to omitting --mixed, i.e. git reset HEAD^)
  3. git reset --soft HEAD^

This gives a simple equivalent to each command making it easy to undo these operations without unintentional loss of changes.

Leave a Reply

No comments