Reputation: 2205
I read about git-stash and dont understand what the -index
does
I understand what stash does, apply pop ans so on but this -index
not
I read whats-the-difference-between-git-stash-apply-and-git-stash-apply-index but don't understand it. Would someone draw a scenario?
--index This option is only valid for pop and apply commands.
Tries to reinstate not only the working tree’s changes, but also the index’s ones. However, this can fail, when you have conflicts (which are stored in the index, where you therefore can no longer apply the changes as they were originally).
Upvotes: 1
Views: 763
Reputation: 51810
Suppose you work on your master
branch, and that your master branch contains a file foo.txt
, whose content is :
first line: master
You edit that file, and add a new line :
first line: master
second line: index
and you run git add foo.txt
but don't commit yet ;
before committing, you add a third line :
first line: master
second line: index
third line: worktree
Now, for some reason, you run git stash
.
If you restore your stash using git stash pop
or git stash apply
, the 2 lines version will be "forgotten", you will only get back the 3 lines version, and it will be placed in the index.
If you add --index
, however, you will retrieve the 2 lines version in the index, and the 3 lines version on disk, not added yet.
The caveat about the index is: if you try to reapply the stash on a different commit, and restoring the stashed changes trigger conflicts : git uses the index to store the conflicting versions, and git stash
may not be able to separate the worktree version and the indexed version.
When you have both staged files and modified-but-unstaged files, it can be useful to get back to that state.
The default behavior of git stash pop / git stash apply
is to restore everything in the index, so you would have to manually select the files you want to actually stage and the files you want to unstage.
If your intention is to have "what was staged" in the index, and not mix it with "what was not staged", that's when the --index
comes in handy.
That being said, I, for one, most often don't use --index
when restoring my stash.
Upvotes: 4