How to stash a specific file or multiple files?
For further actions, you may consider blocking this person and/or reporting abuse
How to stash a specific file or multiple files?
For further actions, you may consider blocking this person and/or reporting abuse
Saurabh Dhariwal -
Maxim McCain -
faxridd1n -
Harshal Ranjhani -
Top comments (11)
This didn't work:
git stash _file_
This did:
git stash push _file_ --keep-index
As Ben pointed out, you may use
git stash path/to/file path/to/other/file
to stash specific files. Another handy tip is that git provides very detailedman
pages for each sub-command! However, these pages are somewhat tricky to find if you don't know the secret: Useman git-<name-of-subcommand>
!So if you are struggling with making stash do what you want, you can run
man git-stash
in your terminal and bam all kinds of detailed information that makes my head spin.My favorite of the
git-stash
flags is-p -u
. Runninggit stash save -p -u
will step-by-step walk you through each and every changed/new file in your repo and ask "Do you want to stash this?"Cool, huh?
Yup. Cool 😎
What does
-p -u
flag does?the
-p
flag allows you to decide, change by change, what you want to stash. The-u
flag says "Also, stash untracked files"They do appear to be incompatible sometimes though.
git stash
- e.g.git stash .
orgit stash src/index.js
. Thengit stash apply
to apply the stashed changes orgit stash drop
to discard them.Is that what you're looking for?
git stash pop
is like apply + drop as well. I thinkgit stash --keep-index
keeps everything staged, and stashes unstaged changes. Useful if it's hard to type the pathspec for multiple files, or if you want to stash some changes to a file but not all.Big fan of
git stash pop
. Definitely a1 command > 2 commands
scenario.Whoa, TIL. Git's pretty cool, isn't it
git stash --keep-index
it is.Add the changes to the staging area which you do not want to stash. All changes to be stashed should be not staged but modified or untracked. Then you can just
git stash --keep-index
to stash changes. As mentioned in some other answer, add-u
to add untracked files also. Then you can justgit restore --staged <file names>
to bring staged changes back to modified.I suggest git stash save "a comment here". I always use it, it is the best way or working with stashes, the indexes may get you lost
Just to help people coming from google the save command has been deprecated:
This option is deprecated in favour of git stash push. It differs from "stash push" in that it cannot take pathspecs, and any non-option arguments form the message.