290

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?

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510

2 Answers2

389

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.

hfingler
  • 1,931
  • 4
  • 29
  • 36
Jim Puls
  • 79,175
  • 10
  • 73
  • 78
  • 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 07 '09 at 04:56
  • 1
    No. Git does not support that. Luckily, Github has a merge button in the web interface now, though. – cweiske Oct 21 '11 at 05: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 '11 at 15:33
  • Can anyone describe how to do the same from TortoiseGit? – DitherSky Jun 18 '12 at 08:10
  • 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. – Rob Barreca Dec 11 '12 at 22:18
  • Thanks, worked great. For those using Visual Studio, to get to a command prompt. In the Team Explorer Panel, choose Commits, then Actions, then Command Prompt. You don't need to type 'git push origin', you can just press push in the Team Explorer Panel. – adudley Nov 27 '13 at 15:51
  • 3
    The button [Fast-forward](https://github.com/blog/266-fast-forward-your-fork) does not seem to be available any more. For information, I have used another [tag:Git] URL within my command: `git remote add snaury git@github.com:snaury/script-runner` – oHo Dec 05 '13 at 21:11
  • @RobBarreca So if I create a fork and a change, you're saying that only the original owner will see a "Merge" button and only they can merge the changes? How do they know there is an open "Pull Request"? – vapcguy Oct 16 '15 at 20:34
  • Github fast-forward option: On the Code page of the fork, there is a Sync Fork button (to the right of "this branch is N commits behind" message). It gives you two options: to delete commits in the fork that aren't upstream, or to merge changes from the upstream... To merge changes TO the upstream, looks like you have to do what this answer describes: add the downstream as a remote, then merge commits manually from it. – Bampfer Mar 16 '23 at 16:21
90

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
Star
  • 131
  • 3
  • 18
Bob Spryn
  • 17,742
  • 12
  • 68
  • 91
  • 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. – Pablo Olmos de Aguilera C. Jan 12 '14 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 '14 at 07:25