How do I push a new local branch to a remote Git repository and track it too?

后端 未结 15 1326
逝去的感伤
逝去的感伤 2020-11-22 09:17

I want to be able to do the following:

  1. Create a local branch based on some other (remote or local) branch (via git branch or git checkout

相关标签:
15条回答
  • 2020-11-22 10:15

    I suppose that you have already cloned a project like:

    git clone http://github.com/myproject.git
    
    1. Then in your local copy, create a new branch and check it out:

      git checkout -b <newbranch>
      
    2. Supposing that you made a "git bare --init" on your server and created the myapp.git, you should:

      git remote add origin ssh://example.com/var/git/myapp.git
      git push origin master
      
    3. After that, users should be able to

      git clone http://example.com/var/git/myapp.git
      

    NOTE: I'm assuming that you have your server up and running. If it isn't, it won't work. A good how-to is here.

    ADDED

    Add a remote branch:

    git push origin master:new_feature_name
    

    Check if everything is good (fetch origin and list remote branches):

    git fetch origin
    git branch -r
    

    Create a local branch and track the remote branch:

    git checkout -tb new_feature_name origin/new_feature_name
    

    Update everything:

    git pull
    
    0 讨论(0)
  • 2020-11-22 10:18

    For GitLab version prior to 1.7, use:

    git checkout -b name_branch
    

    (name_branch, ex: master)

    To push it to the remote repository, do:

    git push -u origin name_new_branch
    

    (name_new_branch, example: feature)

    0 讨论(0)
  • 2020-11-22 10:19

    If you are not sharing your repo with others, this is useful to push all your branches to the remote, and --set-upstream tracking correctly for you:

    git push --all -u
    

    (Not exactly what the OP was asking for, but this one-liner is pretty popular)

    If you are sharing your repo with others this isn't really good form as you will clog up the repo with all your dodgy experimental branches.

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