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


当前回答

有时,我在提交分支之前对其进行了不相关的更改,我想将其移动到另一个分支并单独提交(如master)。我这样做:

git stash
git checkout master
git stash pop
git add <files that you want to commit>
git commit -m 'Minor feature'
git stash
git checkout topic1
git stash pop
...<resume work>...

请注意,第一个stash和stash pop可以取消,您可以在结账时将所有更改转移到主分支,但前提是没有冲突。此外,如果您要为部分更改创建新分支,则需要隐藏。

假设没有冲突和新分支,您可以简化它:

git checkout master
git add <files that you want to commit>
git commit -m 'Minor feature'
git checkout topic1
...<resume work>...

甚至不需要Stash。。。

其他回答

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

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

我会使用git存储保存补丁。我不觉得交互很烦人,因为在交互过程中,可以选择将所需的操作应用于整个文件。

假设你有3个文件

a.rb
b.rb
c.rb

你只想藏b.rb和c.rb,而不想藏a.rb

你可以这样做

# commit the files temporarily you don't want to stash
git add a.rb
git commit -m "temp" 

# then stash the other files
git stash save "stash message"

# then undo the previous temp commit
git reset --soft HEAD^
git reset

你完蛋了!高温。

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

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

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