刚刚开始了解 git,目前正在考虑合并。假设我有一个 master 分支,我创建了一个名为 child of this 的分支。我在子分支上工作并更改了一些文件。现在我想将此子分支合并到主分支中:
git merge child
Run Code Online (Sandbox Code Playgroud)
但是,子分支中有一些文件(也在主分支中)我不想合并。如何将我的更改从子项合并到主项但从子项中排除特定文件?
简短的回答很简单:不。
长答案要复杂得多,但也等于“不”。本质上,在某些情况下,您可以让 Git 相信如果合并的双方都有更改,则更改总是会发生冲突。请参阅相关问题的答案(尽管我真正要说的是您可以将文件标记为二进制文件,或设置特殊的合并驱动程序,但即使要达到这一点,您也需要了解很多背景知识)。
如果你想避免某些文件,你可以做的是运行你git merge的--no-commit:
$ git status
On branch master
nothing to commit, working directory clean
$ git checkout -b sidebranch
... lots of coding, git committing, etc
$ git checkout master
... more coding and committing, etc.,
or various merges of other branches,
so that master and sidebranch are
actually divergent (if they are
not, note that merge would
normally do a "fast forward" here)
$ git merge --no-commit sidebranch
Run Code Online (Sandbox Code Playgroud)
此时,Git 将使用我在链接答案中描述的过程进行合并(通过做两个差异,然后组合两个差异)。但它不会自动提交结果合并,即使它成功了。
这意味着您现在可以这样做:
$ git rm path/to/ickyfile
# we don't want this file, it's icky!
Run Code Online (Sandbox Code Playgroud)
和/或这个:
$ git checkout --ours -- path/to/foo; git add path/to/foo
# we want the current (master) version of this file,
# ignoring all the changes made in the side branch
Run Code Online (Sandbox Code Playgroud)
甚至像这样的东西(但不要:结果有时被称为“邪恶合并”):
$ vim path/to/bar
<make changes that come from neither master nor sidebranch>
$ git add path/to/bar
Run Code Online (Sandbox Code Playgroud)
一旦你完成了让不好的事情发生在一个好的合并中 :-) 你就可以运行git commit来完成合并。
一般来说,如果您发现需要这样做,您可能使用了错误的工具。 特别是,人们经常用配置文件来做上述事情,当“正确”的事情不是将这些文件放在 Git(或任何版本控制系统)中时。
(相反,在 VCS 中放置一个示例配置文件。而不是prog.conf添加和提交prog.conf.example。放入prog.conf您的.gitignore. 您的安装/初始化代码可以根据需要将示例配置复制到位,以便新用户设置。)
| 归档时间: |
|
| 查看次数: |
10391 次 |
| 最近记录: |