9

I have a repository (repo1) with a master branch and another repository (repo2) with a master branch. Now I want to create a new branch in repo1 from repo2 with all commits history.


My expected result:

repo2
----
|
 \
  master


repo1
----------
|         |
 \         \
  master    master-from-repo2
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150

1 Answers1

10
cd repo1
git fetch repo2 master:master-from-repo2

Remote branch master is fetched from repo2 into local branch master-from-repo2.

phd
  • 82,685
  • 13
  • 120
  • 165
  • Thank you, it works fine. If repo2 has another branch and when I want to get this branch can I do it: `any-branch-in-repo2:master-from-repo2` instead of `master:master-from-repo2`? – Benyamin Jafari Jan 13 '19 at 13:54
  • 1
    Yes, any branch will do. – phd Jan 13 '19 at 14:19
  • Thanks for the answer. But what if the `repo2` that I want to fetch is local? – bytrangle Nov 01 '21 at 09:05
  • @bytrangle Shouldn't matter, `git fetch` fetches over any supported protocol, git://, ssh://, https://, file:/// (local). Just add `repo2` as a remote: `git remote add repo2 file:///path/to/local/repo` – phd Nov 01 '21 at 09:11
  • @phd Thank you. Just fetched a local repo successfully. – bytrangle Nov 01 '21 at 09:13