【问题标题】:How do I push a new local branch to a remote Git repository and track it too?如何将新的本地分支推送到远程 Git 存储库并对其进行跟踪?
【发布时间】:2011-02-15 11:07:35
【问题描述】:

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

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

  2. 推送本地分支 到远程存储库(发布),但让它 可追踪,所以git pullgit push 将立即工作。

我该怎么做?

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

【问题讨论】:

  • 只是指出 --set-upstream 是 -u
  • 很多答案包含不相关的信息(例如如何创建分支),如果答案适用,则缺少有关所用魔术参数的信息。
  • @VividD “添加了说明性图片” - 认真的吗? o.O

标签: git repository git-branch git-push git-remote


【解决方案1】:

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

git checkout -b <branch>

编辑文件、添加和提交。然后push with the -u (short for --set-upstream)选项:

git push -u origin <branch>

Git 会在推送过程中设置跟踪信息。

【讨论】:

  • 另外值得注意的是,如果你已经在你推送的分支上设置了一个现有的跟踪分支,并且push.default 设置为upstream,这不会像你想象的那样做。它将尝试推送现有的跟踪分支。使用:git push -u origin mynewfeature:mynewfeature 或先使用git branch --unset-upstream
  • 我仍然需要 'git branch --set-upstream-to origin/remote' 以使 'git status' 正确报告我的分支相对于远程分支的状态。
  • 对于在 Visual Studio 中使用 Git 的人:实际上这是 Visual Studio 中的“发布分支”。使用 -u 参数执行 git push 后,我终于可以看到我的分支在 VS UI 中发布了。
  • 你也可以使用git push -u origin HEAD
  • @Stephane 您只需-u 一次即可启动跟踪。之后只需使用git push
【解决方案2】:

如果您不与其他人共享您的存储库,这有助于将您的 所有 分支推送到远程,并且 --set-upstream 为您正确跟踪:

git push --all -u

(不完全是 OP 所要求的,但这种单线非常受欢迎)

如果您与其他人共享您的 repo,这不是一个很好的形式,因为您将使用所有不可靠的实验分支阻塞 repo。

【讨论】:

  • git pull --all 将其全部拉回其他地方? kewl
  • Git 允许提交一个分支而不是出于非常好的原因推送它。只使用 git push --all 就像丢掉一个 git 架构。如果它对你有用,那完全没问题,太好了,永远做下去。但是请不要仅仅因为它是一种快速的做事方式而建议其他人避免学习 git。
  • 这确实不是正确的答案,如果没有真正解释它的作用和含义,它也不是推荐的好工具。请考虑记下这个答案。
  • @Federico @akronymn 在哪里可以找到做git push --all -u的危险?
  • @akronymn @ Federico - 我已经对其进行了编辑,以说明我所看到的危险 - 这样更好吗?
【解决方案3】:

在引入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

【讨论】:

  • 有时你需要这个git push origin -u local_branch:remote_branch
  • 为什么“git push origin -u remote_branch_name”有时有效,有时无效?
【解决方案4】:

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

git branch <branch-name>

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

git push -u origin <branch-name>

【讨论】:

  • git branch &lt;branch-name&gt;git checkout -b &lt;branch-name&gt; 都创建了一个分支,但结帐切换到新分支
  • dude 括号只是提到你必须用你想要创建和推送的任何分支名称替换。
  • @AdiPrasetyo 你能详细说明你想说/问什么吗?
【解决方案5】:

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

  1. 基于其他(远程或本地)分支创建本地分支:

    git checkout -b branchname
    
  2. 将本地分支推送到远程存储库(发布),但使其可跟踪,以便 git pullgit push 立即工作

    git push -u origin HEAD
    

    使用HEAD 是一种“将当前分支推送到远程同名的便捷方式”。来源:https://git-scm.com/docs/git-push 在 Git 术语中,HEAD(大写)是对当前分支(树)顶部的引用。

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

【讨论】:

  • 谢谢 :) git push -u origin &lt;branch-name&gt; 对我不起作用,但使用 HEAD 而不是 &lt;branch-name&gt; 效果很好:)
【解决方案6】:

我只是这样做

git push -u origin localBranch:remoteBranchToBeCreated

在一个已经克隆的项目上。

Git 在我在 localBranch 中所做的提交下创建了一个名为 remoteBranchToBeCreated 的新分支。

编辑:这会将您当前的本地分支(可能命名为localBranch)上游更改为origin/remoteBranchToBeCreated。要解决这个问题,只需键入:

git branch --set-upstream-to=origin/localBranch

git branch -u origin/localBranch

因此,您当前的本地分支现在跟踪 origin/localBranch

【讨论】:

  • 这正是我一直在寻找的东西
  • 当我尝试这个时,git 会抛出 error: src refspec &lt;new branch&gt; does not match any.
  • 这应该是最佳答案。
【解决方案7】:

编辑过时了,只用git push -u origin $BRANCHNAME


使用William's miscellaneous Git tools 中的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通常是来源(您可以修改脚本以默认来源等...)

【讨论】:

  • 这假设我已经安装了 ruby​​。没有这样的运气。还有其他想法吗?
  • ruby 脚本调用git pushgit config 命令。我使用脚本的代码来编辑我的答案。您可以使用此信息创建一个小型 shell 脚本来为您执行推送。
  • William 的杂项 git 工具似乎已移动(该链接现已失效)。一个工作链接是:gitorious.org/willgit
  • “威廉的”链接再次断开;新链接似乎是git-wt-commit.rubyforge.org
  • 编辑后的答案只有一个工作链接 (github.com/DanielVartanov/willgit)
