How do you create a remote Git branch?

后端 未结 23 1891
感情败类
感情败类 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 14:17

    I've solved this by adding this into my bash ~/.profile:

    function gitb() { git checkout -b $1 && git push --set-upstream origin $1; }

    Then to start up a new local + remote branch, I write:

    gitb feature/mynewbranch

    This creates the branch and does the first push, not just to setup tracking (so that later git pull and git push work without extra arguments), but actually confirming that the target repo doesn't already have such branch in it.

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

    If you have used --single-branch to clone the current branch, use this to create a new branch from the current:

    git checkout -b <new-branch-name>
    git push -u origin <new-branch-name>
    git remote set-branches origin --add <new-branch-name>
    git fetch
    
    0 讨论(0)
  • 2020-11-22 14:19

    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
    
    0 讨论(0)
  • 2020-11-22 14:20

    First you create the branch locally:

    git checkout -b your_branch
    

    And then to create the branch remotely:

    git push --set-upstream origin your_branch
    

    Note: This works on the latests versions of git:

    $ git --version
    git version 2.3.0
    

    Cheers!

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

    Creating a local branch from an existing branch (can be master/ develop/ any-other-branch).

    git checkout -b branch_name

    Push this to remote

    git push -u remote_name local_branch_name:remote_branch_name

    Here,

    1. -u : sets the upstream branch
    2. remote_name : git sets the name by default to be "origin" when it creates the repository. This can however be changed to a different arbitrary name.
    3. local_branch_name : is the name of the local branch to be pushed.
    4. remote_branch_name : is the name of the remote branch that we want to be created on remote.

    If we remove the local and remote branch names, it will have the format

    git push -u remote_name branch_name

    This will push the local branch to remote and with the same name as the local branch branch_name. The local branch will be tracking the remote branch as well.

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