AnchovyLegend
AnchovyLegend

Reputation: 12538

Two local branches being tracked by two different remote repos

I am currently working on a project where I had to branch off of master, make a bunch of changes locally and 'go-live' with a testing environment that is pushed to a particular remote repo, as follows:

git push origin site-testing 

pushes to site-testing branch in the following repo:

[email protected]:/path/to/root-dir-containing-.git

Now I need to cherry pick changes from the site-testing branch on to the master branch.

The catch is, when I git push origin master, I would like these changes to be pushed to a totally different remote repo.

So the desired outcome is:

git push origin master 

pushes master to the following repo:

[email protected]:/path/to/root-dir-containing-.git

and not master on testing.com..

Is this possible? I appreciate any suggestions

Upvotes: 1

Views: 44

Answers (2)

torek
torek

Reputation: 489998

You can have more than one remote. This lets you do what you want.

The general syntax for using git push is:

git push remote branch

which you will often see, e.g., as git push origin master. The origin part is the name of a remote and the second part is a branch name (well, it's actually more complicated, but let's just go with "branch name" here :-) ).

Remember that a remote in Git is just a short name for another Git repository at some other URL. Typically, a Git repository comes with one remote, named origin, pre-made, because git clone sets up one remote for you, and the name it uses by default is origin.

To add another, different, remote, run git remote add, e.g.:

git remote add xyzzy ssh://[email protected]/path/to/repo.git

Now xyzzy is the name of a second remote, that refers to a Git repository on testing.com, accessed via ssh [email protected], living in path/to/repo.git on that system. You can now git push xyzzy branch.

But wait, there's more!

Each branch may have one (and only one) upstream setting. The upstream of a branch is actually a pair of items: it's the name of the remote, plus a branch-name. This pair of items is also what your Git uses to remember that branch as seen on that remote the last time your Git actually conversed with that other Git.

Hence, if you have a branch plugh-test that should normally push to remote xyzzy, you can do:

git branch --set-upstream-to=xyzzy/plugh-test plugh-test

Now your branch plugh-test remembers that its upstream is xyzzy/plugh-test, which is your own Git's memory of plugh-test as seen on remote xyzzy.

If your push.default setting is simple (as it is by default in Git 2.0 or later), and you are on branch plugh-test, you can just run:

git push

because your Git will look up your current branch's remote, and use that as the default remote argument, when pushing the current branch, which is the default branch argument.

Note, though, that if you accidentally run:

git push colossal-cave

thinking that this will push the branch colossal-cave, it won't: the positional argument here is always a remote, not a branch.

(xyzzy and plugh are magic verbs from Colossal Cave Adventure.)

Upvotes: 2

Arpit Aggarwal
Arpit Aggarwal

Reputation: 29316

You can add another origin and push to it, as follows:

git remote add origin1 https://aws-live-domain.com/user-name/<path/to/root-dir-containing->.git
git push origin1 master 

Upvotes: 3

Related Questions