Graeme
Graeme

Reputation: 4602

Cloning a single branch to differently named branch in git

I have a repo on GitHub called foo with the following branches:

I have a second repo on GitHub called <username>.github.com (a user repo) with the following branches:

I would like to use the code in foo->master in <username>.github.com->source and be able to merge in changes from foo->master when they occur.

What is the best means of achieving this?

Upvotes: 1

Views: 85

Answers (2)

JonnyJD
JonnyJD

Reputation: 2643

You can just add foo/master as a remote, merge it in from time to time (do this in <username>.github.com repo):

# add foo as remote (once)
git remote add foo https://github.com/<username>/foo.git
# fetch from remote (often)
git fetch foo
# merge changes to source (when you want)
git checkout source
git merge foo/master

You can set origin/source as remote for source and origin/master as remote for master as normal.

You can also use git fetch --all to update all remotes, including origin and foo.

If you don't change much in source, then the merge will almost always be fast-forward.


You might want to set a remote url for foo that is read-only. This way you can't push from <username>.github.com by accident and if it's a public repo, you won't need authentication (or password for your ssh key) for the fetch.


You can keep git from fetching the gh-pages branch with

git config remote.foo.fetch "+/refs/heads/master:/refs/remotes/foo/master"
git branch -rD foo/gh-pages

You just have to remember setting that when you expect any other branches than master to appear from foo remote.

A good explanation of the refspec is in the git book

Upvotes: 4

ShadyKiller
ShadyKiller

Reputation: 710

Maybe following will help(I am not 100% sure of this though):

# Add a remote foo
git remote add foo <foor-url>

# create a foo-master branch
git checkout -b foo-master

# Set upstream branch for foo-master
git branch --set-upstream foo-master foo/master

# fetch code from foo master 
git fetch foo master

# reset the current branch to foo master
git reset --hard foo/master

# merge in source
git checkout source
git merge foo-master

To update foo-master:

git checkout foo-master
git pull

Upvotes: 0

Related Questions