Thursday, March 12, 2015

How to keep a GitHub fork repository up to date?

You might come across many instances where you will have to fork a github repository and commit your changes as pull requests to the original master repository. If you are to commit changes every now and then , you need to make sure that your local repository is up to date with the master repository. Else if there are other commits made by users and you do not have the recent changes, you might get commit conflicts once you make a pull request. So to avoid this, you need to follow two steps.

1. Configure a remote pointing to the original repository.
2. Sync your local repository with original repo.

Configure a remote pointing to the original repository
You can configure a remote  like below.

1. Open the terminal, and go to your local repository location.Next type below command.
 You can replace content in red with the url of your original repository.

git remote add upstream

2. Next,you can verify that a remote upstream repository got added, by giving following command.
 git remote -v

  This command should list down the remote you added in previous step.

Sync your local repository with original repo
 After you completed creating a remote upstream, now you can update your local repository by giving following commands.

1. Below command will fetch all the changes/updates of your original repository.
  git fetch upstream

2. Next checkout your local master branch.
 git checkout master 

3. Next merge the changes of your upstream repo to your local repo. This will sync your forked local repository without giving up your local changes.
git merge upstream/master 

There can be situations where your local change commits will have to be overriden to get the updates. In such a case add below commands.

git fetch upstream
git merge  master


git pull upstream master 

No comments:

Post a Comment