1

I have two repos old_repo and new_repo. old_repo has 10 commits. new_repo has 20 commits. Date of all commits of new_repo are later than those of old_repo. I want to prepend 10 commits of old_repo to the beginning of new_repo such that the resultant repo has 30 commits in the same order in which they were originally created. What are possible ways to do it.

One obvious and tiresome approach that I can think of is cherry picking and appending to the old_repo each commit of new_repo one by one. Looking for better suggestions.

PS: the file changes between the 10th and 11th commits are not supposed to be drastic as they were once the same repo but .git folder got deleted accidentally which broke the commit sequence.

2
  • 2
    Why not just merge them together? All commits have timestamps on them, those won't be lost. Commented Jul 20, 2019 at 0:48
  • 1
    Can you open a PR from one repo to the other? That seem like the simplest solution to me. Commented Jul 20, 2019 at 0:48

1 Answer 1

3

I want to prepend 10 commits of old_repo to the beginning of new_repo such that the resultant repo has 30 commits in the same order in which they were originally created.

Git commits are a graph meaning there's multiple ways to interpret the order of commits. By date. Or "topological". Preserving the order in which they were created means date order, but it's really not that important in Git.


You have two options: merge them or rebase them. Which you choose depends on what outcome you want.

In either case, make old_repo a remote of new_repo so the repositories can see each other.

git remote add old_repo <path or url to old_repo> git fetch old_repo 

Merge

Merge old_repo/master into the new_repo's master.

git checkout master git merge --allow-unrelated-histories old_repo/master 

Resolve conflicts as normal. And you're done. The history of both repos is now merged like so. And as you can see "date order" doesn't really matter. The original two branches preserved in the history.

O1 ... O8 - O9 - O10 \ M [master] / N1 ... N18 - N19 - N20 

Rebase

This is like cherry picking each commit of new_repo on top of old_repo, but Git can do this for you.

git checkout master git rebase old_repo/master 

This will replay all the commits on new_repo's master branch on top of old_repo's master branch. You'll wind up with one branch of 30 commits. No evidence that they were ever two different repositories remains.

O1 ... O9 - O10 - N1 - N2 ... N20 [master] 

In either case it's safe to delete the remote to the old repo.

git remote rm old_repo 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.