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


当前回答

对于VS代码的用户。Git侧边栏视图中Changes组的隐藏按钮将仅隐藏组中的文件。因此,如果您将一些文件移出该组,则可以隐藏其余文件。我所知道的在不恢复更改的情况下将一些文件移出的唯一方法是将它们暂存。因此:

暂存您不想隐藏的文件单击更改组标题中的隐藏按钮取消对您移开的文件的分级

其他回答

使用git存储推送,如下所示:

git stash push [--] [<pathspec>...]

例如:

git stash push -- my/file.sh

这是自2017年春季发布的Git 2.13以来提供的。

git stash push -p -m "my commit message"

-p让我们选择应该隐藏的大块;也可以选择整个文件。

系统将提示您对每个大块执行一些操作:

   y - stash this hunk
   n - do not stash this hunk
   q - quit; do not stash this hunk or any of the remaining ones
   a - stash this hunk and all later hunks in the file
   d - do not stash this hunk or any of the later hunks in the file
   g - select a hunk to go to
   / - search for a hunk matching the given regex
   j - leave this hunk undecided, see next undecided hunk
   J - leave this hunk undecided, see next hunk
   k - leave this hunk undecided, see previous undecided hunk
   K - leave this hunk undecided, see previous hunk
   s - split the current hunk into smaller hunks
   e - manually edit the current hunk
   ? - print help

由于在Git中创建分支很简单,您可以创建一个临时分支并将各个文件检入其中。

要隐藏单个文件,请使用git stash--patch[file]。

这将提示:Stash This hunk[y,n,q,a,d,j,j,g,/,e,?]?。只需键入一个(将此大块和所有后续大块存储在文件中),就可以了。

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

“藏起来”怎么样

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

这将弹出文件更改:

git apply /tmp/stash.patch

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