How do you create a remote Git branch?

后端 未结 23 1888
感情败类
感情败类 2020-11-22 13:31

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.

Howeve

相关标签:
23条回答
  • 2020-11-22 13:54

    Now with git, you can just type, when you are in the correct branch

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

    and git create for you the origin branch.

    0 讨论(0)
  • 2020-11-22 13:58

    I have used two ways to create branch

    If you are using TortoiseGit follow these steps:-

    1.Create Branch using TortoiseGit

    Right click on your project >>> TortoiseGit >>> Create Branch >>> write the name of branch and select the base branch then press ok

    2.Push the branch

    Right click on your project >>> TortoiseGit >>> push >>> click ok

    3.Switch to new branch

    Right click on your project >>> TortoiseGit >>> Switch/Checkout >>> select newly created branch and press ok

    If you are using command prompt follow these steps:-

    1.Create branch using command prompt

    $git checkout -b new_branch_name

    2.Push the branch

    $git push origin new_branch_name

    3.Switch to new branch it will already switched to new_branch_name otherwise you can use

    $git checkout new_branch_name

    0 讨论(0)
  • 2020-11-22 14:01

    Heres an example I only have two branches that were local first: origin and mobile-test.

    Nothing for me worked until I used this in command line to actually show my updated files in a remote branch.

    git push --set-upstream origin mobile-test
    
    0 讨论(0)
  • 2020-11-22 14:02

    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.

    0 讨论(0)
  • 2020-11-22 14:04

    Simple Git 2.0+ solution:

    As of Git 2.0, the behavior 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 the same name. Now 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 behavior has changed so that now configuration options exist to make your workflow simpler.

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

    $ git config --global push.default current
    
    0 讨论(0)
  • 2020-11-22 14:05

    [Quick Answer]

    You can do it in 2 steps:

    1. Use the checkout for create the local branch:

    git checkout -b yourBranchName
    

    2. Use the push command to autocreate the branch and send the code to the remote repository:

    git push -u origin yourBranchName
    

    There are multiple ways to do this but I think that this way is really simple.

    0 讨论(0)
提交回复
热议问题