338

Let's assume there is a repository someone/foobar on GitHub, which I forked to me/foobar.

How do I pull new commits from the parent repository directly to my fork, without having to add a separate remote and remember to pull regularly from there ?

The goal is to:

  • git pull to fetch from the parent repository
  • git push to send everything to my fork
1ace
  • 5,272
  • 4
  • 23
  • 30
  • 27
    The surprising answer by @Olufemi seems to do what you want, except that as ChristianGosch notes in a comment, you end up with a "merge commit" on top of your branch, and "nothing to compare" will not appear. That seems unacceptable to many projects for which you want to make branches for them to pull. So is there really no way to do this most basic of contributor workflows in github without running arcane git commands on a personal computer? Or maybe I should just throw away my whole repo and re-fork every time I want to contribute?? – nealmcb Dec 07 '14 at 22:14
  • Possible duplicate of [github, update forked project](http://stackoverflow.com/questions/4936109/github-update-forked-project) – mpromonet Jan 17 '16 at 16:25
  • 3
    https://github.com/isaacs/github/issues/121 – JasonPlutext Jan 03 '17 at 06:45
  • Possible duplicate of [How do I update a GitHub forked repository?](https://stackoverflow.com/questions/7244321/how-do-i-update-a-github-forked-repository) – Ciro Santilli OurBigBook.com Jun 16 '18 at 21:45
  • @nealmcb Isn't the problem with a "merge commit" on the top of your branch avoided if you rebase? – Marko Apr 17 '20 at 08:46
  • @Marko I guess so, but when I made that comment, it seems that the rebase option wasn't in the github gui yet. See the edits to the answer I referenced, and extensive comment history. – nealmcb Apr 19 '20 at 22:49
  • 3
    [The new answer](https://stackoverflow.com/questions/20984802/how-can-i-keep-my-fork-in-sync-without-adding-a-separate-remote/67428134#67428134) for May 2021. – iBug May 07 '21 at 02:10
  • There are two ways one can do that: from the web UI, or from the command line. See [my answer here](https://stackoverflow.com/a/65401892/7109869). – Gonçalo Peres Feb 08 '22 at 17:10

9 Answers9

667

Open the forked Git repository me/foobar.

Click on Compare:

Here is a sample image of the page

You will get the notification:

There isn't anything to compare.
someone:master is up to date with all commits from me:master. Try switching the base for your comparison.

Click on switching the base on this page:

Here is an example on the page

Then you get to see all the commits made to someone/foobar after the day you forked it.

Click on Create pull request:

Here is a sample page

Give the pull request a title and maybe a description and click Create pull request.

On the next page, scroll to the bottom of the page and click Merge pull request and Confirm merge.

Your Git repository me/foobar will be updated.

Edit: rebase options are shown here:

enter image description here

userJT
  • 11,486
  • 20
  • 77
  • 88
  • 59
    This is confusing because when you switch the base github throws you in the page for the original repo, so it seems like you are opening a pull request at the upstream fork. But it totally works. – Eduardo Apr 15 '14 at 18:38
  • 9
    To avoid the confusing situation mention above, it'd better click the `Edit` button and manually switch the `base fork` to `me/foobar` and `head fork` to `someone/foobar`. In this way it's much clear – macemers Aug 06 '14 at 06:55
  • 2
    However it works (though it is confusing for people not grown up with git since their birth). Shoud be marked as the correct answer, although GitHub WebUI has changed slightly meanwhile. Fortunately there are already some links to this from similar questions. But what happens if auto-merge fails and tweaking is required? – Christian Gosch Oct 10 '14 at 08:55
  • Thanks for the reply @ChristianGosch. I have edited the answer to reflect a change in GitHubs interface. I doubt if auto-merge will fail in any case, it is either a merge is possible or not possible. GitHub indicates this before merging. But if in a really weird case a merge fails, this approach won't be the most appropriate. A remote will have to be added, then you can pull from the remote and fix all merge conflicts. – Olufemi Israel Olanipekun Oct 10 '14 at 13:57
  • 28
    Another one: This works fine if you did that not before already, but afterwards there is the "merge commit" on top of your commit history. Thus "nothing to compare" will not appear. Instead one must use "Edit" button and manually interchange base and fork for this to work. – Christian Gosch Oct 23 '14 at 14:04
  • 29
    Is there no way of getting rid of that ridiculous merge commit? – kumarharsh Jan 12 '15 at 12:22
  • 1
    @all -- as a note, if your fork is not up to date, and your commits haven't been accepted yet, the UI may act different. In this case I believe the `edit` and/or `switching the base` button for choosing base fork and head fork will be titled "compare across forks". It acts like a toggle to expose the base->head chooser. After that, it should work the same with the pull request then subsequent merge into your base. – dhaupin Aug 05 '15 at 15:09
  • 1
    I did this once and then submitted a PR to upstream including "Merge PR #1 from upstream/master". Never again. – James Ko Aug 12 '15 at 02:48
  • Instructions say to 'click on "Click to create a pull request for this comparison"' but there is no such button or link. Just "Create pull request", right? – AmbroseChapel Dec 06 '15 at 23:27
  • 6
    @dhaupin: Tricky. The GitHub Web UI does not make it intuitive to swap the head and the base. Just to clarify for other readers: When you expose the base/head chooser, and you change either one of them, *the chooser goes away* and you have to click "compare across forks" **again** to re-expose the chooser so that you can change the other one. – John Y May 16 '16 at 15:50
  • 1
    The magic word here is 'rebase', which means sync from upstream. They use the lesser known magic phrase 'switching the base', which is open to misinterpretation. Rebasing is powerful voodoo, with potential for bad mojo if not automated. The github incantation above conjures a perfect albeit tedious rebase. – Dominic Cerisano Jun 21 '17 at 00:59
  • 21
    @kumar_harsh [since September 2016](https://github.com/blog/2243-rebase-and-merge-pull-requests), there now is a "rebase and merge" option for pull requests that avoids the merge commit. – waldyrious Sep 13 '17 at 09:58
  • 3
    As a shortcut to all that clicking, you can go straight to https://github.com/me/foobar/compare/master...someone:master – Jacktose Jun 26 '18 at 19:02
  • 3
    The "rebase and merge" (https://github.com/blog/2243-rebase-and-merge-pull-requests) is for branch within the same repo but between a upstream and a fork. The solution given by the above always make the fork a commit ahead of the upstream. There is no good solution. Did I miss out anything? – yoonghm Oct 21 '18 at 15:11
  • One problem with this process is that it doesn't sync tags from the parent into your fork. – davidA Dec 07 '18 at 11:53
  • 2
    this anwer needs to be rewritted based an all the inputs in comments. I made edit to try to do that – userJT Apr 02 '19 at 13:41
  • 1
    it looks like "switching the base" link doesn't exist anymore – Alexan May 20 '19 at 14:32
  • Confirming @alexan, so : Click Compare. You're now in the upstream repo. Change the Base repo to Your fork. You're now back in your fork, and it now shows your fork as the destination of diffs from your own fork! Change the Head to the upstream repo. Now create your pull request. Confirm the merge. Each time you do this you will be one more commit ahead of the upstream. :( – TonyG Oct 04 '19 at 00:01
  • @waldyrious it doesn't work https://github.com/int-ua/Cataclysm-DDA/pull/1 – int_ua Dec 24 '20 at 21:33
  • https://github.com/KirstieJane/STEMMRoleModels/wiki/Syncing-your-fork-to-the-original-repository-via-the-browser – JBeen Apr 15 '21 at 15:42
50
git remote set-url origin git@github.com:someone/foobar
git remote set-url origin --push git@github.com:me/foobar

There is one caveat though:
This is perfect if you are the only one making changes to your fork.
However, if it is shared with other people, you may have to pull from your fork, in which case a separate remote is the only solution.

Edit:
Actually, you can git pull git@github.com:me/foobar, which removes the caveat.
The choice is yours as to which is easier to remember.

1ace
  • 5,272
  • 4
  • 23
  • 30
  • 2
    Correct. I'd always suggest a separate remote though: `git remote add upstream https://github.com/someone/foobar` and then `git fetch upstream`. All remote branches of upstream will available as `upstream/branch`. This allows to repeatedly merge or rebase depending on your workflow. – knittl Feb 18 '21 at 06:34
16

Recently (early May 2021) GitHub website provided a one-click button for exactly this purpose:

I can't resist the temptation to click it.

After

See GitHub's official Tweet about this new feature.

iBug
  • 35,554
  • 7
  • 89
  • 134
  • This should be a lot more upvoated as it is the most straightforward way of doing this today. – DimP Jun 09 '22 at 12:50
10

I used a fairly simple method using the GitHub Web UI to do that:

  1. Open the original Git repository (not the forked Git repository me/foobar)
  2. Jump to the src folder, and open the file you want to change
  3. Click the pen icon. It will automatically create a label in your personal fork named "patch-1" based on the current version of the master repository: Enter image description here
  4. Enjoy! Enter image description here
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nadir
  • 695
  • 8
  • 12
  • 3
    Trying this, basically opens the editor on the file in the original git repo. I do not have write privilege (by design) into the original git repo. It seems for this method you must change the file, otherwise there is no way to "save" the file. Without saving the file, I could not get your equivalent of the "patch-1" branch to show up in my forked repository. What am I missing here? – Electro-Bunny Feb 11 '15 at 22:31
9

The "Pull" app is an automatic set-up-and-forget solution. It will sync the default branch of your fork with the upstream repository.

Visit the URL, click the green "Install" button and select the repositories where you want to enable automatic synchronization.

The branch is updated once per hour directly on GitHub, on your local machine you need to pull the master branch to ensure that your local copy is in sync.

Also answered in https://stackoverflow.com/a/58965171/946850.

krlmlr
  • 25,056
  • 14
  • 120
  • 217
1

Another option is to use the Update from upstreamRepoName/master button in GitHub for Mac (and I assume GitHub for Windows).

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Steve Moser
  • 7,647
  • 5
  • 55
  • 94
0

I don't know how it can be done without adding another remote, however I always add the repo I forked from as the upstream remote so I could simply do:

git branch -a|grep remotes/upstream| while IFS="/" read p r b; do echo Syncing $r/$b to origin/$b; git push origin $r/$b:refs/heads/$b; done

This will sync all branches incl. creating new ones (remove the refs/heads/ to only update existing branches). If any of your branches has diverged it will throw an error for that branch.

Thomas Guyot-Sionnest
  • 2,251
  • 22
  • 17
0

I would simply use:

git pull git@github.com:someone/foobar <branch_name> && git push
cybergeek654
  • 290
  • 1
  • 5
  • 15
0
  1. Visit https://github.com/me/foobar/compare/master...someone:master (of course replacing me, someone and foobar with corresponding repo and user names)

  2. If you see green text Able to merge then press Create pull request

  3. On the next page, scroll to the bottom of the page and click Merge pull request and Confirm merge.

Ilyich
  • 4,966
  • 3
  • 39
  • 27