2

In git, is it possible to merge the master of a fork to the main repo but only as a new branch? That way nothing really gets replaced and it's easier to go back since it's in a separate branch.

enchance
  • 29,075
  • 35
  • 87
  • 127
  • 1
    Please see: http://stackoverflow.com/questions/244695/how-to-combine-two-branches-from-two-different-repositories-in-a-single-reposito – praseodym Jan 10 '13 at 21:47

3 Answers3

3

You just need to fetch the master branch of the fork to the main repo as a separate branch. For example

cd /path/to/master
git fetch /path/to/fork master:fork
Kalle Pokki
  • 4,939
  • 2
  • 17
  • 18
  • 1
    Thanks, Kalle! Here's an explanation on how to do it. Since I have both the fork and the main repo locally. I go to `master` of the main repo (not the fork). Create a branch called `mybranch` (don't switch to it). Run `git fetch /path/to/fork master:mybranch`. This gets master of the fork and merges it in `mybranch` in the main repo. Finally, push it `git push origin mybranch`. Again, you are always in the main repo not in the fork. – enchance Jan 11 '13 at 09:09
  • @enchance You don't even need to create `mybranch`, as fetch does that automatically. And techically, it's not a merge as you said, it's just a fetch. Fetch doesn't touch working directories or modify the branches at all. It just fetches the objects and creates/modifies the head that points to them. Couldn't understand what the push was for. – Kalle Pokki Jan 11 '13 at 09:21
1

Push Master to New Refspec

In Git, a branch is just a refspec, so you can push any refspec to any other refspec. For example:

git checkout master
git push origin master:copy_of_master
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • Is *copy_of_master* the branch name in the main repo? I've never encountered using a `:` before. Can you explain what it does? – enchance Jan 10 '13 at 22:11
  • @enchance Yes, for your purposes it's just a branch name. You can substitute any branch name you like for it, or specify the full path of the new refspec. See http://git-scm.com/book/ch9-5.html (especially the section on pushing refspecs) for additional details on what refspecs are and how to specify them. – Todd A. Jacobs Jan 10 '13 at 22:43
0

Add the fork as a remote and do git checkout -b branchname remotename/master and that's all.

wRAR
  • 25,009
  • 4
  • 84
  • 97