比尔盖子
比尔盖子

Reputation: 3617

Keep sync with another git repository?

I forked a repository on Github. Now, the original had some update. How can I pull the changes from the original repository and keep sync with them?

And if my source is conflict with the original source, do I have a chance to edit it manually?

EDIT1: Thanks everyone's help. I think I need to RTFM :)

Upvotes: 9

Views: 13498

Answers (4)

King'ori Maina
King'ori Maina

Reputation: 4507

See github documention on "Syncing A Fork"

https://help.github.com/articles/syncing-a-fork

  1. Before you can sync, you need to add a remote that points to the upstream repository. You may have done this when you originally forked.
  2. There are two steps required to sync your repository with the upstream: first you must fetch from the remote, then you must merge the desired branch into your local branch.
  3. Fetching from the remote repository will bring in its branches and their respective commits. These are stored in your local repository unders special branches. We now have the upstream's master branch stored in a local branch, upstream/master
  4. Now that we have fetched the upstream repository, we want to merge its changes into our local branch. This will bring that branch into sync with the upstream, without losing our local changes. If your local branch didn't have any unique commits, git will instead perform a "fast-forward"

Ps: Syncing your fork only updates your local copy of the repository; it does not update your repository on GitHub.

Ps 2: The actual code for each step is at the link pasted in the beginning.

Upvotes: 1

Jonathan Wakely
Jonathan Wakely

Reputation: 171253

What's not stated explicitly in the other two answers is that you can't merge directly from the original project on github to your fork on github, you need to go via a local clone on your own machine.

So you set up the original project as a remote (called upstream in Magnus Skog and CJlano's answers), then pull from that remote into your local clone, resolving any merge conflicts with your local changes, then push the result to your fork on github.

Upvotes: 9

ralphtheninja
ralphtheninja

Reputation: 132978

Just add the original repository as a remote and pull changes from it

git remote add upstream path/to/upstream/repo.git
git pull upstream master

Upvotes: 8

CJlano
CJlano

Reputation: 1572

See: https://help.github.com/articles/fork-a-repo

Pull in upstream changes

If the original repo you forked your project from gets updated, you can add those updates to your fork by running the following code:

git fetch upstream
# Fetches any new changes from the original repo
git merge upstream/master
# Merges any changes fetched into your working files

Upvotes: 6

Related Questions