git divergent重命名 [英] git divergent renaming

查看:195
本文介绍了git divergent重命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  • 从master创建分支task001

  • master:修改foo.c并将其重命名为bar.c

  • task001:修改foo.c并将其重命名为moo.c

  • 合并task001到master



Git告诉我的是:

  CONFLICT(重命名/重命名):在分支HEAD中重命名foo.c - >bar.c重命名foo.c - >moo。 c在task001中
自动合并失败;修复冲突,然后提交结果。

我该如何解决?我的意思是,一旦名称冲突得到解决,我仍然想合并这两个文件。



谢谢。

解决方案

我最初理解你的问题意味着与你在评论中澄清的东西不同,但最初的答案可能是有用的,所以我将它作为第一部分,但添加一个替代如下:

1。只是解决冲突,所以你只有分歧的moo.c和bar.c



在这里,我假设你想要的结果是有 bar.c moo.c ,以及其合并版本中每个分支的更改。我会通过解决索引和提交中的冲突来解决这个问题。您描述的情况如下所示:

  $ git log --pretty = oneline --graph --decorate master task001 
* fce127471ab5d1ef55b1d16412a75a7129782517(task001)在task001上重命名为不同的文件
* 8edbc1357a3c0484dc077f6f7ce43de91d21e794 task001分支上的小改动
| * d10e9020d147a4bbd3688744823380322bf37718(HEAD,master)在master上重命名为bar.c
| * f952617645456a551df07f219bfdc95e00c8ac9b在主
| /
* e8602eef46af744defd4fb4073ecdb6a7afbfd28中添加了一个小改动foo.c的初始版本

如果我然后将 task001 合并到 master 中,它会产生与您看到的相同的错误: p>

  $ git merge task001 
CONFLICT(重命名/重命名):重命名foo.c - >bar.c 在分支HEAD中重命名foo.c - >moo.c在task001中
自动合并失败;修复冲突,然后提交结果。

然后 git status 会显示一个摘要

  $ git status 
#分支大师
#未合并路径:
#(使用git add / rm< file> ...来标记分辨率)

#由我们添加:bar.c
#两者都删除:foo.c
#添加它们:moo.c

没有更改添加到提交(使用git add和/或git commit -a)

现在您只需要使用 git add git rm 来解决冲突:

  $ git add bar.c.c 
$ git rm --cached foo.c
foo.c:需要合并
moo.c:需要合并
rm'foo.c'
$ git add moo.c
$ git status
#在分支主机上
#要提交的更改:

#新文件:moo.c

(我使用 - cached 来移除 foo。 c 因为它不再在工作警察中y和 - cached 表示该命令只查看索引 - 否则会得到有关 foo.c 不存在。)



然后您只需提交结果:

  $ git commit 
[保存prepopulated提交信息]

你已经解决了冲突并且在合并版本中具有 bar.c moo.c



(我注意到,在这里描述的简单示例情况下,我认为 git merge -s resolve 适用于我,但我希望这更通用。 )

2。将bar.c和moo.c合并成一个文件



从下面的评论看来,您真正想要将这些文件重新合并为一个文件两个分支中文件的更改合并。如果你希望git的合并策略能够帮助你,合并前每个分支上的文件必须具有相同的路径,所以你需要在合并之前至少重命名其中的一个。让我们假设你想把合并文件称为 quux.c ,尽管你也可以使它成为 foo.c 当然了。然后,您可以执行以下步骤,在合并前重命名每个分支上的文件:

 #重命名master上的文件:

$ git checkout master
已经在'master'上
$ git mv bar.c quux.c
$ git commit -m将bar.c重命名为quux。 c在分支主机上
[master f9b3f49]将bar.c重命名为分支主机上的quux.c
1个文件已更改,0个插入(+),0个删除( - )
重命名栏。 c => quux.c(100%)

#在task001上重命名文件:

$ git checkout task001
转换到分支'task001'
$ git mv moo.c quux.c
$ git commit -m将分支task001上的moo.c重命名为quux.c
[task001 c71ad89]将moo.c重命名为quux.c以及分支task001
1个文件已更改,0个插入(+),0个删除( - )
重命名moo.c => quux.c(100%)

#切换回master,并在task001中合并:

$ git checkout master
转换到分支'master'
$ git merge task001

如果更改没有冲突,那么此时合并应该只是工作。在我试过的例子中他们确实有冲突,所以我需要编辑文件 git add 文件并提交结果:

 重命名foo.c-> quux.c 
自动合并quux.c
CONFLICT(内容):合并quux.c中的冲突
自动合并失败;修复冲突,然后提交结果。
$ emacs -nw quux.c
$ git add quux.c
$ git commit
[master 8baa7cb]合并分支'task001'
$ ls
quux.c


