I assume you already know the difference:
- index: staged diff
- worktree: unstaged diff
- both: both
As for examples, I'm not sure if this is a good one, but at times I use this feature:
Suppose you are debugging using the GOAT debugger: print. And suppose after 10 print you've found the bug. Now your files have become:
void a() {
cout << __LINE__ << endl;
cout << __LINE__ << endl;
cout << __LINE__ << endl;
}
void b() {
cout << __LINE__ << endl;
cout << __LINE__ << endl;
cout << __LINE__ << endl;
// .......
// .......
// .......
bug_fix();
// .......
// .......
// .......
// .......
cout << __LINE__ << endl;
cout << __LINE__ << endl;
cout << __LINE__ << endl;
}
Then you stage your bug fix, now git status
has become:
unstaged:
modified:
+ cout << __LINE__ ....
+ cout << __LINE__ ....
+ cout << __LINE__ ....
+ cout << __LINE__ ....
+ cout << __LINE__ ....
+ cout << __LINE__ ....
+ cout << __LINE__ ....
+ cout << __LINE__ ....
+ cout << __LINE__ ....
+ cout << __LINE__ ....
staged:
modified:
+ bug_fix();
And now, you wanna compile with bug_fix but without prints, and at the same time you realize, these prints prove useful in your debugging session and you wanna preserve these lines for your future debugging sessions. So you hit stash worktree
. This way, your bug fix is staged; prints are gone and preserved in your stashes for future usage. And you are ready to test one last time before committing your bug fix.
stash index
would take some effort to come up with a good example. I have reasonable (subjectively speaking) use case sometime ago, but I wouldn't think of that a good example.