如何将更改存储在当前文件夹中?

我希望只将更改存储在当前文件夹及其子文件夹中。

我怎么才能做到呢?

我已经尝试了显而易见的方法-git stash .,但它似乎不工作。

我知道我可以创建临时提交,然后删除它们,但是我想知道 git stash是否支持存储特定的文件夹。

55173 次浏览

git stash will not let you save partial directories with a single command, but there are some alternatives.

You can use git stash -p to select only the diffs that you want to stash.

If the output of git stash -p is huge and/or you want a scriptable solution, and it is acceptable to create temporary commits, you can create a commit with all the changes but those in the subdirectory, then stash away the changes, and rewind the commit. In code:

git add -u :/   # equivalent to (cd reporoot && git add -u) without changing $PWD
git reset HEAD .
git commit -m "tmp"
git stash       # this will stash only the files in the current dir
git reset HEAD~

This should work for you:

cd <repo_root>
git add .         # add all changed files to index
cd my_folder
git reset .       # except for ones you want to stash
git stash -k      # stash only files not in index
git reset         # remove all changed files from index

Basically, it adds all changed files to index, except for folder (or files) you want to stash. Then you stash them using -k (--keep-index). And finally, you reset index back to where you started.

You could checkout one of the GUI git interfaces like SourceTree or TortoiseGit, things like this are what I personally go to tortoise for as it just ends up being much faster than trying to do many commandline commands.

git stash push -- path/to/folder

Does the trick for me.