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

后端 未结 15 1295
逝去的感伤
逝去的感伤 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:13

    A slight variation of the solutions already given here:

    1. Create a local branch based on some other (remote or local) branch:

      git checkout -b branchname
      
    2. Push the local branch to the remote repository (publish), but make it trackable so git pull and git push will work immediately

      git push -u origin HEAD
      

      Using HEAD is a "handy way to push the current branch to the same name on the remote". Source: https://git-scm.com/docs/git-push In Git terms, HEAD (in uppercase) is a reference to the top of the current branch (tree).

      The -u option is just short for --set-upstream. This will add an upstream tracking reference for the current branch. you can verify this by looking in your .git/config file:

提交回复
热议问题