Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy a local Git branch to a remote repo

I've taken the following steps so far:

  1. Cloned a remote Git repo
  2. Branched the master branch to an experimental
  3. edited/tested/committed code in the experimental branch

Now, I'm not ready to merge experimental into master. I do however want to push it back to the remote repo as that's the repository I share with a few colleagues. I'd like for them to see what I've done in the experimental branch. I typically just access the remote repo via SSH.

How do I share my local branch on the remote repo, without affecting the remote repo's master branch?

like image 712
Coocoo4Cocoa Avatar asked Apr 30 '09 03:04

Coocoo4Cocoa


People also ask

How do I copy a branch to another repo?

Simply add the new remote (Organization) to your old repository (master). Once you did it simply push the branch A to the new (organization) repository. Now you should have the new branch A in your new repository. The point is to add new remote and to push the branch to your new repository.

How do I point a local branch to my remote?

When you're publishing a local branch. You can tell Git to track the newly created remote branch simply by using the -u flag with "git push".


3 Answers

According to git push manual page:

 git push origin experimental

Find a ref that matches experimental in the source repository (most likely, it would find refs/heads/experimental), and update the same ref (e.g. refs/heads/experimental) in origin repository with it.
If experimental did not exist remotely, it would be created.

This is the same as:

git push origin experimental:refs/heads/experimental

Create the branch experimental in the origin repository by copying the current experimental branch.
This form is only needed to create a new branch or tag in the remote repository when the local name and the remote name are different; otherwise, the ref name on its own will work.

Or, like mentioned in git tip, you can set up a "Branch’s Default Remote":

You can use git config to assign a default remote to a given branch. This default remote will be used to push that branch unless otherwise specified.

This is already done for you when you use git clone, allowing you to use git push without any arguments to push the local master branch to update the origin repository’s master branch.

git config branch.<name>.remote <remote> 

can be used to specify this manually.


Jan suggests (for git >= 1.7.0) the push -u (or push --set-upstream) option:

For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands.

That way, you don't have to do any git config.

git push -u origin experimental
like image 101
VonC Avatar answered Oct 12 '22 02:10

VonC


If the name of your branch is experimental, and the name of the remote is origin, then it's

git push origin experimental
like image 21
John Douthat Avatar answered Oct 12 '22 04:10

John Douthat


git push -u <remote-name> <branch-name> doesn't work if the newly created branch isn't spawned from the same repo, i.e. if you haven't created the new branch using git checkout -b new_branch, then this will not work.

For eg, I had cloned two different repositories locally and I had to copy repo2/branch1 to repo1/ and then push it too.

This link helped me push my local branch (cloned from another repo) to my remote repo:

like image 29
brokenfoot Avatar answered Oct 12 '22 04:10

brokenfoot