0

git reset --mixed (the default option) resets the staging area to a previous state, but not the working tree.

That seems weird. When would I want to do that?

Also, say I made four commits: A, B, C and D. I then reset to B. With each commit, I added a file. What would my staging area look like after performing git reset B?

Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
  • 2
    *That seems weird.* Not really. One reason to use `--mixed` (the default mode of `git reset`) is when you want to preserve the state of your working tree (your local changes), with which the commit you're resetting your branch to may conflict. As for your second question, why don't you try it for yourself in a toy repo? Experimentation is a good way of learning. – jub0bs Jul 04 '15 at 01:34

1 Answers1

2

say I made four commits: A, B, C and D. I then reset to B. With each commit, I added a file. What would my staging area look like after performing git reset B?

git reset first and foremost moves HEAD.
So HEAD is now to B.

git reset --mixed also reset the index (the staging area), so index reflexes B.

That means a git status would show all modification (files added, removed or modified) introduced by C and D as to be staged (since the working tree was unchanged).
That can give you the chance to redo those staging operations, in a different order, with less or more commits.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • So if I understand you correctly, the only use of `git reset --mixed` is to change the branch commit history by recommitting changes in a different order or number of commits? – Aviv Cohn Jul 04 '15 at 16:26
  • And if I want to change the actual project, i.e. the code, `git reset --mixed` isn't what I need? – Aviv Cohn Jul 04 '15 at 16:26
  • @AvivCohn that's one usage. This is generally use to unstage something that you have added to the index, see for instance http://stackoverflow.com/q/17761896/6309 or http://stackoverflow.com/a/6919749/6309 (`git reset --soft` can also came in handy: http://stackoverflow.com/q/5203535/6309). I also usually use `git reset --hard` to reset the working tree as well. – VonC Jul 04 '15 at 16:31
  • So aside from `git reset --hard`, `git reset` is used to unstage things, and 'rewrite' the branches history. But not change actual code. Correct? – Aviv Cohn Jul 04 '15 at 17:09
  • @AvivCohn yes: the working tree is untouched, so the current code remains the same indeed. – VonC Jul 04 '15 at 17:39