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

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

    In Git 1.7.0 and later, you can checkout a new branch:

    git checkout -b <branch>
    

    Edit files, add and commit. Then push with the -u (short for --set-upstream) option:

    git push -u origin <branch>
    

    Git will set up the tracking information during the push.

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

    Prior to the introduction of git push -u, there was no git push option to obtain what you desire. You had to add new configuration statements.

    If you create a new branch using:

    $ git checkout -b branchB
    $ git push origin branchB:branchB
    

    You can use the git config command to avoid editing directly the .git/config file.

    $ git config branch.branchB.remote origin
    $ git config branch.branchB.merge refs/heads/branchB
    

    Or you can edit manually the .git/config file to had tracking information to this branch.

    [branch "branchB"]
        remote = origin
        merge = refs/heads/branchB
    
    0 讨论(0)
  • 2020-11-22 10:05

    I made an alias so that whenever I create a new branch, it will push and track the remote branch accordingly. I put following chunk into the .bash_profile file:

    # Create a new branch, push to origin and track that remote branch
    publishBranch() {
      git checkout -b $1
      git push -u origin $1
    }
    alias gcb=publishBranch
    

    Usage: just type gcb thuy/do-sth-kool with thuy/do-sth-kool is my new branch name.

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

    Simply put, to create a new local branch, do:

    git branch <branch-name>
    

    To push it to the remote repository, do:

    git push -u origin <branch-name>
    
    0 讨论(0)
  • 2020-11-22 10:11

    edit Outdated, just use git push -u origin $BRANCHNAME


    Use git publish-branch from William's miscellaneous Git tools (gitorious repo and clone).

    OK, no Ruby, so - ignoring the safeguards! - take the last three lines of the script and create a bash script, git-publish-branch:

    #!/bin/bash
    REMOTE=$1 # Rewrite this to make it optional...
    BRANCH=$2
    # Uncomment the following line to create BRANCH locally first
    #git checkout -b ${BRANCH}
    git push ${ORIGIN} ${BRANCH}:refs/heads/${BRANCH} &&
    git config branch.${BRANCH}.remote ${REMOTE} &&
    git config branch.${BRANCH}.merge refs/heads/${BRANCH}
    

    Then run git-publish-branch REMOTENAME BRANCHNAME, where REMOTENAME is usually origin (you may modify the script to take origin as default, etc...)

    0 讨论(0)
  • 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:

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