如何合并两个不同的 Git 存储库? [英] How to Merge two different Git repositories?

查看:23
本文介绍了如何合并两个不同的 Git 存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 Github 存储库.一个存储库在远程服务器上,另一个在本地服务器上.它们都有不同文件和文件夹的不同提交历史.现在我想合并它们,这样我就可以将它们作为一个存储库放在远程服务器上.请帮忙!

I have two Github repositories. One repository is on the remote server and another on the local server. Both of them have different commit histories of different files and folders. Now I wanted to merge both of them so that I can have both of them on the remote server as one single repository. Please help!

我已经寻找了各种解决方案,建议如下:重置本地存储库的头部,然后将远程存储库拉到同一目录,但它似乎不起作用:

I have looked for various solutions suggesting as: reset the head of the local repository and then pull the remote repository on the same directory but it doesn't seem to be working:

git reset --soft head ~CommitSHA (本地仓库的第一次提交)

git reset --soft head ~CommitSHA (First commit of the local repo)

git pull ~giturlofremoterepo(拉取同目录下的远程repo)

git pull ~giturlofremoterepo (Pulling remote repo in the same directory)

推荐答案

简而言之,您可以签出一个存储库,将远程添加到第二个,将第二个重新定位到第一个之上,然后将结果推送到新的单个远程仓库:

In a nutshell you can checkout one repository, add a remote to the second, rebase the second on top of the first and push the result to your new single remote repository:

克隆第一个存储库并将远程添加到第二个存储库

Clone the first repository and add a remote to the second

git clone https://first.repo
git remote add second https://second.repo

获取第二个并将其主分支检查到本地分支second

Fetch the second and check its master branch out to a local branch second

git fetch second
git checkout second/master
git checkout -b second

在第一个存储库的主分支之上重新设置第二个存储库的主分支.解决过程中的任何潜在冲突.

Rebase the master branch of the second repository on top of the master branch of the first. Resolve any potential conflict along the way.

git rebase master second

推送到新的上游仓库

git push -u ...

这导致两个提交历史被一个接一个地连接起来.

This results in the two commit histories being concatenated one after each other.

这篇关于如何合并两个不同的 Git 存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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