简体   繁体   English

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

[英]Merge git repo into branch of another repo

Given repo Foo and repo Bar.给定 repo Foo 和 repo Bar。 I want to merge Bar with Foo, but only into a separate branch, called baz .我想将 Bar 与 Foo 合并,只能合并到一个名为baz的单独分支中。

git switch -c baz <= put the Bar repo here. git switch -c baz <= 把 Bar 仓库放在这里。

You can't merge a repository into a branch .您不能将存储库合并到分支中 You can merge a branch from another repository into a branch in your local repository.你可以在你的本地仓库合并来自另一个存储库分支到一个分支 Assuming that you have two repositories, foo and bar both located in your current directory:假设您有两个存储库, foobar都位于当前目录中:

$ ls
foo bar

Change into the foo repository:切换到foo存储库:

$ cd foo

Add the bar repository as a remote and fetch it:bar存储库添加为远程并获取它:

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

Create a new branch baz in the foo repository based on whatever your current branch is:根据您当前的分支在foo存储库中创建一个新分支baz

$ git switch -c baz

Merge branch somebranch from the bar repository into the current branch:bar存储库中的somebranch分支合并到当前分支中:

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

( --allow-unrelated-histories is not required prior to git version 2.9) (在 git 2.9 版之前不需要--allow-unrelated-histories

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>

Add the secondary remote, then fetch it:添加辅助遥控器,然后获取它:

git remote add <repo-name> git@github.com:xxx/<repo-name>.git
git remote update

Merge one of their branches in your current 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 theirs as option for git merge in 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

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使用 SourceTree 或我的 IDE 解决了任何冲突
  5. Commit the changes in my branch.在我的分支中提交更改。
  6. Remove the source repository from the remote list, by using the Settings button.使用“设置”按钮从远程列表中删除源存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM