284

I forked a GitHub repository. Then I pushed some changes to my fork. Then the original repository merged my changes and some others. Now, I want to merge those changes I'm missing. I tried a simple pull followed by push, but this yield my commits in duplicate. What's the best way to do it?

0

2 Answers 2

385

You probably have a "remote" for each repository. You need to pull from one remote and push to the other.

If you originally cloned from your fork, that remote will be called "origin". If you haven't added it already, you'll need to add the other repository as another remote:

git remote add <shortname> git://github.com/<ownerName>/repo.git

After that's all set up, you should indeed be able to (github changed default branch from master to main, change as necessary)

git pull <shortname> master
git push origin

Remember, git pull is nothing more than a macro that does git fetch and git merge, in that order. You just need to fetch the list of commits from the other repository and then merge his branch into your tree. Merging should do the right thing with your commits on both branches.

GitHub, in all its perpetual awesomeness, gives you a shortcut, of course. There's a "fast-forward" button on your fork of the repository that you can use to catch your fork up if you're entirely merged into the other side.

9
  • 1
    Is there a way to do this entirely with remote operations? If I understand correctly, with this method you will download all the changes to the local repository and then upload (push) them all back to the fork on github. I'd rather just somehow pull all the changes directly into the fork on github.
    – Ken Liu
    Dec 7, 2009 at 4:56
  • 1
    No. Git does not support that. Luckily, Github has a merge button in the web interface now, though.
    – cweiske
    Oct 21, 2011 at 5:04
  • 13
    @cweiske - where is this Merge button? I've looked all through the admin pages and main pages, but can't find it :(.
    – Adam
    Dec 18, 2011 at 15:33
  • 2
    @Adam, it's on the Pull Request page. So the forker would open a Pull Request to merge one of their branches to one of yours. There you can click merge button. Dec 11, 2012 at 22:18
  • 3
    The button Fast-forward does not seem to be available any more. For information, I have used another git URL within my command: git remote add snaury git@github.com:snaury/script-runner
    – oHo
    Dec 5, 2013 at 21:11
86

So the accepted answer above didn't work for me perfectly. Namely, it seemed to lose the link to the original github author when it worked, and then didn't seem to work anymore after that. I think the problem was that the answer left out the / between the remote name and the branch. So it would fetch a branch called master from the remote, but then not be able to do anything with it. Not really sure why.

Here's the way github recommends from their site.

Once you have cloned your forked repo, you do need to add a remote pointing to the original like the previous answer said. They like to call it upstream, but it doesn't matter.

git remote add upstream git://github.com/octocat/Spoon-Knife.git

Then you fetch

git fetch upstream

and you'll see the versions available for merging

From git://github.com/octocat/Spoon-Knife.git
 * [new branch]      gh-pages   -> upstream/gh-pages
 * [new branch]      master     -> upstream/main

Then you just need to choose the branch you want to merge in. Mind you these aren't local branches, they are stored under remotes. But provided you don't have a local branch called upstream/master (which is allowed) you should be fine merging with the line below:

git merge upstream/main

Alternatively you could shortcut the fetch/merge (after the initial fetch at least) with this line:

git pull upstream/main
2
  • The problem is that if you already have changes, the changes you incorporate will create a merge commit. Useful in some cases, but most times a bit pointless. Jan 12, 2014 at 23:47
  • 2
    In those case use git rebase it would work far better, and leave out those ugly empty merge commits
    – Fruch
    Sep 19, 2014 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.