21

我正在寻找选择性地将文件的某些部分与 git 合并。

我有两个分支——称它们为 master 和 changed——它们是分开的一个提交;如果我将 master 合并到 changed 中,它只会快进。

但是,我只想合并多个部分——在一个文件中——从更改到主文件。

有没有一种干净的方法来做到这一点 - 例如在进行每次更改之前 git 询问?还是我坚持用手做?(不是世界末日,但我不想这样做,而且——是的——这本可以通过我的更多计划来避免——这就是生活。)

4

1 回答 1

16

尝试这个:

git merge --no-ff --no-commit changed    # Merge in 'changed' but don't commit
git reset <path/to/your/file>            # Reset the stage state but not 
                                         # the working version of the file
git add -p <path/to/your/file>           # Start adding in interactive mode

(Git will prompt you for each hunk; you can split hunks up if necessary.)

git commit                               # Finally commit the merge with only
                                         # the bits you chose to stage

但是请注意,这样做会使 Git 认为文件已完全合并,因此如果有人再次合并到同一个分支中,则该提交的其他更改仍然不会合并。这可能会也可能不会你想要什么。

如果您需要有关交互模式下各个选项含义的帮助git add,请参阅“交互模式git help add”部分的输出。

于 2013-10-22T16:39:01.243 回答