35

I wanted to commit somthing to a github repository, but I (obviously) didn't have any rights to do so. I made a fork of that repo, commited my changes and submitted a pull-request. Now, the problem is that after a while other people have made commits to the original repo, which means my fork is no longer up-to-date.

How should now update my fork? Is this (https://stackoverflow.com/a/23853061/5513628) still a valid way or do I have to delete my repo and make a new fork every time?

This is what the fork looks like in github desktop:

enter image description here

The pull request was made by me but the two commits after that were made by other people. They are not contained in my repo...

Community
  • 1
  • 1
MyNameIsHans
  • 624
  • 1
  • 7
  • 15
  • 3
    Possible duplicate of [How do I update a GitHub forked repository?](http://stackoverflow.com/questions/7244321/how-do-i-update-a-github-forked-repository) – Tumbler41 Feb 17 '17 at 21:16
  • I made [fork-sync](https://github.com/imkarrer/fork-sync) to keep forked branches up to date across multiple repos. – ferahgo Dec 14 '18 at 00:14

4 Answers4

46

To sync changes you make in a fork with the original repository, you must configure a remote that points to the upstream repository in Git.

Specify a new remote upstream repository that will be synced with the fork.

git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

You can check if it was succesful with:

git remote -v

Then fetch it to update your project:

git fetch upstream

Merge the changes from upstream/master into your local master branch.

git merge upstream/master

At last, you can commit new update from original repository to your fork repository.

Shortcut: you can also combine the last two commands into a single command:

git fetch upstream
git merge upstream/master

Is equal to:

git pull upstream/master

This information can also be found on GitHub here and here.

Kostas Minaidis
  • 4,681
  • 3
  • 17
  • 25
Diki Ananta
  • 630
  • 8
  • 16
  • My local repo still stays the same. I edited my post and added an image to clarify my situation. – MyNameIsHans Oct 04 '16 at 18:36
  • Is `git rebase upstream/master` a good option for merging as well? – zyy Oct 29 '19 at 02:36
  • Is this same as pull request? – variable Nov 25 '21 at 09:51
  • @variable `git pull` can internally do either rebase or merge and hence its needs to be decided whether you prefer merge or rebase. for detailed answer read post https://stackoverflow.com/a/44476803/1738222 – Krishna Oza Jul 08 '22 at 14:25
  • 1
    @zyy yes `git rebase` is a good option however since keeping the fork upto date has various methods one should be aware of what they are trying and adopting. refer https://stackoverflow.com/a/44476803/1738222 for detailed write up on the same. – Krishna Oza Jul 08 '22 at 14:26
8

To sync changes you make in a fork with the original repository, you must configure a remote that points to the upstream repository in Git.

Not anymore, since May 2021:

Sync an out of date branch of a fork from the web

You can now use the web UI to synchronize an out of date branch of a fork with its upstream branch. If there are no merge conflicts between the branches, the fork's branch is updated either by fast-forwarding or by merging from the upstream's branch. If there are conflicts, you will be prompted to open a pull request to resolve.

Fetch upstream up to date, no conflict, and conflict flows

From GitHub:

Go to your fork, click on Fetch upstream and then click on Fetch and merge to directly sync your fork with its parent repo.

Compare and sync

You may also click on the Compare button to compare the changes before merging.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Hey, Github allows searches for specific commits... so redacting the repo name and most recent commit is useless. There isn't anything wrong with the repo name anyway. But, I can understand if you didn't want curious & mildly nosy people snooping around, or if it was just outside the scope of the answer... – Gpine185 Aug 12 '23 at 19:13
  • @Gpine185 True. I do not remember now why those fields were redacted. – VonC Aug 12 '23 at 19:30
3

Adding to Diki Andriansyah answer, rather than using

git merge upstream/master

try using:

git pull upstream master

This helped me :) .

Yathartha Joshi
  • 716
  • 1
  • 14
  • 29
0

I had the same problem and made a docker container that will update the forked branch I care about.

Try using fork-sync

The README walks you through how to configure a docker image that will clone, setup a remote to track upstream, fast forward the desired branch, and finally push the changes to your fork. The same container can be started over and over again to keep the branch up to date. I set my docker container to run on a crontab so that every two hours my workstation is updating the forks I regularly contribute to.

I wanted to be able to make branches which tracked my fork from the tip of upstream. This could have been accomplished with git checkout -b mybranch upstream/master. But this by default sets upstream as the remote, which means if I push my branch will go to the upstream repo, and possible trigger CI jobs. Now, I can do a git checkout -b mybranch origin/master and not have to worry about a dangerous push waiting for me later.

ferahgo
  • 408
  • 1
  • 4
  • 11