I'd like to know how you handle a situation like this in Git:

  • create branch task001 from master
  • master: modify foo.c and rename it to bar.c
  • task001: modify foo.c and rename it to moo.c
  • merge task001 to master

What Git tells me is:

CONFLICT (rename/rename): Rename "foo.c"->"bar.c" in branch "HEAD" rename "foo.c"->"moo.c" in "task001"
Automatic merge failed; fix conflicts and then commit the result.

How should I solve it? I mean, I still want to merge the two files once the name conflict is resolved.

Thanks.

解决方案

I initially understood your question to mean something different from what you have clarified in the comments, but the original answer may be useful, so I'm leaving that as the first part, but adding an alternative below:

1. Just resolving the conflict, so you only have the diverged moo.c and bar.c

Here I'm assuming that the result you want is to have bar.c and moo.c, with their changes from each branch, in the merged version. I would deal with this by resolving the conflict in the index and committing. The situation you describe looks like this:

$ git log --pretty=oneline --graph --decorate master task001
* fce127471ab5d1ef55b1d16412a75a7129782517 (task001) Rename to a different file on task001
* 8edbc1357a3c0484dc077f6f7ce43de91d21e794 A small change on the task001 branch
| * d10e9020d147a4bbd3688744823380322bf37718 (HEAD, master) Rename on master to bar.c
| * f952617645456a551df07f219bfdc95e00c8ac9b Added a small change in master
|/  
* e8602eef46af744defd4fb4073ecdb6a7afbfd28 Initial version of foo.c

If I then merge task001 into master, it produces the same error that you see:

$ git merge task001 
CONFLICT (rename/rename): Rename "foo.c"->"bar.c" in branch "HEAD" rename "foo.c"->"moo.c" in "task001"
Automatic merge failed; fix conflicts and then commit the result.

Then git status will show you a summary of problems:

$ git status
# On branch master
# Unmerged paths:
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#   added by us:        bar.c
#   both deleted:       foo.c
#   added by them:      moo.c
#
no changes added to commit (use "git add" and/or "git commit -a")

Now you just need to use git add and git rm to resolve the conflicts:

$ git add bar.c
$ git rm --cached foo.c
foo.c: needs merge
moo.c: needs merge
rm 'foo.c'
$ git add moo.c
$ git status
# On branch master
# Changes to be committed:
#
#   new file:   moo.c
#

(I used --cached for removing foo.c because it's no longer in the working copy, and --cached means the command only looks at the index -- otherwise you get an error about foo.c not existing.)

Then you just commit the results:

$ git commit
[ save the prepopulated commit message ]

And you've resolved the conflicts and have bar.c and moo.c in the merged version.

(I note in passing that git merge -s resolve works for me in the simple example situation describe here, but I hope this is more generally useful.)

2. Merging bar.c and moo.c back into one file

It seems from your comments below that really what you would like is to merge these files back into one again, with the changes to the file in both branches merged. If you want git's merge strategies to help you with this, the files on each branch must have the same path before merging, so you'll need to rename at least one of them before merging. Let's suppose you want the merged file to be called quux.c, although you could just as well make it foo.c again, of course. Then you could do the following steps, which rename the file on each branch before merging:

# Rename the file on master:

$ git checkout master
Already on 'master'
$ git mv bar.c quux.c
$ git commit -m "Rename bar.c to quux.c on branch master"
[master f9b3f49] Rename bar.c to quux.c on branch master
 1 files changed, 0 insertions(+), 0 deletions(-)
 rename bar.c => quux.c (100%)

# Rename the file on task001:

$ git checkout task001 
Switched to branch 'task001'
$ git mv moo.c quux.c
$ git commit -m "Rename moo.c to quux.c as well on branch task001"
[task001 c71ad89] Rename moo.c to quux.c as well on branch task001
 1 files changed, 0 insertions(+), 0 deletions(-)
 rename moo.c => quux.c (100%)

# Switch back to master, and merge in task001:

$ git checkout master
Switched to branch 'master'
$ git merge task001

If the changes didn't conflict, then at this point that merge should Just Work. In the example I tried they did conflict, so I needed to edit the file, git add the file and commit the result:

Renaming foo.c->quux.c
Auto-merging quux.c
CONFLICT (content): merge conflict in quux.c
Automatic merge failed; fix conflicts and then commit the result.
$ emacs -nw quux.c 
$ git add quux.c
$ git commit
[master 8baa7cb] Merge branch 'task001'
$ ls
quux.c

这篇关于git divergent重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