简体   繁体   English

Git 在两个文件夹之间合并,而不是分支

[英]Git merge between two folders, not branches

Suppose the following scenario:假设以下场景:

I have a git project that has a dir named project .我有一个 git 项目,它有一个名为project的目录。 Inside this dir I made some commits.在这个目录中,我做了一些承诺。 Then, with cp -r I duplicated this dir to a dir called project-with-feature-b , ie I manually created a branch.然后,使用cp -r我将这个目录复制到一个名为project-with-feature-b的目录,即我手动创建了一个分支。

Now I want to merge these two folders like if they were two branches, is there any way to do that using git?现在我想合并这两个文件夹,就像它们是两个分支一样,有没有办法使用 git 来做到这一点?

To be more specific to my problem.更具体地说明我的问题。 I have a svn repository which I am using with git-svn tool to clone it.我有一个 svn 存储库,我正在使用 git-svn 工具来克隆它。 And I cant use the -T/-b/-t options but I want to make a merge.我不能使用 -T/-b/-t 选项,但我想进行合并。

This questions is very old, I joined recently.这个问题很老,我最近加入了。

I am using ubuntu, and use meld to solve merge issues.我用的是ubuntu,用meld解决合并问题。

So If I have 2 folders of the same app, which might have minor changes.所以如果我有同一个应用程序的 2 个文件夹,它们可能会有微小的变化。

I will do this in the terminal我将在终端中执行此操作

meld ./app_branch_master ./app_branch_dev

This will open up both the folder, Meld will take time to compare both the folders.这将打开两个文件夹,Meld 将花时间比较两个文件夹。 It will show you what's there on left hand side and what's on the right hand side.它会告诉你左边有什么,右边有什么。 You can also move little code from there.您也可以从那里移动少量代码。

That's how I do it.我就是这样做的。

Yes, you can technically accomplish this, but only because it is not too late to fix your mistake.是的,你可以在技术上做到这一点,但这只是因为现在纠正你的错误还为时不晚。 You need to go back in time and do this the correct way:您需要及时返回 go 并以正确的方式执行此操作:

  1. Move your project-with-feature-b folder outside your git repo:project-with-feature-b文件夹移到 git 存储库之外:

     $ mv project-with-feature-b..
  2. Go back through git log and find the commit ID of the last commit before you created the project-with-feature-b directory. Go 通过git log返回并找到在创建project-with-feature-b目录之前最后一次提交的提交 ID。 If you haven't made any commits "inside" the project-with-feature-b directory, you can skip this step.如果您还没有在project-with-feature-b目录“内部”进行任何提交,则可以跳过此步骤。

  3. Create a new branch called feature-b based on that commit ID.基于该提交 ID 创建一个名为feature-b的新分支。 If you skipped step 2, omit <commit-id-from-step-2>如果您跳过了第 2 步,请省略<commit-id-from-step-2>

     $ git branch feature-b <commit-id-from-step-2> $ git checkout feature-b
  4. In the feature-b branch, replace the project folder with your backup of project-with-feauture-bfeature-b分支中,将project文件夹替换为project-with-feauture-b的备份

    $ rm -rf project $ mv../project-with-feature-b project $ git add project $ git commit -m "Add feature-b"
  5. Go back to your master branch and merge your feature-b branch into master Go 回到你的 master 分支并将你的feature-b分支合并到 master

     $ git merge --no-ff feature-b

In the future, you should use branches as above before you start doing work.以后在开始工作之前,应该像上面那样使用分支。 Learn to use the tools correctly rather than how you think they should work.学会正确使用这些工具,而不是你认为它们应该如何工作。

Take a look at Detach subdirectory into separate Git repository , once you split the history of both directories you'll be able to branch, merge or do whatever you want.查看Detach subdirectory into separate Git repository ,一旦你拆分了两个目录的历史,你就可以分支、合并或做任何你想做的事。

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

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