在不重命名文件的情况下将两个git回购合并为一个

在不重命名文件的情况下将两个git回购合并为一个,git,merge,Git,Merge,我想把两个回购A和B合并成一个共同的回购C。 A和B在根文件夹级别都有很多文件,所以我想在repo C的根文件夹中创建文件夹A和B,并将每个原始repo的内容放在其匹配的文件夹中 在这个过程中是否没有使用git mv?问题是我使用的是Xcode,不幸的是它没有遵循重命名文件的历史记录,所以如果可能的话,我希望避免移动。实际上,我想将每个回购协议直接合并到它的子文件夹中。实现这一点的方法有很多,博客文章的数量是原来的两倍,因此回答了这个问题。最简单的解决方案可能是使用该工具 另一个选择(我没有尝试

我想把两个回购A和B合并成一个共同的回购C。 A和B在根文件夹级别都有很多文件,所以我想在repo C的根文件夹中创建文件夹A和B,并将每个原始repo的内容放在其匹配的文件夹中


在这个过程中是否没有使用
git mv
?问题是我使用的是Xcode,不幸的是它没有遵循重命名文件的历史记录,所以如果可能的话,我希望避免移动。实际上,我想将每个回购协议直接合并到它的子文件夹中。

实现这一点的方法有很多,博客文章的数量是原来的两倍,因此回答了这个问题。最简单的解决方案可能是使用该工具

另一个选择(我没有尝试过)是遵循这个。但是,如果您有要移动到子目录中的点文件,则需要对其进行一些调整。

  • -一个简单的解决方案。将只合并一组文件,同时在不同的前缀(目录)下重新定位它们。不会对这些文件的历史记录做任何更改
  • 脚本及其
    add
    命令。将创建合成历史记录,就像原始代码始终位于指定目录中一样

我已经能够在Michael建议的基础上进行合并。最初的帖子只负责合并主分支机构——我将其扩展为从合并的回购协议中提取所有分支机构,因此不会丢失历史记录

重申一下,我已经找到了许多其他建议的进行这种合并的方法,但是没有一种既保存历史记录又不重命名路径(导致Xcode无法显示历史记录)

#初始化一个git repo,它将包含两个repo的合并,每个repo都位于自己的子目录中
cd~/dev
mkdir合并
cd合并
初始化
#创建主分支的虚拟初始提交
git提交--允许空-m“初始提交”
#克隆首次回购
光盘
git克隆
cd报告
#结帐所有分行
对于“git branch-r | grep-v master”中的远程;进行git签出——跟踪$remote;完成
#将子目录前置到所有分支中的所有提交文件路径
git filter branch-f--prune empty--tree filter'mkdir-p.sub;
找到-mindepth 1-exec mv{}.sub;
mv.次级报告
'--glob=refs/heads/*
#垃圾清理
git gc——攻击性
#第二次回购的步骤相同
光盘
git克隆
cd报告
#结帐所有分行
对于“git branch-r | grep-v master”中的远程;进行git签出——跟踪$remote;完成
#将子目录前置到所有分支中的所有提交文件路径
git filter branch-f--prune empty--tree filter'mkdir-p.sub;
找到-mindepth 1-exec mv{}.sub;
mv.次级报告
'--glob=refs/heads/*
#垃圾清理
git gc——攻击性
#将修改后的回购合并为统一回购
cd../合并
git远程添加RepoA../RepoA
git远程添加RepoB../RepoB
git fetch——全部
用于“git分支-r”中的远程;DoGit checkout-b$remote——跟踪$remote;完成
#将每个原始回购协议的所需分支(通常为主分支)合并到统一回购协议的主分支中
切换到主分支
git合并RepoA/master
git合并RepoB/master
#移除遥控器
git远程rm RepoA
git远程rm RepoB
#垃圾清理
git gc——攻击性
#全部完成
#(可选)推入新的空远程存储库
git远程添加
git推送--所有的重新合并

我看到的大多数答案中提到的子树都有很多关于日志被破坏的评论-你知道这些吗?我相信我完全按照第一个方法中的说明进行了操作-看起来子树下的文件根本不存在历史记录,所有文件都被认为是新的。不幸的是,第二种方法也有效地将所有文件添加为新文件,尽管保留了存储库的一般历史记录。因此,再一次,就我所知,子树下的所有文件都不存在任何历史记录:(我无法在Mac(Mountain Lion)上完成这项工作)。获取:无法在@INC中找到Git/FastExport/Stitch.pm(@INC包含:/Library/Perl/5.12/darwin-thread-multi-2level/Library/Perl/5.12/Network/Library/Perl/5.12/Library/Perl/Updates/5.12.4/System/Library/Perl/5.12/darwin-thread-multi-2level/System/Library/Perl/5.12/System/Library/Library/Perl/Perl/Perl/Perl/extrs/5.12/darwin-thread-multi-2level/System/System/Library/Per)l/Extras/5.12.)在script/git stitch repo第6行。BEGIN失败——编译在script/git stitch repo第6行中止。您是如何安装它的?
cpan安装git::FastExport::stitch
?不,只是从页面下载的。在尝试cpan安装时,我发现了许多类似的错误,例如:在@INC中找不到git.pm(@INC包含:/Users/dan/.cpan/build/Git-fastedexport-0.08-gNondI/blib/lib/Users/dan/.cpan/build/Git-fastedexport-0.08-gNondI/blib/arch/System/Library/Perl/Extras/5.12。)在t/Utils.pm第6行。您需要确保Git安装中的
Git.pm
位于
PERL5LIB
路径变量上。不知道如何安装Git,很难给出建议。通常它类似于
$PREFIX/lib/perl5/site\u perl
,其中$PREFIX是Git i的安装前缀安装,例如
/usr
/usr/local
。谢谢!我可以通过将
/Applications/Xcode.app/Contents/Developer/usr/share/git-core/perl
添加到
/Library/perl/5.12/AppendToPath
文件中,并运行
git fast import
,而不是
git fast import
来安装它。然而,looking对于结果,我不确定这是否是我想要的。回购协议是完全不相交的-因此我对正在合并的主分支不感兴趣。我认为它是合并的,因为我看到“master-B”分支包含两个文件夹,而不是一个。而且
--select
参数意味着正在进行合并?隐藏文件技巧ld b
# Init a git repo which will contain the merge of the two repos, each in its own subdirectory
cd ~/dev
mkdir Merged
cd Merged
git init

