简体   繁体   English

合并两个具有不同文件夹名称的git分支

[英]Merge two git branch with different folder names

There are two branches in my project: R1 and OPE 我的项目中有两个分支:R1和OPE

Original folder structure of both the branches was: 两个分支的原始文件夹结构为:

dev-core/projectX/java-files

Now we started working on OPE branch and meanwhile root folder in R1 branch was renamed from dev-core to core. 现在我们开始在OPE分支上工作,同时R1分支中的根文件夹从dev-core重命名为core。 Now folder structure of R1 branch is: 现在,R1分支的文件夹结构为:

core/projectX/java-files

and folder structure of OPE branch is: OPE分支的文件夹结构为:

dev-core/projectX/java-files

I have to merge changes of R1 branch in my OPE branch. 我必须在OPE分支中合并R1分支的更改。 Now if I do a simple git merge R1 , git is creating a dev-core folder in R1 branch and duplicating all the files in it. 现在,如果我执行一个简单的git merge R1 ,git会在R1分支中创建一个dev-core文件夹并复制其中的所有文件。

I tried to rename folders in OPE by using git mv and then tried doing git merge R1 , it is giving me following message for almost all files: 我尝试使用git mv重命名OPE中的文件夹,然后尝试执行git merge R1 ,这给了我几乎所有文件的以下消息:

CONFLICT (rename/delete): core/projectX/path/to/java/file deleted in R1
  and renamed in HEAD. Version HEAD of core/projectX/path/to/java/file
  left in tree.

Now when I am checking the file and looking for conflicts (by searching for symbol <<< ) I am not able find any conflicts. 现在,当我检查文件并查找冲突时(通过搜索符号<<< ),我找不到任何冲突。 Can anyone please explain me what is happening here and how can I fix this? 谁能解释一下这是怎么回事,我该如何解决?

Note: I used --no-commit option while merging. 注意:我在合并时使用了--no-commit选项。

More Info 更多信息

This is how I did the merge: 这是我进行合并的方式:

git checkout -b OPE origin/OPE
git checkout -b R1 origin/R1
git checkout OPE
git mv dev-core core
git merge R1

Here is my understanding of whats happening: Lets say if there is a file dev-core/projectX/file1 . 这是我对所发生情况的理解:让我们说一下是否有一个文件dev-core/projectX/file1 I made some changes to this file in my OPE branch and committed it. 我在OPE branch对此文件进行了一些更改并提交。 Now somebody else made some change in core/projectX/file1 in R1 branch . 现在,其他人在R1 branch core/projectX/file1中进行了一些更改。 Now when I'll merge there are two possibilities: 现在,当我合并时,有两种可能性:

  1. Either git will merge both versions of file1 and will show me the conflict using <<<<< git将合并file1的两个版本,并使用<<<<<

  2. OR git will sense that file1 is renamed from dev-core/projectX/file1 to core/projectX/file1 and will let me keep my file and will discard the changes from R1 branch file If I am correct then 2nd scenarios is my case and this might cause me loss of imp code Plz correct me if I'm wrong. 或git会感觉到file1已从dev-core / projectX / file1重命名为core / projectX / file1,并让我保留我的文件,并放弃了R1分支文件中的更改。如果我是正确的,那么第二种情况就是我的情况,这如果我错了,可能会导致我丢失imp代码Plz纠正我。

I tested renaming folders in git and my understanding was wrong. 我在git中测试了重命名文件夹,但我的理解是错误的。 :( git detects folder renaming and will merge smartly. Now I have to figure out problem in my case. :( git会检测到文件夹的重命名,并会智能地合并。现在,我必须弄清楚我的情况下的问题。

Ahaaa I found my mistake, git is smart and I tried to be oversmart ;) All I had to do was git merge me doing git mv dev-core core added hell lot of complexity and caused lot of trouble :P Ahaaa我发现了我的错误,git很聪明,我试图过分聪明;)我所要做的就是git merge me做git mv dev-core core增加了很多麻烦,并带来很多麻烦:P

You're not going to see files with conflict markers, the conflict says 冲突说,您不会看到带有冲突标记的文件

CONFLICT (rename/delete): core/projectX/path/to/java/file deleted in R1
    and renamed in HEAD.

which means there's a conflict because the file was renamed in your current branch, while it was deleted altogether in R1. 这意味着存在冲突,因为该文件在您当前的分支中被重命名,而在R1中被完全删除了。

You have to tell git how to resolve that conflict (ie do you want to rename the file, or delete it?). 您必须告诉git如何解决该冲突(即,您要重命名文件还是删除文件?)。 Git left behind the renamed version of the file in your working copy: Git在工作副本中留下了文件的重命名版本:

Version HEAD of core/projectX/path/to/java/file left in tree. 树中剩余的core/projectX/path/to/java/file版本HEAD。

If you do git status , you'll see something like this: 如果执行git status ,则会看到类似以下内容:

$ git status
On branch test
Your branch and 'origin/test' have diverged,
and have 1 and 1 different commit each, respectively.
  (use "git pull" to merge the remote branch into yours)

You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        added by them:      core/projectX/path/to/java/file

You can either choose to keep the renamed version of the file by doing git add <file> , or you can remove it by using git rm <file> . 您可以选择通过执行git add <file>保留文件的重命名版本,也可以使用git rm <file>删除git rm <file> Then commit the changes. 然后提交更改。 That's how you resolve the merge conflict in this case. 在这种情况下,这就是解决合并冲突的方法。

The solution to my problem is don't rename folder using git mv dev-core core just merge. 解决我的问题的方法是不要使用git mv dev-core core重命名文件夹,而只是合并。 Git will take care of everything. Git会照顾好一切。

In case you see two folder dev-core and core in your workspace that means git was not able to merge some files and they are kept at old location only. 如果您在工作区中看到两个文件夹dev-corecore ,这意味着git无法合并某些文件,并且它们仅保留在旧位置。 Just compare both files dev-core/projectX/file1 & core/projectX/file1 using some comparing tool and fix the problem. 只需使用一些比较工具比较两个文件dev-core/projectX/file1core/projectX/file1解决问题。 Delete old file and you are done :) 删除旧文件,您就可以完成:)

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

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