如何在我的分支中只存储多个已更改文件中的一个?


当前回答

这里的每个答案都很复杂。。。

“藏起来”怎么样

git diff /dir/to/file/file_to_stash > /tmp/stash.patch
git checkout -- /dir/to/file/file_to_stash

这将弹出文件更改:

git apply /tmp/stash.patch

与隐藏一个文件并将其弹出完全相同的行为。

其他回答

当git stash-p(或git add-p with stash--keep index)太麻烦时,我发现使用diff、checkout和apply更容易:

仅“隐藏”特定文件/dir:

git diff path/to/dir > stashed.diff
git checkout path/to/dir

然后

git apply stashed.diff

这里的每个答案都很复杂。。。

“藏起来”怎么样

git diff /dir/to/file/file_to_stash > /tmp/stash.patch
git checkout -- /dir/to/file/file_to_stash

这将弹出文件更改:

git apply /tmp/stash.patch

与隐藏一个文件并将其弹出完全相同的行为。

一个复杂的方法是首先提交所有内容:

git add -u
git commit // creates commit with sha-1 A

重置回原始提交,但从新提交中签出_one_file:

git reset --hard HEAD^
git checkout A path/to/the_one_file

现在,您可以存储_one_file:

git stash

通过将提交的内容保存在文件系统中并重置为原始提交来进行清理:

git reset --hard A
git reset --soft HEAD^

是的,有点尴尬。。。

VonC将文件复制到Git存储库外部的“中间”解决方案的问题是,您会丢失路径信息,这使得稍后将一堆文件复制回来有些麻烦。

A发现使用tar(类似的工具可能会这样做)而不是复制更容易:

tar cvf/tmp/stash.tar路径/to/some/file路径/to/some/other/file(…等)git签出路径/to/some/file-path/to/some/other/file暂存tar xvf/tmp/stash.tar等等(见VonC的“中间”建议)

我没有找到我需要的答案,这很简单:

git add -A
git reset HEAD fileThatYouWantToStash
git commit -m "committing all but one file"
git stash

这只存储了一个文件。