# Dummy initial commit to create a master branch
git commit --allow-empty -m "Initial commit"

# Clone first repo
cd ..
git clone <RepoA url>
cd RepoA

# Checkout all branches
for remote in `git branch -r | grep -v master `; do git checkout --track $remote ; done

# Prepend subdirectory to all committed files paths in all branches
git filter-branch -f --prune-empty --tree-filter ' mkdir -p .sub;
  find . -mindepth 1 -exec mv {} .sub;
  mv .sub RepoA
  ' -- --glob=refs/heads/*

# Garbage cleanup
git gc --aggressive

# Same steps for second repo
cd ..
git clone <RepoB URL>
cd RepoB

# Checkout all branches  
for remote in `git branch -r | grep -v master `; do git checkout --track $remote ; done

# Prepend subdirectory to all committed files paths in all branches
git filter-branch -f --prune-empty --tree-filter ' mkdir -p .sub;
  find . -mindepth 1 -exec mv {} .sub;
  mv .sub RepoB
  ' -- --glob=refs/heads/*

# Garbage cleanup
git gc --aggressive

# Merge modified repos into unified repo
cd ../Merged
git remote add RepoA ../RepoA
git remote add RepoB ../RepoB
git fetch --all
for remote in `git branch -r`; do git checkout -b $remote --track $remote ; done

# Merge wanted branches (usually master) from each original repo into the master branch of the unified repo
git checkout master
git merge RepoA/master
git merge RepoB/master

# Remove remotes
git remote rm RepoA
git remote rm RepoB

# Garbage cleanup
git gc --aggressive

# All done

# Optionally push into a new empty remote repository
git remote add RepoMerged <Merged Repo URL>
git push --all RepoMerged