我该如何仅从特定目录的“他们”分支接受git merge冲突?

如何解决我该如何仅从特定目录的“他们”分支接受git merge冲突?

这是我的情况:git merge master导致50个文件发生合并冲突。我希望其中的45个文件可以从master中接受并完成,我想手动解决其余5个文件中的冲突。所有这些文件中的45个都位于目录{{1}中}。其他5个分散在其他地方。 如果,我只想接受所有50个冲突的管理员更改,就可以运行此命令:

some/dir

但是我不想要那样,就像我说的那样。我想要更像这样的东西:

git merge -X theirs master
# OR (same thing):
git merge --strategy-option theirs master

,以便它自动为git merge -X theirs master some/dir 中的所有冲突选择“他们的”(主人)一方,否则让我手动解决冲突。但是,这不存在。

所以,我的解决方法是:

简短说明:

开始合并,手动修复许多冲突文件中需要的几个文件。备份我刚刚修复的所有文件。中止合并。重做与some/dir的合并,以自动接受git merge -X theirs master对所有文件中的所有冲突所做的更改。将我的一些手动修复的文件手动复制回仓库,然后提交。

这避免了手动修复50个文件的麻烦,当只有5个文件真正需要我注意时,这可能非常繁琐且耗时。

完整的详细步骤:

  1. 开始常规合并:

    master
  2. 仅手动解决我要在其中处理的5个文件中的冲突,然后保存每个文件。然后,将它们的副本复制到回购协议之外的位置(或至少复制)在被仓库忽略的文件夹中)。我现在拥有手动解决的这5个文件的副本。这是完成此操作的一种方法:

     git merge master
    
  3. 执行 mkdir -p temp # create `temp` dir inside the repo. # Note: if you're not aware,the ".git/info/exclude" file in your repo is # an **untracked** form of a ".gitignore" file for the repo. We will use # this file below. # Add a `/temp/` entry to the ".git/info/exclude" file to keep # the repo from tracking the /temp/ dir,but withOUT using the # shared ".gitignore" file since this is my own personal setting # not everyone else on the team necessarily wants. echo -e "\n# don't track this temporary folder for my arbitrary use\n/temp/" \ >> .git/info/exclude # Now manually make copies of your 5 files you just resolved conflicts in: cp some/dir/file1.cpp temp cp some/dir/file2.cpp temp cp some/dir/file3.cpp temp cp some/dir/file4.cpp temp cp some/dir/file5.cpp temp 中止合并,然后执行git merge --abort重做合并,除了这次自动接受所有50个文件的所有主更改(即:针对所有冲突)。

  4. 现在,从上方将我手动解析的备份副本手动复制回各自文件顶部的存储库中。

    git merge -X theirs master
  5. 最后,执行 cp temp/file1.cpp some/dir cp temp/file2.cpp some/dir cp temp/file3.cpp some/dir cp temp/file4.cpp some/dir cp temp/file5.cpp some/dir git add -A来将它们修改为合并提交,瞧!我有45个文件的自动分辨率,和5个文件的手动分辨率。

有更好的方法吗?

我认为这种方法效果很好并且非常有效,但是我愿意学习替代方法,尤其是当它们更快或更容易时。

相关,但重复:

  1. Merging two branches,how do I accept one branch for all conflicts
  2. Simple tool to 'accept theirs' or 'accept mine' on a whole file using git

解决方法

解决冲突后,您可以直接从另一个分支检出文件。

因此,在完成git merge并解决您需要修复的文件的冲突之后。

执行git checkout <branch you're merging> -- some/dir这将仅通过那些更改将文件从另一个分支移出。我相信这也将使他们做好承诺。

如果ours分支中有更改,则可以仅在--之后列出文件,而不仅仅是检出整个目录。

,

在 6 个月前投票并标记正确的 this answer 之后,今天我意识到在一个非常大且非常拙劣的 merge 需要这个答案是错误的。假设您这样做是为了使用 feature_branch 的最新更改更新您的 master

git checkout feature_branch
git merge master
# conflicts result...

...而且有很多冲突。您会看到其中 25 个在 some/dir 内,并且您希望保留来自 feature_branch 的所有冲突更改,因此您执行以下 3 个命令中的任何一个(在本例中都是相同的):

git checkout -- some/dir
# OR
git checkout HEAD -- some/dir
# OR
git checkout feature_branch -- some/dir

好吧,你刚刚搞砸了! master 还在 some/dir 中添加了一些更改和新文件,其中许多与您的 feature_branch 更改零冲突,而所有这些都是您想要(这是将最新的 master 合并到您的 feature_branch 中的全部意义!)但现在它们全部消失,因为您只是用 {{1} 中的 some/dir 覆盖了它们}}。这根本不是我们想要的!

所以,这是正确答案:

feature_branch

再说一次,这与此不同:

# RIGHT ANSWER

# Keep only conflicting changes for all conflicts within files inside
# some/dir,from the "--ours" (`feature_branch` in this case) side.
git checkout --ours -- some/dir

所以,这是完整上下文中的正确答案:

# WRONG ANSWER

# Overwrite this entire directory 
git checkout feature_branch -- some/dir

完成!

有关详细信息和清晰度,请参阅我在此处的回答:Who is "us" and who is "them" according to Git?。这在那个答案的“警告警告”部分中有介绍,尽管这个答案是一个更全面的例子,更好地说明了问题。

,

我认为@Schleis的答案很好。如果您的回购壁球仍然合并,另一种选择是...甚至在开始合并之前,只需执行以下操作:

git checkout gabriel.staples/MyBranch
git checkout develop path/You/Want/To/Accept/Everything
git add path/You/Want/To/Accept/Everything
git commit -m "Partially merge develop at SHA XXXXX (squash-style)"
git merge develop
<resolve conflicts>
git add path/To/Your/Hand/Resolved/Files
git merge --continue

如果您确实需要,也可以使用以下方法使以上内容更接近真实的合并(将其设为父项):

git commit-tree aboveMergeSHA^{tree} -p `git merge-base develop pseudoMergeSHA` -p develop -m "Merge develop at SHA XXXXX"

但是到那时,我认为您最好也使用@Schleis的答案。我不喜欢在不需要时手动与父母打交道...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-