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


    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 just 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.


    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
    

    Simple Git 2.0+ solution:

    As of Git 2.0 the behaviour has become simpler :

    You can configure git with push.default = current to make life easier:

    I added this so now I can just push a new branch upstream with

    $ git push -u
    

    -u will track remote branch of same name. No with this configuration you will auto-guess the remote reference to git push. From git.config documentation:

    push.default

    Defines the action git push should take if no refspec is explicitly given.

    push.default = current - push the current branch to update a branch with the same name on the receiving end. Works in both central and non-central workflows.

    For me, this is a good simplification of my day-to-day Git workflow. The configuration setting takes care of the 'usual' use case where you add a branch locally and want to create it remotely. Also, I can just as easily create local branches from remotes by just doing git co remote_branch_name (as opposed to using --set-upstream-to flag).

    I know this question and the accepted answers are rather old, but the behaviour has changed so that now configuration options exists to make your workflow simpler.

    To add to your global Git configuration, run this on the command line:

    $ git config --global push.default current
    
    链接地址: http://www.djcxy.com/p/78.html

    上一篇: 更改远程Git存储库的URI(URL)

    下一篇: 你如何创建一个远程Git分支?