Adrian Andreias

Jul 21, 2014

Merge git personal/master to upstream/master

So you forked the project's repo to your own personal remote repo (in Gitlab/Github/etc.) and you committed your changes to personal/master and now you're trying to merge your commits to upstream/master. (Good idea to work in a branch, and not on master, by the way :)

You'll have a problem because both branches, from the personal and the upstream repos, are called master.

I gave up doing this when working with a single local git directory that has the origin/master branch tracking the personal remote repo and upstream/master branch tracking the main repo because would involve some non-straight forward configs that look hard to debug/maintain.

Another option is to do the merge in another directory. You'd have a separate local repo (besides the personal development repo) that is cloned from the upstream:

git clone git@gitserver:<<theproject>>/<<theproject>>.git .

and:

# add your personal (development) remote repository
git remote add <<adrian>> git@gitserver:<<adrian>>/<<theproject>>.git .
# create a local branch named <<adrian>>-master (avoiding the master name collision!) tracking <<adrian>>/master
git fetch <<adrian>> git checkout -b <<adrian>>-master <<adrian>>/master
# make sure you're on origin/master (main/upstream project)
git checkout master
# and merge your commits into main project
git merge <<adrian>>-master --ff-only
# send the commits to main project remote server
git push

Notes:

  • I'm using Gitlab 7.0, but that should matter (it uses the git ssh user for key based authentication). I'm doing the merge from command line because I want to have fast-forward merges (without an extra commit for the merge - this is how the pull request merge works in the Gitlab web interface). Also, my pull request web page is crashing right now, but that's another story.
  • replace <<adrian>> with your personal repo name
  • same with <<theproject>>
  • gitserver is obviously your own git server hostname