How to merge branch of forked repo into master branch of original repo?

42,849

Solution 1

When using GitHub, you could do this using a Pull Request. Simply push your development branch to the forked remote repository and create the pull request as described in the linked article. The owner of the original repository can then add your repository as a new remote repository, fetch your changes and merge your development branch back into the master branch.

It is usually not necessary for your master branch to be fully up-to-date -- although it is extremly helpful, especially when merging the development branch.

First, you should bring the master branch in the forked repository up-to-date:

$ git remote add upstream ssh://path/to/original/repo
$ git checkout master
$ git fetch upstream # Instead of "fetch" & "merge", "pull" would also work
$ git merge upstream/master 

Then either rebase or merge your development branch into the master branch:

$ git checkout development
$ git rebase master # OR "git merge master"

Resolve any conflicts, if necessary. When merging, use git commit to finish the merge, when rebasing use git commit && git rebase --continue.

You should now be able to git push to the origin of the forked repository, and create a pull request. Alternatively, if you're authorized to do so, you can also push directly to the original repository from your fork.

Solution 2

It sounds like you've already cloned both repositories. I'm going to assume you have them in directories clone1 and clone2.

You need to get all the changesets into one repository before you can do the merge. So we're going to pull from one of your clones into the other one. Were you doing this from scratch, you would just clone one repository and then fetch the other, instead of cloning both.

  1. cd clone1
  2. git checkout master
  3. git remote add clone2 ../clone2 (put both remotes into one repo). If you hadn't cloned both repositories, you wouldn't have to do this - you'd just do git remote add otherthing https://<github URI>
  4. git fetch (fetch changes from clone2 into clone1). Now you have one repository with everything.
  5. git checkout origin/master Or you can check out a local branch. Your preference. Remember, origin here refers to the source for clone1.
  6. git merge clone2/development If necessary, resolve conflicts at this point. Commit the result.
  7. git checkout -b my-ongoing-development (name the branch you just made, which contains both remote branches)

Solution 3

The previous comment is close, except step 6 should be

git pull clone2 development

Refer to Git merge branch of another remote

Share:
42,849
pemistahl
Author by

pemistahl

Computational linguist and software engineer, currently interested in Kotlin and Rust programming

Updated on July 15, 2021

Comments

  • pemistahl
    pemistahl almost 3 years

    There is an open source project on GitHub. Its original repo contains the most recent master branch. Now there is someone (not me) who has forked this repo, created a development branch from the master branch and added stuff to the development branch. What I would like to do now is to merge the development branch back to the master branch. Unfortunately, the master branch of the forked repo is not up-to-date. Also note that I'm not in possession of the two respective remote repos.

    At the moment, I have cloned both the original repo and the forked repo to my local machine using git clone. How can I merge the development branch of the forked repo into the master branch of the original repo on my local machine (i.e. not on the remote server which is not possible anyway)?