Stash only one file out of multiple files that hav

2019-01-03 00:18发布

How can I stash only one of multiple changed files on my branch?

标签: git git-stash
27条回答
Melony?
2楼-- · 2019-01-03 01:15

I've reviewed answers and comments for this and a number of similar threads. Be aware that none of the following commands are correct for the purpose of being able to stash any specific tracked/untracked files:

  • git stash -p (--patch): select hunks manually, excluding untracked files
  • git stash -k (--keep-index): stash all tracked/untracked files and keep them in the working directory
  • git stash -u (--include-untracked): stash all tracked/untracked files
  • git stash -p (--patch) -u (--include-untracked): invalid command

Currently, the most reasonable method to be able to stash any specific tracked/untracked files is to:

  • Temporarily commit the files you don't want to stash
  • Add and stash
  • Pop the temporary commit

I wrote a simple script for this procedure in an answer to another question, and there are steps for performing the procedure in SourceTree here.

查看更多
虎瘦雄心在
3楼-- · 2019-01-03 01:15

When you try to switch between two branches, this situation occurs.

Try to add the files using "git add filepath".

Later execute this line

git stash --keep-index

查看更多
来,给爷笑一个
4楼-- · 2019-01-03 01:16
git add .                           //stage all the files
git reset <pathToFileWillBeStashed> //unstage file which will be stashed
git stash                           //stash the file(s)
git reset .                         // unstage all staged files
git stash pop                       // unstash file(s)
查看更多
登录 后发表回答