将一个 git repo 作为分支导入另一个 git repo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9767381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Importing one git repo as a branch into another git repo
提问by fasseg
For historic reason we have source code for different version in different git repositories. So while Project A holds the version X of the source Project B holds version Y of the source.
由于历史原因,我们在不同的 git 存储库中有不同版本的源代码。因此,虽然项目 A 拥有源代码的 X 版本,但项目 B 拥有源代码的 Y 版本。
Do you guys know a way to import Project B as a branch of Project A?
你们知道一种将项目B作为项目A的分支导入的方法吗?
Thanks
谢谢
回答by Jan Hudec
I am not sure, what you mean by "git project". In git the states of source code are described by commits(a.k.a. revisions). These are stored in repositories, but are independent of the them and can be copied between repositories freely. In fact, to work on the sources git always copies the commits to your local repository that lives in the .git
directory of your working copy. Branches are just names pointing to commits.
我不确定,您所说的“git 项目”是什么意思。在 git 中,源代码的状态由提交(又名修订)描述。这些存储在存储库中,但独立于它们并且可以在存储库之间自由复制。事实上,为了处理源代码,git 总是将提交复制到位于.git
工作副本目录中的本地存储库。分支只是指向提交的名称。
So if you have some branches in one repository and other branches in another repository, you can:
因此,如果您在一个存储库中有一些分支而在另一个存储库中有其他分支,您可以:
Pull both into your local working repository:
git remote add B git://url.to/project.B.git git fetch B
Base your work on branches from B
git checkout -b newname remotes/B/branchname
Push the branches you got from one central repository to the other:
git push origin remotes/B/branchname:branchname
or the other way around
git push B remotes/origin/master:othername
将两者都拉入您的本地工作存储库:
git remote add B git://url.to/project.B.git git fetch B
将您的工作基于 B 的分支
git checkout -b newname remotes/B/branchname
将您从一个中央存储库获得的分支推送到另一个:
git push origin remotes/B/branchname:branchname
或者反过来
git push B remotes/origin/master:othername
You can omit the remotes/
prefix most of the time.
remotes/
大多数情况下您可以省略前缀。
回答by knittl
This is simple with Git. You have to add project B as remote, then fetch:
这对 Git 来说很简单。您必须将项目 B 添加为远程,然后获取:
git remote add projectB git://url.to/projectB.git
git fetch projectB
回答by VST
- First clone project A from git hub
- 首先从 git hub 克隆项目 A
git clone {git hub Project A URL}
git clone {git hub 项目 A URL}
- Add remote repository path of project B
- 添加项目B的远程仓库路径
git remote add projectBrepo {git hub project B URL}
git remote add projectBrepo {git hub project B URL}
- project B branches will be fetched
- 将获取项目 B 分支
git fetch projectBrepo
git fetch projectBrepo
- check the all branches from project A and project B
- 检查项目 A 和项目 B 的所有分支
git branch -v -a
git 分支 -v -a
- check-out each branch name from project B (ex:master, branch_name1)
- 检出项目 B 中的每个分支名称(例如:master、branch_name1)
git checkout -b master_old remotes/projectBrepo/master
git checkout -b master_old 遥控器/projectBrepo/master
- Push the master branch from project B to project A as master_old
- 将 master 分支从项目 B 推送到项目 A 作为 master_old
git push origin master_old
git push origin master_old
- check-out branch_name1 from project B
- 从项目 B 中签出 branch_name1
git checkout -b branch_name1 remotes/projectBrepo/branch_name1
git checkout -b branch_name1 远程/projectBrepo/branch_name1
- Push the brnach_name1 from project B to project A
- 将 brnach_name1 从项目 B 推送到项目 A
git push origin branch_name1
git push origin branch_name1