ChatGPT解决这个技术问题 Extra ChatGPT

Pull new updates from original GitHub repository into forked GitHub repository

I forked someone's repository on GitHub and would like to update my version with commits and updates made in the original repository. These were made after I forked my copy.

How can I pull in the changes that were made in the origin and incorporate them into my repository?

Possible duplicate, or maybe just related: Merging between forks in GitHub.
In case there are additional tags you may want to sync, do git push --force origin --tags after the proposed solutions!
Very old but still: the question is unclear, don't know if @why was asking about pulling in changes from the primary repo to the fork via the GitHub web UI, or pulling in the changes via the git command line locally.
@chrisinmtown Not sure either, but in the end, you had (at the time) to use a local repository, fetch from upstream, push to origin (your fork), as I mention in my 2010 answer below. The final result is "my repository" updated with changes made from the original repository. Nowadays, you can do it through pull-request on github.com itself.

V
VonC

You have to add the original repository (the one you forked) as a remote.

From the GitHub documentation on forking a repository:

https://i.stack.imgur.com/ztqaV.jpg

Once the clone is complete your repo will have a remote named “origin” that points to your fork on GitHub. Don’t let the name confuse you, this does not point to the original repo you forked from. To help you keep track of that repo we will add another remote named “upstream”: $ cd PROJECT_NAME $ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git $ git fetch upstream # then: (like "git pull" which is fetch + merge) $ git merge upstream/master master # or, better, replay your local work on top of the fetched branch # like a "git pull --rebase" $ git rebase upstream/master

There's also a command-line tool (gh) which can facilitate the operations above.

Here's a visual of how it works:

https://i.stack.imgur.com/cEJjT.png

See also "Are Git forks actually Git clones?".


@syedrakib I prefer a git rebase upstream/master, but I have added the two possibilities in the answer.
@PaBLoX if you have forked a repo, you are working on your repo, in your branch: rebase and force a push: no mess involved. Even a pull request in progress would be correctly updated.
@PaBLoX you don't create a mess: you git push --force, replacing the history of your branch on GitHub by your local branch you just rebased. Since only you is using sad branch, no mess is involved.
I understand. I still think it's hard, nontrivial and non intuitive. Still it's weird that my changes will be always on top (last), while actually they were made before. The solution I posted before looks better (still nontrivial too). The problem is that commit hashes changes (obviously, since there's a new parent) and generates a lot of noise inside github when issues are being called. Still it surprise me that there isn't a way to stay update with upstream and manage your own fork without creating pointless merge commits or "lie" about the history.
B
Boris Däppen

In addition to VonC's answer, you could tweak it to your liking even further.

After fetching from the remote branch, you would still have to merge the commits. I would replace

$ git fetch upstream

with

$ git pull upstream master

since git pull is essentially git fetch + git merge.


What if I know that upstream branch doesn't have any changes to existing files, but only few resource files added - do I still need merge?
Surely it will just do a fast-forward in that case
how does one make upstream master overwrite all local files (so no merge conflicts) upstream master is leading in code in this case so we trust it 100% ... have managed to do this
@snh_nl git rebase upstream master Note that this is not conflict-free if you have sufficiently diverged from upstream/master. See git-scm.com/docs/git-rebase (tl;dr: this hard resets your local master to that of upstream, and then tries to remerge all of the local commits from the point of divergence forward)
H
Hussain

Use:

git remote add upstream ORIGINAL_REPOSITORY_URL

This will set your upstream to the repository you forked from. Then do this:

git fetch upstream      

This will fetch all the branches including master from the original repository.

Merge this data in your local master branch:

git merge upstream/master

Push the changes to your forked repository i.e. to origin:

git push origin master

Voila! You are done with the syncing the original repository.


how does one make upstream master overwrite all local files (so no merge conflicts) upstream master is leading in code in this case so we trust it 100% ... have managed to do this
C
Cody Gray

This video shows how to update a fork directly from GitHub

Steps:

Open your fork on GitHub. Click on Pull Requests. Click on New Pull Request. By default, GitHub will compare the original with your fork, and there shouldn’t be anything to compare if you didn’t make any changes. Click on switching the base. Now GitHub will compare your fork with the original, and you should see all the latest changes. Click on Create a pull request for this comparison and assign a predictable name to your pull request (e.g., Update from original). Click on Create pull request. Scroll down and click Merge pull request and finally Confirm merge. If your fork didn’t have any changes, you will be able to merge it automatically.


Unfortunately, this nice graphical method creates added noise in your fork as mentioned above in the comments for the accepted answer. Therefore the command-line method is recommended: help.github.com/articles/syncing-a-fork
I could not find the switching the base option
The Github web UI in Sep 2020 has a button "Compare & Pull Request" (where previously there were separate buttons). Now it has a link "compare across branches" that I had to use. So a fetch-and merge (i.e., pull) action to get updates from master into fork can be done but not by these instructions. And even tho it's a fast forward, it clutters the history.
V
Vlad L.

If you want to do it without cli, you can do it fully on the Github website.

Go to your fork repository. Click on New pull request. Make sure to set your fork as the base repository, and the original (upstream) repository as a head repository. Usually, you only want to sync the master branch. Create a new pull request. Select the arrow to the right of the merging button, and make sure choose to rebase instead of merge. Then click the button. This way, it will not produce unnecessary merge commit. Done.


P
Peter Mortensen

If you're using the GitHub desktop application, there is a synchronise button on the top right corner. Click on it then Update from <original repo> near top left.

If there are no changes to be synchronised, this will be inactive.

Here are some screenshots to make this easy.


C
Chan

If there is nothing to lose you could also just delete your fork just go to settings... go to danger zone section below and click delete repository. It will ask you to input the repository name and your password after. After that you just fork the original again.


S
Saurabh P Bhandari

To automatically sync your forked repository with the parent repository, you could use the Pull App on GitHub.

Refer to the Readme for more details.

For advanced setup where you want to preserve your changes done to the forked repository, refer to my answer on a similar question here.