How to combine two separate unrelated Git repositories into one with single history timeline

我是研究僧i 提交于 2020-01-09 10:05:06

问题


I have two unrelated (not sharing any ancestor check in) Git repositories, one is a super repository which consists a number of smaller projects (Lets call it repository A). Another one is just a makeshift local Git repository for a smaller project (lets call it repository B). Graphically, it would look like this

A0-B0-C0-D0-E0-F0-G0-HEAD (repo A)
A0-B0-C0-D0-E0-F0-G0-HEAD (remote/master bare repo pulled & pushed from repo A)
A1-B1-C1-D1-E1-HEAD (repo B)

Ideally, I would really like to merge repo B into repo A with a single history timeline. So it would appear that I originally started project in repo A. Graphically, this would be the ideal end result

A0-A1-B1-B0-D1-C0-D0-E0-F0-G0-E1-H(from repo B)-HEAD (new repo A)
A0-A1-B1-B0-D1-C0-D0-E0-F0-G0-E1-H(from repo B)-HEAD (remote/master bare repo pulled & pushed from repo A)

I have been doing some reading with submodules and subtree (Pro Git is a pretty good book by the way), but both of them seem to cater solution towards maintaining two separate branch with sub module being able to pull changes from upstream and subtree being slightly less headache. Both solution require additional and specialized git commands to handle check ins and sync between master and sub tree/module branch. Both solution also result in multiple timelines (with subtree you even get 3 separate timelines when using --squash).

The closest solution from SO seems to talk about "graft", but is that really it? The goal is to have a single unified repository where I can pull/push check-ins, so that there are no more repo B, just repo A in the end.


回答1:


I think you do it like this:

  1. git remote add [repo b]
  2. git fetch//get the repo b into repo a
  3. due to you want to keep the history like this: A0-A1-B1-B0-D1-C0-D0-E0-F0-G0-E1-H(from repo B)-HEAD (new repo A) you can first select the A0 as a start point, after that, use git cherry-pick one by one.

Hope this is useful for you.

Br, Tim




回答2:


I did something similar (merge history from B into A) like this:

  1. go to your repo A
  2. git fetch <ref to your repo B> master:new-branch-on-A (so, you have copied master branch of B into a new branch 'new-branch-on-A' into your repo A.
  3. git checkout new-branch-on-A
  4. git rebase master (you rebase the new-branch-on-A with the master branch)
  5. resolve conflicts if there are any
  6. git checkout master
  7. git merge new-branch-on-A
  8. git branch -d new-branch-on-A
  9. Done :), your histories are merged. I think all commits from A are before commit from B in the new history (git rebase -i is your friend if needed).


来源:https://stackoverflow.com/questions/9781729/how-to-combine-two-separate-unrelated-git-repositories-into-one-with-single-hist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!