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


当前回答

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 diff /dir/to/file/file_to_stash > /tmp/stash.patch
git checkout -- /dir/to/file/file_to_stash

这将弹出文件更改:

git apply /tmp/stash.patch

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

您还可以选择只存储单个文件、文件集合或文件内的单个更改。如果您将-p选项(或--patch)传递给git stash,它将遍历工作副本中每个更改的“hunk”,并询问您是否希望隐藏它:

$ git stash -p

按以下键之一,它将运行该命令

Command   Description 
y         stash this hunk  
/         search for a hunk by regex .  
?         help .   
n         don't stash this hunk .    
q         quit (any hunks that have already been selected will be stashed) .   
s         split this hunk into smaller hunks    

我不知道如何在命令行上执行,只使用SourceTree。假设您已经更改了文件A,并且在文件B中有两个更改块。如果您只想将第二个块存储在文件B,而其他所有内容都保持不变,请执行以下操作:

舞台上的一切对工作副本执行更改,以撤消文件A中的所有更改(例如,启动外部diff工具并使文件匹配)使文件B看起来好像只应用了第二个更改。(例如,启动外部diff工具并撤消第一个更改。)使用“保留暂存更改”创建存储。取消标记所有内容完成!

如果您只想隐藏一些已更改的文件,只需在Stage中添加其他文件,然后执行git stash push--keep index

它将隐藏所有未暂存的更改文件

使用SourceTree,这可以通过3个步骤轻松完成。

暂时提交所有你不想隐藏的东西。Git添加所有其他内容,然后将其隐藏。通过运行git reset来弹出临时提交,在临时提交之前进行提交。

这一切都可以在SourceTree中在几秒钟内完成,您只需单击要添加的文件(甚至单个行)。添加后,只需将它们提交到临时提交。接下来,单击复选框以添加所有更改,然后单击“隐藏”以隐藏所有更改。在隐藏的更改不碍事的情况下,浏览一下提交列表,并在临时提交之前记录提交的哈希值,然后运行“git reset hash_b4_temp_commit”,这基本上就像通过将分支重置为提交之前的提交来“弹出”提交。现在,只剩下不想隐藏的内容。