简体   繁体   English

git rebase-如何接受所​​有合并冲突的远程分支?

[英]Git rebase - how do I accept remote branch for all merge conflicts?

I'm working with my_branch_1 using git 1.7.1, and I'm trying to rebase from master. 我正在使用git 1.7.1使用my_branch_1,并且正在尝试从master恢复基准。 I have some changes of my own in directory_1. 我在directory_1中有一些自己的更改。

When I attempt: 当我尝试:

git rebase master

from my branch, I get large number of merge conflicts (over 200) from files in directory_2. 从我的分支机构中,我从directory_2中的文件中收到大量合并冲突(超过200个)。 How these conflicts got there, I have no idea. 这些冲突如何到达那里,我不知道。 I also have no way of knowing how the conflicts should be corrected since they are all in files I've never opened and don't know anything about. 我也没有办法知道应该如何纠正冲突,因为它们都在我从未打开过且一无所知的文件中。

Ideally, I'd love to be able to tell git: "hey git - if you hit a merge conflict in directory_2, just take the file from master, don't ask me about fixing it". 理想情况下,我希望能够告诉git:“嘿git-如果您在directory_2中遇到合并冲突,只需从master那里获取文件,不要问我有关修复的问题”。

At this point, I'd even be ok with just accepting master for ALL merge conflicts, as most of my work has been with new files. 在这一点上,我什至可以接受master来处理所有合并冲突,因为我的大部分工作都是使用新文件。 How do I automatically tell git to rebase and resolve all conflicts by using files from master? 如何通过使用master文件自动告诉git重新设置基准并解决所有冲突?

What you can do is do a git merge master . 您可以做的是git merge master This will generate the merge conflicts. 这将产生合并冲突。 Then you can use a little shell magic to do the rest with git diff --name-only --diff-filter=U . 然后,您可以使用一点外壳魔术来完成其余工作: git diff --name-only --diff-filter=U This command will list all the files with conflicts. 该命令将列出所有有冲突的文件。 So you can do something like 所以你可以做类似的事情

$ git merge master
$ git diff --name-only --diff-filter=U | grep ^path/to/directory_2 | xargs -I% sh -c 'git checkout -f HEAD -- % && git add %'

And then you may finish merging 然后您可以完成合并

Or you can do the other way 或者你可以用其他方式

$ git diff --name-only --diff-filter=U | grep ^path/to/directory_2 | xargs -I sh -c 'git checkout -f master -- % && git add %'

The first does ours on the directory directory_2 , and the second does theirs on the directory directory_2 . 第一次做ours的目录directory_2 ,第二个做theirs的目录directory_2

What this script does is get all the unresolved conflicts, filters with grep so that only the ones that are in directory_2 are in the list, and then uses xargs to run the command git checkout -f <branch: Either HEAD or master> -- <file> && git add <file> for each file in the list. 该脚本的作用是获取所有未解决的冲突,使用grep过滤,以便列表中仅包含directory_2中的冲突,然后使用xargs运行命令git checkout -f <branch: Either HEAD or master> -- <file> && git add <file>为列表中的每个文件git checkout -f <branch: Either HEAD or master> -- <file> && git add <file>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM