使用git将两个目录与​​公共父级(但不同的SHA1)合并 [英] Using git to merge two directories with common parent (but different SHA1)

查看:284
本文介绍了使用git将两个目录与​​公共父级(但不同的SHA1)合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的项目中,有人拿了一份代码。他们在他们的目录中创建了一个新的git存储库,并继续他们的工作。同时,我们主要项目库的工作也在继续。

  A --- B --- C --- D我们现在只剩下两个没有共同SHA1的git仓库了。 
|
|代码复制到新目录
|
E --- F --- G



B和E是完全相同的文件,文件夹等等。



我们的目标是使用Git将变化F和G合并到主分支中(当前位于D处)首先,我要重新绘制你的图。



<$ p $

解决方案

p>
A --- B --- C --- D主人

E --- F --- G副本

让我们先把这两个都放在同一个回购中。这里没有特别的步骤,你只需把 git fetch 都放到同一个仓库中。

然后你创建一个接枝。编辑 .git / info / grafts ...

 < E -sha1> < B-SHA1> 

然后执行 git filter-branch 它是永久的。

  git filter-branch --tag-name-filter cat  -  --all 
code>

这将创建一个新的历史记录,即正确历史记录:

 
A --- B --- C --- D主人
\
E --- F --- G副本


$ p

之后,您可以合并。



演示实际操作



我创建了两个与图相匹配的存储库,从提交B复制到提交E。




  • E有SHA-1 B $ SHA $ 1 3aa6b69127d20ac42746c39be3c273a9d80fb706
  • $ b


 
$ git init
$ git remote add master ../gittest
$ git remote add copy ../ gittest2
$ git fetch master
remote:计数对象:12,完成。
remote:压缩对象:100%(7/7),完成。
remote:合计12(delta 2),重用0(delta 0)
开箱对象:100%(12/12),完成。
从../gittest
* [新分支] master - > master / master
$ git checkout -b master master / master
Branch master设置为从主站跟踪远程分支主站。
已经在'master'上
$ git fetch copy
警告:没有通用提交
remote:计数对象:9,完成。
remote:压缩对象:100%(6/6),完成。
remote:合计9(delta 0),重用0(delta 0)
开箱对象:100%(9/9),完成。
从../gittest2
* [new branch] master - > copy / master
$ git log --format = oneline copy / master | tail -n 1
3aa6b69127d20ac42746c39be3c273a9d80fb706 E
$ git log --format = oneline master / master
1532332fe705931babe9db04c8d84051867b52c9 D
90903c0ac8dc26649b875ee00ea39bfd7571b1fb C
95b0fc836dbea7eaa0d043390df841d184af7cd5乙
e9de90c82a32041cff3a19f8e40d4358bc4ec2ca提交一个
$的 git的日志--format = ONELINE主/主
bec0e7af9374e4c57cb87f3fd5b2fb2b6a3f431bģ
a61e01d096e81c36bcc450afd98ca94cff909622˚F
3aa6b69127d20ac42746c39be3c273a9d80fb706 E



如果现在合并,会发生什么情况?



 
$ git merge copy / master
自动合并test.txt
CONFLICT(添加/添加):在test.txt中合并冲突
自动合并失败;修复冲突,然后提交结果。



嫁接在行动中



 
$ echo 3aa6b69127d20ac42746c39be3c273a9d80fb706 \
95b0fc836dbea7eaa0d043390df841d184af7cd5> .git / info / grafts

$ git filter-branch --tag-name-filter cat - --all
重写bec0e7af9374e4c57cb87f3fd5b2fb2b6a3f431b(7/7)
警告:Ref'refs / heads / master'不变
Ref'refs / remotes / copy / master'被重写
警告:Ref'refs / remotes / master / master'不变
$ git merge copy / master
自动合并test.txt
由'递归'策略合并。
test.txt | 5 +++++
1个文件已更改,5个插入(+)



最终结果



 
$ git log --graph --oneline | cat
* 4dadb0a合并远程追踪分支'copy / master'
| \
| * bb2fc85 G
| * be88bc5 F
| * 0816baf E
* | 1532332 D
* | 90903c0 C
| /
* 95b0fc8 B
* e9de90c提交A


In our project somebody took a copy of the code. They started a new git repository in their directory and continued their work there. Meanwhile the work on our main project repository also continued. We are now left with two git repositories with no common SHA1.

A --- B --- C --- D
      |
      | Copy of code to new directory
      |
      E --- F --- G

B and E are the exact same files, folders, etc.

Our goal is to use Git to merge changes F and G into the main branch (currently at D)

解决方案

First, I'm going to redraw your diagram.

A --- B --- C --- D  master

         E --- F --- G  copy

Let's first put both of these in the same repo. There are no special steps to this, you just git fetch both into the same repository.

Then you create a graft. Edit .git/info/grafts...

<E-sha1> <B-sha1>

Then run git filter-branch to make it permanent.

git filter-branch --tag-name-filter cat -- --all

This will create a new history, the "correct" history:

A --- B --- C --- D  master
        \
         E --- F --- G  copy

After that, you can just merge.

Demo in action

I created two repositories which matched the diagram, copying from commit B to make commit E.

  • E has SHA-1 3aa6b69127d20ac42746c39be3c273a9d80fb706
  • B has SHA-1 95b0fc836dbea7eaa0d043390df841d184af7cd5

$ git init
$ git remote add master ../gittest
$ git remote add copy ../gittest2
$ git fetch master
remote: Counting objects: 12, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 12 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (12/12), done.
From ../gittest
 * [new branch]      master     -> master/master
$ git checkout -b master master/master
Branch master set up to track remote branch master from master.
Already on 'master'
$ git fetch copy
warning: no common commits
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 9 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (9/9), done.
From ../gittest2
 * [new branch]      master     -> copy/master
$ git log --format=oneline copy/master | tail -n 1
3aa6b69127d20ac42746c39be3c273a9d80fb706 E
$ git log --format=oneline master/master
1532332fe705931babe9db04c8d84051867b52c9 D
90903c0ac8dc26649b875ee00ea39bfd7571b1fb C
95b0fc836dbea7eaa0d043390df841d184af7cd5 B
e9de90c82a32041cff3a19f8e40d4358bc4ec2ca Commit A
$ git log --format=oneline master/master
bec0e7af9374e4c57cb87f3fd5b2fb2b6a3f431b G
a61e01d096e81c36bcc450afd98ca94cff909622 F
3aa6b69127d20ac42746c39be3c273a9d80fb706 E

What happens if you merge now?

I got merge conflicts that I'd rather auto-resolve.

$ git merge copy/master
Auto-merging test.txt
CONFLICT (add/add): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.

Grafts in action

$ echo 3aa6b69127d20ac42746c39be3c273a9d80fb706 \
    95b0fc836dbea7eaa0d043390df841d184af7cd5 > .git/info/grafts
$ git filter-branch --tag-name-filter cat -- --all
Rewrite bec0e7af9374e4c57cb87f3fd5b2fb2b6a3f431b (7/7)
WARNING: Ref 'refs/heads/master' is unchanged
Ref 'refs/remotes/copy/master' was rewritten
WARNING: Ref 'refs/remotes/master/master' is unchanged
$ git merge copy/master
Auto-merging test.txt
Merge made by the 'recursive' strategy.
 test.txt |    5 +++++
 1 file changed, 5 insertions(+)

The final result

$ git log --graph --oneline | cat
*   4dadb0a Merge remote-tracking branch 'copy/master'
|\  
| * bb2fc85 G
| * be88bc5 F
| * 0816baf E
* | 1532332 D
* | 90903c0 C
|/  
* 95b0fc8 B
* e9de90c Commit A

这篇关于使用git将两个目录与​​公共父级(但不同的SHA1)合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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