Skip to content

Instantly share code, notes, and snippets.

@kpsychas
Last active October 9, 2015 00:16
Show Gist options
  • Save kpsychas/357adc86069472f939eb to your computer and use it in GitHub Desktop.
Save kpsychas/357adc86069472f939eb to your computer and use it in GitHub Desktop.

Working with 2 remotes

This tutorial will show how to setup a local repository for working with 2 remote repositories. The assumption is that we want to pull and push certain features to a public repository, while having a private repository for independent development, that will only merge changes from the public.

Setting up public repository

First clone the public repository and move into the project folder.

git clone https://github.com/example/example.git cd example

Next we may need to work on an existing branch of the repository e.g b1 so we need to switch to it.

git checkout b1

This operation basically fetches loads the latest revision of remote branch and creates a local branch with the same name. That means if someone merges this branch into master the local branch will still be branch1 but it will track the remote master.

Setting up private repository

First we create a new branch, let's call it private_master

git checkout -b private_master

which is shorthand for

git branch private_master git checkout private_master

If the repository exists already one should first merge it in the new branch

git remote add --track master private git://bitbucket.org/example/example.git git pull private master

which is shortcut for

git fetch private git merge private/master

Useful operations

Merge changes from b1 branch (when in private_master)

git merge b1

If you merge changes in the wrong branch e.g in master instead of b1, assuming you are in master

git reset --hard origin/master

and more generally

git reset --hard remote_name/remote_branch

See details about the branches that are tracked from a specific remote and their status

git remote show remote_name

Avoid pushing accidentally to a remote if you only need it for pulling by setting the remote url to a dummy value

git remote set-url --push remote_name nopush

list all branches including remote branches with information about latest commit

git branch -a -v

push to a specific branch

git push remote_branch remote_name

or in configuration file add

[remote “remote_branch”]
     push = local_name:remote_name
@kpsychas
Copy link
Author

kpsychas commented Oct 9, 2015

Improvements and/or corrections are welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment