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

std*_*all 42 git git-stash

我想隐藏更改当前文件夹及其子文件夹中的更改.

我怎样才能做到这一点?

我尝试了一种显而易见的方法 - git stash .但似乎没有用.

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

gor*_*ate 60

git stash push -- path/to/folder
Run Code Online (Sandbox Code Playgroud)

对我来说是诀窍.

  • 因此,要回答OP的问题,`git stash push-.`应该可以解决问题;) (5认同)
  • 旧版本的 git(例如 2.11)有 `save` 而不是 `push`,并且会存储所有文件,而不仅仅是指定的目录:( (4认同)

Mar*_*nde 12

git stash 不会让你用一个命令保存部分目录,但有一些替代方案.

您可以使用git stash -p仅选择要隐藏的差异.

如果输出git stash -p很大并且/或者您需要可编写脚本的解决方案,并且可以创建临时提交,则可以使用子目录中的所有更改创建提交,然后隐藏更改,并回退提交.在代码中:

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~
Run Code Online (Sandbox Code Playgroud)


mvp*_*mvp 7

这应该适合你:

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
Run Code Online (Sandbox Code Playgroud)

基本上,它将所有已更改的文件添加到索引,但要隐藏的文件夹(或文件)除外.然后用-k(--keep-index)隐藏它们.最后,将索引重置回您开始的位置.

  • 这实际上会做一些不同的事情:`git stash -k`将使索引保持原样,因此索引中的更改不会被删除,但新的存储将******来自索引的变化**和**那些不在其中的人,所以在任何情况下它都将是一个"完整"藏匿. (4认同)