将 git repo 合并到另一个 repo 的分支中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21353656/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:38:03  来源:igfitidea点击:

Merge git repo into branch of another repo

git

提问by Milktrader

Given repo Foo and repo Bar. I want to merge Bar with Foo, butonly into a separate branch, called baz.

给定 repo Foo 和 repo Bar。我想将 Bar 与 Foo 合并,只能合并到一个名为baz.

git checkout -b baz<= put the Bar repo here.

git checkout -b baz<= 把 Bar 仓库放在这里。

回答by larsks

You can't merge a repositoryinto a branch. You can merge a branchfrom another repository into a branchin your local repository. Assuming that you have two repositories, fooand barboth located in your current directory:

您不能将存储库合并到分支中。您可以合并一个分支,从另外一个仓库到一个分支在本地资源库。假设您有两个存储库,foo并且bar都位于当前目录中:

$ ls
foo bar

Change into the foorepository:

更改到foo存储库:

$ cd foo

Add the barrepository as a remote and fetch it:

bar存储库添加为远程并获取它:

$ git remote add bar ../bar
$ git remote update

Create a new branch bazin the foorepository based on whatever your current branch is:

根据您当前的分支bazfoo存储库中创建一个新分支:

$ git checkout -b baz

Merge branch somebranchfrom the barrepository into the current branch:

somebranchbar存储库中的分支合并到当前分支中:

$ git merge --allow-unrelated-histories bar/somebranch

(--allow-unrelated-historiesis not required prior to git version 2.9)

--allow-unrelated-histories在 git 2.9 版之前不需要)

回答by vinzdef

Updated with "real-life" commands:

更新了“现实生活”命令:

Start from your repo directory, make sure your working copy is clean (no files changed, added or removed).

从您的 repo 目录开始,确保您的工作副本是干净的(没有更改、添加或删除文件)。



Make a new branch:

新建一个分支:

git checkout -b <my-branch>

git checkout -b <my-branch>

Add the secondary remote, then fetch it:

添加辅助遥控器,然后获取它:

git remote add <repo-name> [email protected]:xxx/<repo-name>.git
git remote update

Merge one of their branches in your current branch:

将他们的分支之一合并到您当前的分支中:

git merge <repo-name>/<their-branch>

git merge <repo-name>/<their-branch>



If you don't know which <their-branch>you want, then go for master

如果你不知道<their-branch>你想要哪个,那就去吧master

If you are sure you want to accept all remote changes and avoid conflicts (overwrite yours) then you can specify -X theirsas option for git mergein the last step.

如果您确定要接受所有远程更改并避免冲突(覆盖您的),那么您可以在最后一步中指定-X theirs为选项git merge

If you want to add it in a subdirectory then probably you should probably use git submodules

如果你想将它添加到一个子目录中,那么你可能应该使用git submodules

回答by TheBigSot

Using the guide from larsks, I was able to do this using SourceTree.

使用 larsks 的指南,我能够使用 SourceTree 做到这一点。

  1. Created a branch in the destination repository
  2. Added the source repository as a remote, by hitting the Settings button and adding the source repository.
  3. Branches from both repository now show in the branch list. I used the merge tool to merge a branch from the source repository to my new destination repository's branch.
  4. Resolved any conflicts using either SourceTree or my IDE
  5. Commit the changes in my branch.
  6. Remove the source repository from the remote list, by using the Settings button.
  1. 在目标存储库中创建了一个分支
  2. 通过点击设置按钮并添加源存储库,将源存储库添加为远程存储库。
  3. 来自两个存储库的分支现在显示在分支列表中。我使用合并工具将源存储库中的分支合并到新目标存储库的分支。
  4. 使用 SourceTree 或我的 IDE 解决了任何冲突
  5. 在我的分支中提交更改。
  6. 使用“设置”按钮从远程列表中删除源存储库。