【解决方案8】:

我想你已经克隆了一个类似的项目:

git clone http://github.com/myproject.git
  1. 然后在您的本地副本中,创建一个新分支并检查它:

    git checkout -b <newbranch>
    
  2. 假设您在服务器上创建了“git bare --init”并创建了 myapp.git,您应该:

    git remote add origin ssh://example.com/var/git/myapp.git
    git push origin master
    
  3. 之后,用户应该可以

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

注意:我假设您的服务器已启动并正在运行。如果不是,它将无法正常工作。一个很好的方法是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

【讨论】:

  • 我链接到的威廉的脚本与删除远程分支和一些保护措施的附加选项大致相同
  • >将本地分支推送到远程仓库(发布),但使其>可跟踪,以便 git pull 和 git push 立即工作。当您将代码推送到他们的存储库时,它会自动执行 github 的操作 :-)
  • 这不回答问题,原始 repo 的 不可跟踪(并重命名为 是您在步骤 3 中克隆的新 repo)。
  • 似乎有点矫枉过正。 git remote add origin 是否使本地分支可跟踪?这是这里的关键命令吗?
  • @Roni Yaniv:没有git remote add origin 只注册一个新的远程存储库。这只是将您的分支推送到该远程存储库之前所需的一个步骤(如果您不想每次都输入整个地址)
【解决方案9】:

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

git checkout -b <new_branch>

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

git push -u origin <new_branch>

这会创建所有本地提交并将其推送到新创建的远程分支origin/&lt;new_branch&gt;

【讨论】:

    【解决方案10】:

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

    git checkout -b name_branch
    

    (name_branch,例如:master

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

    git push -u origin name_new_branch
    

    (name_new_branch,例如:feature

    【讨论】:

      【解决方案11】:

      我创建了一个别名,这样每当我创建一个新分支时,它就会相应地推送和跟踪远程分支。我将以下块放入.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
      

      用法:只需输入gcb thuy/do-sth-koolthuy/do-sth-kool 是我的新分支名称。

      【讨论】:

        【解决方案12】:

        您可以分两步完成:

        1. 使用checkout 创建本地分支:

        git checkout -b yourBranchName
        

        随心所欲地使用您的分支。

        2. 使用push 命令自动创建分支并将代码发送到远程存储库:

        git push -u origin yourBanchName
        

        有多种方法可以做到这一点,但我认为这种方法非常简单。

        【讨论】:

          【解决方案13】:

          将本地更改推送到新功能分支的完整 Git 工作流程如下所示

          拉取所有远程分支

          git pull --all
          

          现在列出所有分支

          git branch -a  
          

          结帐或创建分支(将&lt;feature branch&gt;替换为您的分支名称):

          git checkout -b <feature branch>
          

          显示当前分支。必须在前面显示 *

          git branch      
          

          添加您的本地更改(这里是故意的)

          git add .
          

          现在提交您的更改:

          git commit -m "Refactored/ Added Feature XYZ"
          

          重要提示:从 master 获取更新:

          git pull origin feature-branch
          

          现在推送您的本地更改:

          git push origin feature-branch
          

          【讨论】:

            【解决方案14】:

            基于这里的答案,我将这个过程封装为一个简单的 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
            

            【讨论】:

              【解决方案15】:

              为了获得最大的灵活性,您可以使用custom Git command。例如,在 $PATH 的某个位置创建以下 Python 脚本,名称为 git-publish 并使其可执行:

              #!/usr/bin/env python3
              
              import argparse
              import subprocess
              import sys
              
              
              def publish(args):
                  return subprocess.run(['git', 'push', '--set-upstream', args.remote, args.branch]).returncode
              
              
              def parse_args():
                  parser = argparse.ArgumentParser(description='Push and set upstream for a branch')
                  parser.add_argument('-r', '--remote', default='origin',
                                      help="The remote name (default is 'origin')")
                  parser.add_argument('-b', '--branch', help='The branch name (default is whatever HEAD is pointing to)',
                                      default='HEAD')
                  return parser.parse_args()
              
              
              def main():
                  args = parse_args()
                  return publish(args)
              
              
              if __name__ == '__main__':
                  sys.exit(main())
              

              然后git publish -h会显示使用信息:

              usage: git-publish [-h] [-r REMOTE] [-b BRANCH]
              
              Push and set upstream for a branch
              
              optional arguments:
                -h, --help            show this help message and exit
                -r REMOTE, --remote REMOTE
                                      The remote name (default is 'origin')
                -b BRANCH, --branch BRANCH
                                      The branch name (default is whatever HEAD is pointing to)
              

              【讨论】:

                【解决方案16】:

                我认为这是最简单的别名,添加到您的~/.gitconfig

                [alias]
                  publish-branch = !git push -u origin $(git rev-parse --abbrev-ref HEAD)
                

                你只是跑

                git publish-branch
                

                并且...它发布了分支

                【讨论】:

                  猜你喜欢
                  • 2011-02-15
                  • 1970-01-01
                  • 2011-01-09
                  • 2016-02-22
                  • 2014-02-04
                  • 1970-01-01
                  相关资源
                  最近更新 更多