【问题标题】:How can I push a local Git branch to a remote with a different name easily?如何轻松地将本地 Git 分支推送到具有不同名称的远程?
【发布时间】:2011-08-09 23:24:33
【问题描述】:

我一直想知道是否有一种简单的方法可以在不总是指定两个名称的情况下使用不同名称的远程分支来推送和拉取本地分支。

例如:

$ git clone myrepo.git
$ git checkout -b newb
$ ...
$ git commit -m "Some change"
$ git push origin newb:remote_branch_name

现在如果有人更新 remote_branch_name,我可以:

$ git pull

一切都被合并/快进了。但是,如果我在本地“newb”中进行更改,我不能:

$ git push

相反,我必须:

% git push origin newb:remote_branch_name

看起来有点傻。如果git-pull 使用git-config branch.newb.merge 来确定从哪里拉取,为什么git-push 不能有类似的配置选项?有没有一个不错的捷径,还是我应该继续走很长的路?

【问题讨论】:

    标签: git version-control git-push git-remote


    【解决方案1】:

    当然。只需将您的 push.default 设置为 upstream 即可将分支推送到它们的上游(这与 pull 将从中提取的相同,由 branch.newb.merge 定义),而不是将分支推送到名称匹配的分支(这是默认设置) push.default, matching).

    git config push.default upstream
    

    请注意,在 Git 1.7.4.2 之前,这曾经被称为 tracking 而不是 upstream,因此如果您使用的是旧版本的 Git,请改用 trackingpush.default 选项是在 Git 1.6.4 中添加的,因此如果您使用的是旧版本,则根本没有此选项,需要明确指定要推送到的分支。

    【讨论】:

    • 就是这样!这适用于所有正在跟踪的分支,但这很好。谢谢!
    • 我很想知道是否有我签出的分支名称的简写。我可能只想git push buildserver .:test_this . 应该是我不一定知道的本地分支名称。我可以查一下,当然,但如果有一个短手,比如点,那会很酷。
    • @FrederickNord HEAD 是您正在寻找的简写。 HEAD 指向当前签出的分支,或者如果您有一个分离的头,则直接指向特定的提交。
    • 谢谢!这为我修复了 Updates were rejected because a pushed branch tip is behind its remote 错误。我的本地存储库是最新的,但名称不同。
    【解决方案2】:

    当你进行初始推送时添加the -u parameter:

    git push -u origin my_branch:remote_branch
    

    后续推送将转到您想要的位置。

    编辑:

    根据评论,这只设置了拉动。

    git branch --set-upstream
    

    应该这样做。

    【讨论】:

    • -u 只是设置上游,根据问题,它已经设置好了。他需要将push.default 设置为upstrem 以使push 尊重上游设置,因为默认情况下只有pull 可以。
    • 这似乎行不通 - 至少在从不版本的 git 上不行
    • git branch --set-upstream 只有在此之后才需要设置拉取设置“pushRemote”。
    • 我不认为git branch --set-upstream 是必要的。注意:我的git --version 是 2.17.1。
    • 您上面显示的语法在我的回答中很重要,并被我的回答引用:How to change the owner of a PR on GitHub / How to commandeer an open GitHub PR。顺便说一句,您是如何学习 from_branch:to_branch 语法的?它是埋在man git push 页面的某个地方吗?
    【解决方案3】:

    Adam 的命令现已弃用。您可以使用:

    git branch --set-upstream-to origin/my_remote_branch my_local_branch
    

    my_local_branch的上游分支设置为origin/my_remote_branch

    【讨论】:

    • 您能否提供一个链接来支持您分享的有关 -u 被弃用的信息? git-scm.com 上有关推送的最新版本的文档似乎表明它是最新的。在此先感谢 - 读者跟进了解更多信息可能会非常有帮助。
    • 同意@KayV,尤其是当我尝试创建一个名称与本地分支不同的新远程分支时收到此消息:'如果您计划推出一个新的本地分支,该分支将跟踪其远程副本,您可能希望在推送时使用“git push -u”来设置上游配置。'
    【解决方案4】:

    这是对我有用的过程。

    git clone original-repo-url
    git remote rename origin upstream
    git remote add origin new-repo-url
    

    现在您的新存储库将是“起源”,而原始存储库是“上游”。通过运行 git remote -v 确认它。 (旁注:Upstream 用于从原始存储库中获取 - 为了使您的本地副本与您想要贡献的项目保持同步 - 而 origin 用于拉取和推送,因为您可以为自己的存储库做出贡献)。

    git push origin master

    现在,您的新远程 repo 的 master(在 Github 上)将与原始 master 同步,但它不会有任何功能分支。

    git rebase upstream/branch-name
    git push origin master
    

    Rebase 是一种智能合并。然后再次 push 到 master,你会在新的 repo 上看到所选的功能分支作为 master。

    可选:

    git remote rm upstream
    git remote add upstream new-repo-url
    

    【讨论】:

      【解决方案5】:

      我遇到同样的问题已经有一段时间了。我终于有了一组语句,所以我不必每次都做git push origin local:remote。我关注了这些:

      git branch --set-upstream-to origin/remote_branch_name
      git config push.default upstream
      git push
      

      将上游设置为具有不同名称的远程分支(第 1 行)然后将该上游设置为默认(第 2 行)后,第 3 行现在将遵循这些规则并推送到设置的上游。

      【讨论】:

        【解决方案6】:

        如何在 Git 上推送到不同名称的分支

        您通常会将本地分支推送到同名的远程分支,但并非总是如此。

        要推送到不同名称的分支,您只需指定branch you want to push 和要推送到的分支的名称,用冒号 (:) 分隔。

        例如,如果你想将一个名为some-branch 的分支推送到my-feature

        (some-branch)$ git push origin some-branch:my-feature
        Total 0 (delta 0), reused 0 (delta 0)
        To github.com:johnmosesman/burner-repo.git
         + 728f0df...8bf04ea some-branch -> my-feature
        

        如何将所有本地分支推送到远程

        您不需要经常从本地推送所有分支,但如果这样做,您可以添加 --all 标志:

        (main)$ git branch
        * main
          my-feature
        
        (main)$ git push --all
        ...
        To github.com:johnmosesman/burner-repo.git
           b7f661f..6e36148  main -> main
         * [new branch]      my-feature -> my-feature
        

        【讨论】:

          【解决方案7】:

          推送并创建一个临时远程分支

          如果你想:

          • 以新名称将当前分支推送到远程,但是:
          • 不要更改当前分支的远程跟踪分支,并且:
          • 不要以新名称创建本地分支,

          那么就这么简单:

          git push origin HEAD:temp-branch-name
          

          注意:您可以将 HEAD 替换为任何其他分支或提交 ID 来推送它。

          【讨论】:

            【解决方案8】:

            如何轻松地将本地 Git 分支推送到具有不同名称的远程?

            总结:

            以下是您通常需要的关键命令的简短摘要:

            # push from your local `branch2` to a remote `branch1` (push to a branch with
            # a different name) on the remote named `origin`
            git push -u origin branch2:branch1
            # pull from a remote branch `branch1` into your currently-checked-out branch
            # (which could have a different name--ex: `branch2`)
            git pull origin branch1
            
            # Set your upstream to something new in case you want to change it; ex: set your
            # currently-checked-out branch (perhaps `branch2`) to track `branch1` on the 
            # remote named `origin`
            git branch -u origin/branch1
            # Unset your upstream
            git branch --unset-upstream
            
            # See what your upstream is currently set to
            git branch -vv
            

            细节:推送到另一个分支,从另一个分支拉取,设置和取消设置上游分支以跟踪

            这里有太多不完整和部分的答案,这给我留下了很多问题和很多不足之处。因此,经过大量的努力、研究和试验,我尝试提供一个完整的解决方案。

            1。从本地分支推送到具有不同名称的远程分支

            从本地 branch2 推送到远程 branch1,您必须像这样指定两个分支:

            # Push from local `branch2` to remote `branch1`
            git push origin branch2:branch1
            
            # General form: push from local `from_branch` to remote `to_branch`. 
            # - Watch out!: see also the additional explanations and NB note below!
            git push <remote> <from_branch>[:to_branch]
            

            但是请注意,我在上面一般形式中写的方括号表示:to_branch 部分是可选的。我的意思是从一个名称的本地分支推送到另一个名称的远程分支,这部分不是可选的,但是,作为一个通用的 git 命令,如果你不包含:to_branch,该命令将运行部分,这意味着它在这个意义上是可选的。但是,它可能会产生意想不到的结果!看看这个命令,例如:

            # (push to a remote branch with the **same name** as the local branch)
            
            # Reduced **and confusing** form: this pushes from local `branch2` (even if you
            # don't currently have it checked-out!) to remote `branch2`.
            git checkout branch3 
            git push origin branch2          # Push from local branch2 to remote branch2
            

            您可能当前已签出本地branch3,并认为git push origin branch2 会将您本地的branch3 推送到远程branch2,因为您的系统上当前已签出branch3,但是这不是会发生的!相反,git push origin branch2 会将您的本地 branch2 再次推送到您的远程 branch2即使您当前没有 branch2 已签出! 因此,git push origin branch2 是一个等效的短- 这个的手:

            # These 2 commands are **exactly identical**! The 1st cmd is the short form
            # of the 2nd. 
            git push origin branch2          # Push from local branch2 to remote branch2
            git push origin branch2:branch2  # Push from local branch2 to remote branch2
            

            如果您认为它会从您当前签出的分支推送,那么上述 cmd 的简短形式会产生非常混乱的行为。以下是总结上述行为的 Nota bene 说明:

            NB:在一般形式git push &lt;remote&gt; &lt;from_branch&gt;[:to_branch]中,如果你没有用:to_branch指定远程TO分支,则假定它与你的本地FROM分支同名,from_branch,on remote!这意味着如果您只键入git push origin branch2 而不是git push origin some_other_branch:branch2,它会从您的本地branch2 推送到branch2 的远程副本,即使您在运行时没有branch2 在本地签出命令!如果您认为输入git push origin branch2 刚刚告诉您当前签出的分支some_other_branch 推送到远程的branch2 而不是本地的branch2,这可能会非常令人困惑被推送到远程branch2

            通用表单 (git push &lt;remote&gt; &lt;from_branch&gt;[:to_branch]) 的文档很难找到,但实际上可以在靠近顶部的 man git push 页面中找到,位于“"&lt;refspec&gt;...”部分下方:

            &lt;refspec&gt; 参数的格式是可选的加上+,后跟源对象&lt;src&gt;,后跟冒号:,后跟目标引用&lt;dst&gt;

            然后:

            :&lt;dst&gt; 部分可以省略——这样的推送将更新一个引用,&lt;src&gt; 通常会在命令行上没有任何&lt;refspec&gt; 的情况下更新。

            我认为此文档不直观且很难理解,但是,如果没有一些示例和我上面的解释。

            [BETTER FORM OF git push]你也可以在推送的同时设置上游分支

            # Push from local `branch2` to the remote `branch1`, while also at the same time
            # setting `branch2` to track `origin/branch1` as the upstream
            git push -u origin branch2:branch1
            # OR (same thing)
            git push --set-upstream origin branch2:branch1
            # General form
            git push -u <remote> <from_branch>[:to_branch]
            

            作为上述命令输出的一部分,您应该看到:

            Branch 'branch2' set up to track remote branch 'branch1' from 'origin'.
            

            为了让那里发生的事情变得明显,请知道上面的两个命令中的任何一个都等效于这些 两个 单独的命令:

            git push origin branch2:branch1
            git branch -u origin/branch1
            

            现在,要查看您分支的上游分支当前设置的内容,请运行 double-verbose (-vv) git branch cmd:

            git branch -vv
            

            样本输出:
            这里可以看到上游分支是origin/master,也就是说远程上的master分支名为origin

            * master b2f0466 [origin/master] c/array_filter_and_remove_element.c: add O(n) in-place solution
            

            注意事项:

            1. -vv 上面的意思是“双重冗长”。这意味着它将打印git branch,不仅是冗长的,而且是双倍的,或者更冗长的。现在打印的“额外详细”内容包括方括号中的上游分支,如上所示:[origin/matser]
            2. 您可以使用git remote -v 查看所有遥控器。 origin 是上面示例中显示的遥控器。

            2。从具有不同名称的远程分支拉到本地分支

            [如果您已经有分支 branch2 在本地签出,推荐使用!]在名为 origin 的遥控器上拉出 branch1,到 branch2,您必须指定要从中提取的远程分支,如下所示:

            # THIS ASSUMES YOU ARE ALREADY CHECKED-OUT ON BRANCH `branch2`!
            
            git pull origin branch1
            # General form
            git pull <remote> [from_branch]
            

            你也可以指定两个分支,但是我不完全确定这种情况下有什么区别(如果你知道的话,请在这里帮助我):

            git pull origin branch1:branch2
            
            # The general form seems to be:
            git pull <remote> <from_branch>[:to_branch]
            

            以下命令仅在远程分支和本地分支具有相同名称时才有效!(因此它不会回答这个 Stack Overflow 问题)。如果您还没有分支 some_branch 已签出,建议使用此命令!

            # Pull FROM a remote branch named `some_branch` TO a local branch named
            # `some_branch`, while you do NOT have `some_branch` locally checked-out.
            git fetch origin some_branch:some_branch
            # General form
            git fetch <remote> <from_branch>:<to_branch>
            
            # The above is a special form of `git fetch`, and (I believe) requires that 
            # `from_branch` and `to_branch` are **the same branch name**. It is roughly 
            # equivalent to the following *several* commands:
            git checkout any_other_branch
            # this `git fetch` cmd updates the **locally-stored**, hidden, remote-tracking
            # branch named `origin/some_branch` with the latest changes from the branch
            # by this name stored on the remote server named `origin`
            git fetch origin some_branch 
            git checkout some_branch
            git merge origin/some_branch  # merge `origin/some_branch` into `some_branch`
            git checkout any_other_branch # go back to the branch we started on
            

            注意事项:

            1. git push 不同,git pull 没有-u 选项。
            2. 另请参阅我的另一个答案:How to change the owner of a PR on GitHub / How to commandeer an open GitHub PR
            3. git fetch origin some_branch:some_branch 命令使用相同的 some_branch 名称完成两次 -- 在命令的两个位置。区别很简单,git fetch origin some_branch 仅更新名为origin/some_branch本地存储、隐藏、远程跟踪分支,该分支的最新更改存储在名为@987654405 的远程服务器上@,而 git fetch origin some_branch:some_branch 这样做 PLUS 也会使用这些更改更新本地存储的可见 some_branch
              1. 如果您对此感到困惑,您需要了解每 1 个some_branch认为你实际上有多达 3 个分支:1) 本地分支 some_branch,2) 远程服务器上的远程分支 some_branch,名为 origin,以及 3) 本地存储、隐藏、远程跟踪分支,名为 origin/some_branch。在这里阅读更多信息。我第一次了解到每个分支 3 个分支 的概念:How do I delete a Git branch locally and remotely?。另请参阅该答案下的 my comment here

            3。配置本地分支以跟踪或取消跟踪远程分支

            您可以设置本地分支名为branch2跟踪上游分支名为branch1在推送的同时使用上面显示的git push -u cmd。

            您还可以设置名为branch2 的本地分支来跟踪名为branch1 的上游分支,如下所示:

            # Set branch2 to track origin/branch1 (`branch1` on remote `origin`)
            git branch --set-upstream-to=origin/branch1 branch2
            # OR (same thing as just above)
            git branch -u origin/branch1 branch2
            # General form
            git branch -u <remote>/<to_branch> [from_branch]
            
            # OR, same as above if the currently-checked-out branch is `branch2`
            git branch --set-upstream-to=origin/branch1
            # OR (same thing as just above)
            git branch -u origin/branch1
            # General form
            git branch -u <remote>/<to_branch>
            

            branch2 取消设置您的上游分支,使其不再跟踪先前设置的上游分支(在上面的示例中为origin/branch1),请运行以下命令:

            git branch --unset-upstream branch2
            # OR, same as above if the currently-checked-out branch is `branch2`
            git branch --unset-upstream
            

            再次,如上所示,要查看您的分支的上游分支当前设置的内容,请运行 double-verbose (-vv) git branch命令:

            git branch -vv
            

            参考资料:

            1. 我第一次学习git push -u origin local_FROM_branch:remote_TO_branch 语法的地方:@Adam Dymitruk's answer
            2. https://devconnected.com/how-to-set-upstream-branch-on-git/
            3. How do I delete a Git branch locally and remotely?

            我写过的相关git主题:

            1. 初学者:
              1. Create a branch in Git from another branch
            2. 中级:
              1. How to cherry-pick multiple commits
            3. 高级:
              1. How to get just one file from another branch?
              2. Who is "us" and who is "them" according to Git?

            【讨论】:

            • 谢谢,您的回答非常详细,但在短时间内有点难以理解:(例如,即使您在运行命令时没有 from_branch 在本地签出!这可以如果#你以为你刚刚告诉你当前签出的分支将#推送到远程的某个分支,而不是某个#分支的本地副本,那会非常混乱>(您当前没有签出)被推送到 # 遥控器。)这部分可以更简洁一点吗?
            • @LanSi,这肯定是一个非常令人困惑的话题,我很难解释。我更新了我的答案,试图澄清它并使那部分更容易理解。如果现在更容易理解,请告诉我。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-10-25
            • 2015-04-02
            • 2018-10-02
            • 1970-01-01
            • 2013-06-10
            • 2016-02-22
            • 2020-02-05
            相关资源
            最近更新 更多