Home | Edit | New

Guides: Keeping a git fork in sync with the forked repo feed

Suppose you fork http://github.com/wycats/merb-core/tree/master by clicking on the “fork” button at the top of that page.

(Image from the Rails on the Run tutorial)

That copies the repository to your github account, with username username (for example). Once you’ve provided your SSH key, you can check out this remote branch to a directory on your local computer with git’s clone command:
git clone git@github.com:username/merb-core.git
The above command copies the repository to the directory in which it is run with the following remote reference in your .git/config file1:
[remote "origin"]
    url = git@github.com:username/merb-core.git
    fetch = +refs/heads/*:refs/remotes/origin/*
To also be able to pull in updates from wycats/merb-core, add it to your remote references:
git remote add wycats git://github.com/wycats/merb-core.git
The above command adds the following remote reference to your .git/config file1:
[remote "wycats"]
    url = git://github.com/wycats/merb-core.git
    fetch = +refs/heads/*:refs/remotes/wycats/*

Manually Pull from Each Repository

Once the remote branch is created (You only have to do this once.), you can pull in new changes:
git checkout master
git pull wycats master
If you don’t wish for an automatic merge to happen, you can instead fetch and merge:
git checkout master
git fetch wycats
git merge wycats/master

Shortcut: Pull from Both Repositories in One Command

NB: I’m not sure why, but for this shortcut to work for me, I had to first pull manually once (as instructed above).

To make things easier, you can create an alias that fetches from origin and wycats and octopus merges them with the current branch on your local machine. Paste the following in your .git/config file1:

[alias]
    pu = !"git fetch origin -v; git fetch wycats -v; git merge wycats/master" 
You can delete both -v’s, which stand for verbose, if you don’t care for git to explain what’s happening in detail. Now, all you have to type to stay completely up to date is:
git pu

1 To get a better understanding of git configuration files, go to the git-config(1) Manual Page, search for “syntax”, and read the first few paragraphs of the “Configuration File” section. Then, skim the “Variables” section to get a sense of what variables you can configure.

Last edited by andymitchell, about 1 month ago
Versions: