简体   繁体   English

GIT合并冲突:创建新分支并删除一些文件后

[英]GIT merge conflict: after create new branch and deleting some files

I have a problem with a GIT merge.我有一个 GIT 合并问题。 In fact, I have a master branch with this architecture:事实上,我有一个采用这种架构的主分支:

.git
doc
   \__ some files

src
   \__ tools 1
              \__ some files

   \__ tools 2
              \__ some files

In my project, I developed tools and I will add other tools in the "src" directory.在我的项目中,我开发了工具,我将在“src”目录中添加其他工具。 For that, for each time I want to create a new tool, I will create a new branch named "tools3" from master branch and then remove all directories that I do not need, here "tools 1", "tools 2" and "doc".为此,每次我想创建一个新工具时,我都会从 master 分支创建一个名为“tools3”的新分支,然后删除我不需要的所有目录,这里是“工具 1”、“工具 2”和“博士”。 The reason is that I do not need documentation and other tools to develop.原因是我不需要文档和其他工具来开发。

So now, after those deleting I code my "tools 3" and I commit and push it into the new branch "tools3".所以现在,在删除这些之后,我编写了我的“工具 3”,然后提交并将其推送到新分支“tools3”中。 It's time to merge my two branches with a git merge tools3 -m "Merge tools3 branch with master branch" (in master branch).是时候用git merge tools3 -m "Merge tools3 branch with master branch" (在 master 分支中)合并我的两个分支了。

I have two problems, first some conflicts among with documentation files:我有两个问题,首先是与文档文件之间的一些冲突:

CONFLICT MESSAGE: CONFLICT (modify/delete): doc/api_project.json deleted in tools3 and modified in HEAD. Version HEAD of doc/api_project.json left in tree.冲突消息: CONFLICT (modify/delete): doc/api_project.json deleted in tools3 and modified in HEAD. Version HEAD of doc/api_project.json left in tree. CONFLICT (modify/delete): doc/api_project.json deleted in tools3 and modified in HEAD. Version HEAD of doc/api_project.json left in tree.

In the second time, the merging removes my "src" files (the ones I deleted earlier in "tools3" branch).第二次,合并删除了我的“src”文件(我之前在“tools3”分支中删除的文件)。

So, how can I fix this merge conflict and then how can I retrieve my removing files in master branch?那么,如何解决这个合并冲突,然后如何在 master 分支中检索我的删除文件? I think I do not have the best practice to do this type of things with GIT so I am open to any suggestions about GIT practices.我想我没有用 GIT 做这类事情的最佳实践,所以我愿意接受任何关于 GIT 实践的建议。

You mustn't delete the files from the repo you don't need.您不得从不需要的存储库中删除文件。 This is the reason you get the conflict(s).这就是您遇到冲突的原因。

While you've been working in your tools 3 dir, other contributors may have worked in them.当您在tools 3目录中工作时,其他贡献者可能已经在其中工作。 Eg fixing bugs, adding new features, etc. They've completed their work and pushed it to master .例如修复错误,添加新功能等。他们已经完成了他们的工作并将其推送给master

When its time for you to do push your work, history has diverged.当是时候让你推动你的工作时,历史已经发生了分歧。 Since you started your branch, on one hand you've deleted some files, and on the other these files have been changed by other people.自从您开始分支以来,一方面您删除了一些文件,另一方面这些文件已被其他人更改。 Git can't make the decision on what the correct route to take is, so it creates a conflict. Git 无法决定要采取的正确路线,因此会产生冲突。

You only really want to delete a file from a git repo if it is no longer useful for the entire project and no longer need it source controlled.如果一个文件对整个项目不再有用并且不再需要它的源代码控制,那么您真的只想从 git repo 中删除它。 To me it looks like you are only deleting them because you want to keep your directory structure "clean" or some other reason we can only speculate on.在我看来,您删除它们只是因为您想保持目录结构“干净”或其他一些我们只能推测的原因。

Every time you create new tool you are creating the branch by cloning your master branch.每次创建新工具时,您都是通过克隆主分支来创建分支。 once you added a new tool into the newly created branch, you are comparing the master and newly created branch in the background.一旦您将新工具添加到新创建的分支中,您就是在后台比较主分支和新创建的分支。 So whatever the new changes are there will be added in the master branch and what is the modified changes will be updated/removed from the master branch.因此,无论有什么新更改,都将添加到 master 分支中,并且将从 master 分支中更新/删除修改后的更改。 So you must not delete the code from any branch as it will be removed at the time of merging.所以你不能从任何分支中删除代码,因为它会在合并时被删除。

The conflict you're encountering is because another person has made a change in master to one of the files that you have deleted in tools3 .您遇到的冲突是因为另一个人在master对您在tools3删除的文件之一进行了更改。 As a result, when you're attempting to merge git needs to work out how to apply the changes that were made in master to a file that you deleted before the changes were made.因此,当您尝试合并 git 时,需要弄清楚如何将master中所做的更改应用于您在更改之前删除的文件。

Visual representation:视觉表现:

*   - Added doc/api_project.json
|\
| * - Deleted doc/api_project.json
| |
* | - [master] Updated doc/api_project.json
  |
  * - [tools3] Dev work on tools

As you can see above, to put those commits in to a single branch, git needs to know how to apply the changes made in the third commit to a file deleted in the second commit.正如您在上面看到的,要将这些提交放入单个分支,git 需要知道如何将在第三次提交中所做的更改应用于在第二次提交中删除的文件。

You shouldn't make any changes in a branch which do not apply to the feature that you're working on in that branch.您不应在分支中进行任何不适用于您在该分支中处理的功能的更改。 By deleting files, you've created a commit that deletes those files.通过删除文件,您创建了一个删除这些文件的提交。

Hopefully you performed the deletion as a separate commit (can we see git log output for your tools3 branch?) which would mean all you'd need to do is remove that commit from your branch before merging.希望您将删除作为单独的提交执行(我们可以看到您的tools3分支的git log输出吗?)这意味着您需要做的就是在合并之前从分支中删除该提交。

Removing the deletion operations will solve both your problems, you'll no longer have a conflict and the files that are being removed from the master branch (because you deleted them in tools3 ) will no longer be removed.删除删除操作将解决您的两个问题,您将不再有冲突,并且从master分支中删除的文件(因为您在tools3删除了它们)将不再被删除。

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

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