Sandeep
Sandeep

Reputation: 31

How do you mirror a specific branch to a branch in another repository?

I want to mirror only the master branch in "old repository" to a branch "X" in the "new repository". How do i do this? [To be clear I do not want to mirror the whole repo or the other branches]

Once i mirror this, at some point I will want to take "X" and merge all the new changes into the "master" of the "new repository". I know i can do a pull request but how would i do this through the terminal?

Thanks for all the help.

Upvotes: 3

Views: 4691

Answers (1)

Schwern
Schwern

Reputation: 164939

You can do this by adding the old repository as a remote with git remote add <name> <url> and then fetch one or more branches with git fetch <name> <branch> .... Then you can work with it like any other remote branches and merge your branches normally with git merge <name>/<branch>.

I would advise against it except in certain circumstances like an enormous branch with lots of big files (which would benefit from using git-lfs). It's extra complexity you don't need.

Because of how Git works, most branches will share the majority of the repository. Once you fetch one open branch, fetching other branches comes at a low cost. For example...

                                       Q - R - S [branch2]
                                      /
A - B - F ------ G - H - I - J - K - O - P [master]
     \          /                 \
      C - D - E                    L - M - N [branch1]

If you git fetch old branch1 you'll get commits N, M, L, K, J, I, and everything else all the way back to A. If you then git fetch old branch2 it will only fetch Q, R, and S. So unless your repository is oddly structured, fetching branches after the first comes at a very low cost. Might as well catch fetch em all and make the whole process simpler.

Upvotes: 2

Related Questions