首页 文章

如何将新的本地分支推送到远程Git存储库并进行跟踪?

提问于
浏览
3802

我希望能够做到以下几点:

  • 基于其他(远程或本地)分支创建本地分支(通过 git branchgit checkout -b

  • 将本地分支推送到远程存储库(发布),但使其可跟踪,以便 git pullgit push 立即生效 .

我怎么做?

我在Git 1.7中了解 --set-upstream ,但这是一个创作后的动作 . 我想找到一种方法来在将分支推送到远程存储库时进行类似的更改 .

13 回答

  • 29

    简单地说,要创建一个新的 local 分支,请执行以下操作:

    git branch <branch-name>
    

    要将其推送到 remote 存储库,请执行以下操作:

    git push -u origin <branch-name>
    
  • 28

    对于1.7之前的GitLab版本,请使用:

    git checkout -b name_branch

    (name_branch,ex:master)

    要将其推送到远程存储库,请执行以下操作:

    git push -u origin name_new_branch

    (name_new_branch,示例:功能)

  • -7

    要上传公共存储库的本地分支,需要 cd 到公共存储库,然后使用以下代码:

    git push -u origin branchname
    
  • 5975

    这里给出的解决方案略有不同:

    • 基于其他(远程或本地)分支创建本地分支:
    git checkout -b branchname
    
    • 将本地分支推送到远程存储库(发布),但使其可跟踪,以便 git pullgit push 立即生效
    git push -u origin HEAD
    

    使用 HEAD 是"handy way to push the current branch to the same name on the remote" . 来源:https://git-scm.com/docs/git-push在Git术语中,HEAD(大写)是对当前分支(树)顶部的引用 .

    -u 选项只是 --set-setupstream 的缩写 . 这将为当前分支添加上游跟踪参考 . 您可以通过查看.git / config文件来验证这一点:

  • 18

    我想你已经克隆了一个项目,如:

    git clone http://github.com/myproject.git
    
    • 然后在本地副本中创建一个新分支并将其签出:
    git checkout -b <newbranch>
    
    • 假设您在服务器上创建了“git bare --init”并创建了myapp.git,您应该:
    git remote add origin ssh://example.com/var/git/myapp.git
    git push origin master
    
    • 之后,用户应该能够
    git clone http://example.com/var/git/myapp.git
    

    NOTE: 我'm assuming that you have your server up and running. If it isn' t,它不起作用 . 一个好的方法是here .

    已添加

    添加远程分支:

    git push origin master:new_feature_name
    

    检查一切是否正常(获取原点并列出远程分支):

    git fetch origin
    git branch -r
    

    创建本地分支并跟踪远程分支:

    git checkout -tb new_feature_name origin/new_feature_name
    

    更新一切:

    git pull
    
  • 8

    稍微根据这里的答案构建,我将这个过程包装成一个简单的Bash脚本,当然也可以用作Git别名 .

    对我来说重要的补充是,这提示我在提交之前运行单元测试,默认情况下传入当前分支名称 .

    $ git_push_new_branch.sh
    
      Have you run your unit tests yet? If so, pass OK or a branch name, and try again
    
      usage: git_push_new_branch {OK|BRANCH_NAME}
    
      e.g.
    
      git_push_new_branch           -> Displays prompt reminding you to run unit tests
      git_push_new_branch OK        -> Pushes the current branch as a new branch to the origin
      git_push_new_branch MYBRANCH  -> Pushes branch MYBRANCH as a new branch to the origin
    

    git_push_new_branch.sh

    function show_help()
    {
      IT=$(CAT <<EOF
    
      Have you run your unit tests yet? If so, pass OK or a branch name, and try again
    
      usage: git_push_new_branch {OK|BRANCH_NAME}
    
      e.g.
    
      git_push_new_branch.sh           -> Displays prompt reminding you to run unit tests
      git_push_new_branch.sh OK        -> Pushes the current branch as a new branch to the origin
      git_push_new_branch.sh MYBRANCH  -> Pushes branch MYBRANCH as a new branch to the origin
    
      )
      echo "$IT"
      exit
    }
    
    if [ -z "$1" ]
    then
      show_help
    fi
    
    CURR_BRANCH=$(git rev-parse --abbrev-ref HEAD)
    if [ "$1" == "OK" ]
    then
      BRANCH=$CURR_BRANCH
    else
      BRANCH=${1:-$CURR_BRANCH}
    fi
    
    git push -u origin $BRANCH
    
  • 2

    我创建了一个别名,这样每当我创建一个新分支时,它都会相应地推送和跟踪远程分支 . 我将以下块放入 .bash_profile 文件中:

    # 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 :只需输入 gcb thuy/do-sth-koolthuy/do-sth-kool 是我的新分支名称 .

  • 135

    edit 已过时,只需使用 git push -u origin $BRANCHNAME


    William's miscellaneous Git toolsgitorious repoclone)使用 git publish-branch .

    好的,没有Ruby,所以 - 忽略了保护措施! - 获取脚本的最后三行并创建一个bash脚本, 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}
    

    然后运行 git-publish-branch REMOTENAME BRANCHNAME ,其中REMOTENAME通常是原点(您可以修改脚本以将原点作为默认值等等)

  • 21

    如果您不与他人共享您的仓库,将 all 您的分支机构推送到远程,以及 --set-upstream 正确跟踪您将非常有用:

    git push --all -u
    

    (不完全是OP的要求,但这个单线很受欢迎)

    如果你与其他人分享你的回购,这不是一个很好的形式,因为你会用你所有狡猾的实验分支堵塞回购 .

  • 8

    我只是这样做

    git push -u origin localBranch:remoteBranchToBeCreated
    

    在已经克隆的项目上 .

    Git在 localBranch 的提交下创建了一个名为 remoteBranchToBeCreated 的新分支 .

  • 114

    通过从现有分支分支来创建新分支

    git checkout -b <new_branch>

    然后使用将此新分支推送到存储库

    git push -u origin <new_branch>

    这会创建并将所有本地提交推送到新创建的远程分支 origin/<new_branch>

  • 66

    在Git 1.7.0及更高版本中,您可以签出一个新分支:

    git checkout -b <branch>
    

    编辑文件,添加和提交 . 那么push with the -u (short for --set-upstream)选项:

    git push -u origin <branch>
    

    Git将在推送期间设置跟踪信息 .

  • 460

    在引入 git push -u 之前,没有 git push 选项来获得您想要的东西 . 您必须添加新的配置语句 .

    如果您使用以下方法创建新分支:

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

    您可以使用 git config 命令避免直接编辑 .git/config 文件 .

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

    或者,您可以手动编辑 .git/config 文件以获得此分支的跟踪信息 .

    [branch "branchB"]
        remote = origin
        merge = refs/heads/branchB
    

相关问题