35

I have a lot of files changed in my project. I want to stash 2 files but i'm a bit afraid to make a mistake since i never did this.

If i would stash now, will it only stash the 2 staged files? enter image description here

And if I don't mark the "Keep staged changes", will it then revert to how it was before? This might sound stupid, but better save then sorry.

SQB
  • 3,926
  • 2
  • 28
  • 49
clankill3r
  • 9,146
  • 20
  • 70
  • 126

3 Answers3

52

This applies to Git in general, not just with SourceTree. When you stash changes, the items that will be stashed are the changes to tracked files in your working copy and in the staging area. Those changes will be saved in the stash, and reverted in the working copy and index.

When you choose to keep changes in the index/staging area, those changes will still be stashed, but Git won't also revert them in the staging area. This is useful if, for example, you make several unrelated changes, and you want to run tests only some of those changes, without having the unrelated ones affect the test.

Stashing is safe. If you want to get your stashed changes back, you just pop them back out of the stash.

However, untracked files aren't normally stashed. If you want to also stash those files, you need to pass an additional option to git stash on the command line (SourceTree for Windows doesn't currently have such an option. I don't know if the same is true for the Mac version):

git stash save --include-untracked
# Or shorter
git stash save -u

See Also

  • SourceTree still doesn't support this. Even when stashing from command line, SourceTree fails to show the untracked files as changes in diff output. I wouldn't rely on SourceTree to actually being able to bring back the untracked files, and would do the `git stash pop|apply` also from command line. – kaskelotti Jul 11 '16 at 12:44
  • @kaskelotti the "git stash save -u" works, the untracked files are not shown in the "stash items" in SourceTree, but they will appear when you apply the stash. – Alan Zhiliang Feng Jul 28 '16 at 19:24
  • Also ........ the "STASHES" item on the left bar does not show up if you have selected "Current Branch" in the top left above the list of commits (and you are not on the branch that the stash was created). So to make it appear ... select "All Branches". Be careful not to run the 'git reflog' statement as it will most likely DELETE all your stashes. And to retrieve them you have to play with the command line. – goamn Jan 26 '17 at 23:28
  • It works if you stage the untracked file and then stash it. – craigmoliver Apr 01 '19 at 17:02
10

Yes, but stashing in this case would mean two scenarios -

  1. Checking the Keep staged changes: staged files would still be in your staging area, but all your modified unsaved files will be stashed.

  2. Without checking the staged changes: all your files will be stashed.

gravetii
  • 9,273
  • 9
  • 56
  • 75
2

I'm also looking for a way to stash only a few items from my working copy and I stumbled upon this question. I found a workaround that was mentioned in the official Atlassian forums. I tried it using SourceTree version 3.3.8 for Windows and it works for me.

To stash select files:

  • Stage the files you want to stash

  • Then stash all files, but making sure that 'Keep staged changes' is checked

  • Now you only have the files you want to stash in your current working copy

  • Stash all files, with 'Keep staged changes' unchecked

You can then re-apply the first stash, and discard the files that you wanted to stash.

Annie Lagang
  • 3,185
  • 1
  • 29
  • 36