339

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
8
  • 29
    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
    Commented Dec 7, 2014 at 22:14
  • Possible duplicate of github, update forked project
    – mpromonet
    Commented Jan 17, 2016 at 16:25
  • 3
    github.com/isaacs/github/issues/121 Commented Jan 3, 2017 at 6:45
  • Possible duplicate of How do I update a GitHub forked repository? Commented Jun 16, 2018 at 21:45
  • 3
    The new answer for May 2021.
    – iBug
    Commented May 7, 2021 at 2:10

9 Answers 9

669

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

20
  • 60
    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
    Commented Apr 15, 2014 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
    Commented Aug 6, 2014 at 6:55
  • 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. Commented Oct 23, 2014 at 14:04
  • 29
    Is there no way of getting rid of that ridiculous merge commit?
    – kumarharsh
    Commented Jan 12, 2015 at 12:22
  • 21
    @kumar_harsh since September 2016, there now is a "rebase and merge" option for pull requests that avoids the merge commit.
    – waldyrious
    Commented Sep 13, 2017 at 9:58
51
git remote set-url origin [email protected]:someone/foobar
git remote set-url origin --push [email protected]: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 [email protected]:me/foobar, which removes the caveat.
The choice is yours as to which is easier to remember.

1
  • 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
    Commented Feb 18, 2021 at 6: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.

1
  • This should be a lot more upvoated as it is the most straightforward way of doing this today.
    – DimP
    Commented Jun 9, 2022 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
1
  • 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? Commented Feb 11, 2015 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.

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

4
  • TIL: that there are apps for GitHub beyond the web interface :-) [and it's a shame that the experience across them isn't unified] Commented Jun 26, 2017 at 7:17
  • 1
    I don't see this in Windows
    – Denis
    Commented Mar 6, 2019 at 15:50
  • 1
    @Denis this is their old app. I'm not sure their new app has it. Commented Mar 6, 2019 at 16:25
  • Aha. This worked in "GitHub Desktop" (for Mac, not sure about Windows).
    – michaelok
    Commented Sep 9, 2019 at 3:44
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.

0

I would simply use:

git pull [email protected]:someone/foobar <branch_name> && git push
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.

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