1

currently I got a few modified files into my local branch:

modified:   commands/abc/Test.cs
modified:   common/des/Info.cs
modified:   common/pit/Abc.cs
modified:   services/Services123.cs

I want to put to stash just first 3. How can I do that?

4est
  • 3,010
  • 6
  • 41
  • 63

1 Answers1

1

You can explicitly list files to stash after --

git stash -- commands/abc/Test.cs common/des/Info.cs common/pit/Abc.cs

Then your changes in services/Services123.cs won't be stashed.

(Note that in your specific case you could conveniently take advantage of the file names and use a wildcard, like git stash -- com*)

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • what if I have all my files added to stage? And then I want to all files from stage put to stash? – 4est Jul 08 '20 at 11:25
  • @4est You can then use another flag, `--keep-index`, but warning : there's no `--stash-index-only` counterpart to do what you want. You'll have to 1) `git stash --keep-index` to save unstaged changes, then 2) do a regular `git stash` which will contain only your staged files, and finally recover in the working tree what you first stashed, with `git stash pop stash{1}`. At the end of this (slightly clunky) process, all your staged changes are stashed in `stash{0}`, and the other changes are in the working tree, waiting to be staged/discarded. – Romain Valeri Jul 08 '20 at 12:07
  • thx, so then I need to do: 1) git stash --keep-index 2) git stash push -m "my_new_stash" – 4est Jul 08 '20 at 12:13
  • Yes, if you prefer to refer to your (unstaged changes) stash by a name handle rather than by its stash number like I did. Both work. (If you're unsure, try it on a test repo, it's very quick to setup with just a couple of text files.) – Romain Valeri Jul 08 '20 at 12:15
  • last qq: instead of point `2) git stash push -m "my_new_stash"`....what else I can do ? – 4est Jul 08 '20 at 12:21
  • @4est What is unclear in my first comment? I already detailed steps 2) `git stash` and 3) `git stash pop stash@{1}`. Tell me what is problematic / confusing to you. OH WOW .... Important edit : in my previous comment I made a typo that I can't correct any more now : it's not `stash{n}` but `stash@{n}` in every instance. Sorry for the added confusion. – Romain Valeri Jul 08 '20 at 12:34