Can you explain what git reset does in plain English?

Technology CommunityCategory: GitCan you explain what git reset does in plain English?
VietMX Staff asked 3 years ago

In general, git reset function is to take the current branch and reset it to point somewhere else, and possibly bring the index and work tree along.

- A - B - C (HEAD, master)
# after git reset B (--mixed by default)
- A - B (HEAD, master)      # - C is still here (working tree didn't change state), but there's no branch pointing to it anymore

Remeber that in git you have:

  • the HEAD pointer, which tells you what commit you’re working on
  • the working tree, which represents the state of the files on your system
  • the staging area (also called the index), which “stages” changes so that they can later be committed together

So consider:

  • git reset --soft moves HEAD but doesn’t touch the staging area or the working tree.
  • git reset --mixed moves HEAD and updates the staging area, but not the working tree.
  • git reset --merge moves HEAD, resets the staging area, and tries to move all the changes in your working tree into the new working tree.
  • git reset --hard moves HEAD and adjusts your staging area and working tree to the new HEAD, throwing away everything.

Use cases:

  • Use --soft when you want to move to another commit and patch things up without “losing your place”. It’s pretty rare that you need this.
  • Use --mixed (which is the default) when you want to see what things look like at another commit, but you don’t want to lose any changes you already have.
  • Use --merge when you want to move to a new spot but incorporate the changes you already have into that the working tree.
  • Use --hard to wipe everything out and start a fresh slate at the new commit.