Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a remote Git branch?

I created a local branch which I want to 'push' upstream. There is a similar question here on Stack Overflow on how to track a newly created remote branch.

However, my workflow is slightly different. First I want to create a local branch, and I will only push it upstream when I'm satisfied and want to share my branch.

  • How would I do that? (my google searches did not seem to come up with anything).
  • How would I tell my colleagues to pull it from the upstream repository?

UPDATE With Git 2.0 there is a simpler answer I have written below: https://stackoverflow.com/a/27185855/109305

like image 338
Jesper Rønn-Jensen Avatar asked Oct 05 '09 09:10

Jesper Rønn-Jensen


People also ask

How do I create a new remote branch in git?

Push a new Git branch to a remote repoClone the remote Git repo locally. Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch. Continue to perform Git commits locally on the new branch.

How do I add a remote branch to github?

Adding a remote repository To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at. The git remote add command takes two arguments: A remote name, for example, origin.

How do you create a new branch that tracks remote branches?

To create a new local branch based on a remote branch, use the "-track" option in the branch command. You can also do this by using the "checkout" command. If you want your local branch to have the same name as the remote branch, you only need to specify the name of the remote branch.

What is remote branch in git?

Git checkout remote branch is a way for a programmer to access the work of a colleague or collaborator for the purpose of review and collaboration. There is no actual command called “git checkout remote branch.” It's just a way of referring to the action of checking out a remote branch.


2 Answers

First, you create your branch locally:

git checkout -b <branch-name> # Create a new branch and check it out 

The remote branch is automatically created when you push it to the remote server. So when you feel ready for it, you can do:

git push <remote-name> <branch-name>  

Where <remote-name> is typically origin, the name which git gives to the remote you cloned from. Your colleagues would then just pull that branch, and it's automatically created locally.

Note however that formally, the format is:

git push <remote-name> <local-branch-name>:<remote-branch-name> 

But when you omit one, it assumes both branch names are the same. Having said this, as a word of caution, do not make the critical mistake of specifying only :<remote-branch-name> (with the colon), or the remote branch will be deleted!

So that a subsequent git pull will know what to do, you might instead want to use:

git push --set-upstream <remote-name> <local-branch-name>  

As described below, the --set-upstream option sets up an upstream branch:

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.

like image 110
Ikke Avatar answered Oct 06 '22 01:10

Ikke


First, you must create your branch locally

git checkout -b your_branch 

After that, you can work locally in your branch, when you are ready to share the branch, push it. The next command push the branch to the remote repository origin and tracks it

git push -u origin your_branch 

Teammates can reach your branch, by doing:

git fetch git checkout origin/your_branch 

You can continue working in the branch and pushing whenever you want without passing arguments to git push (argumentless git push will push the master to remote master, your_branch local to remote your_branch, etc...)

git push 

Teammates can push to your branch by doing commits and then push explicitly

... work ... git commit ... work ... git commit git push origin HEAD:refs/heads/your_branch 

Or tracking the branch to avoid the arguments to git push

git checkout --track -b your_branch origin/your_branch ... work ... git commit ... work ... git commit git push 
like image 43
dseminara Avatar answered Oct 06 '22 01:10

dseminara