Git push:为分支设置目标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8170558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:19:30  来源:igfitidea点击:

Git push: set target for branch

git

提问by m1schka

I'd like to push my current branch (hp1) with

我想推送我当前的分支(hp1)

git push

and not

并不是

git push origin hp1:team/hp1

The remote branch already exists.

远程分支已经存在。

My local branches:

我当地的分支机构:

develop
master
* hp1

git remote show origin tells me:

git remote show origin 告诉我:

Remote branches:
  develop  tracked
  master   tracked
  team/h2  tracked
  team/hp1 tracked
  team/n1  tracked
Local branches configured for 'git pull':
  develop  merges with remote develop
  master   merges with remote master
  hp1 merges with remote team/hp1
Local refs configured for 'git push':
  master   pushes to master   (up to date)

I already tried

我已经试过了

git branch --set-upstream hp1 origin/team/hp1

and

git branch --set-upstream hp1 refs/remotes/origin/team/hp1

but both don't work.

但两者都不起作用。

My colleague has a local branch called as the remote branch (team/hp1) and the code above works for him. He gets at the end an additional

我的同事有一个本地分支称为远程分支 (team/hp1),上面的代码适用于他。他最后得到了一个额外的

Local refs configured for 'git push':
  develop  pushes to develop  (up to date)
  master   pushes to master   (up to date)
  team/hp1 pushes to team/hp1 (up to date)

So maybe you can tell me what's wrong and how to fix it.

所以也许你可以告诉我出了什么问题以及如何解决它。

EDITmy config:

编辑我的配置:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = ***@***:***.git
[branch "master"]
    remote = origin
    merge = refs/heads/master
[branch "hp1"]
    remote = origin
    merge = refs/heads/team/hp1

回答by manojlds

First of all, when pushing for the first time, do:

首先,第一次推送的时候,做:

git push -u origin hp1:team/hp1

About -u option:

关于 -u 选项:

-u
--set-upstream

For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch..merge in git-config(1).

-u
--set-upstream

对于每个最新或成功推送的分支,添加上游(跟踪)引用,由无参数 git-pull(1) 和其他命令使用。有关更多信息,请参阅 git-config(1) 中的 branch..merge。

Note from the manual that, this in itself will not determine what happens when you do git pushthe next time. When you do git pullwhile in this branch, it will fetch it from the upstream that you have set. But when you push, it will push to a matching branch ( in this case hp1 and not team/hp1)

请注意手册中的内容,这本身并不能决定您git push下次执行时会发生什么。当您git pull在此分支中执行时,它将从您设置的上游获取它。但是当你推送时,它会推送到一个匹配的分支(在这种情况下是 hp1 而不是 team/hp1)

For that to work, you have to set push.defaultconfig value to upstream. Once you set that, when you push from a branch ( just do git push), it will push to the upstream as mentioned by branch.<name>.merge

为此,您必须将push.default配置值设置为upstream. 一旦你设置了它,当你从一个分支推送(just do git push)时,它会推送到上游,正如前面提到的branch.<name>.merge

So do:

所以这样做:

git config push.default upstream

About push.default:

关于 push.default:

push.default

Defines the action git push should take if no refspec is given on the command line, no refspec is configured in the remote, and no refspec is implied by any of the options given on the command line. Possible values are:

nothing - do not push anything.

matching - push all matching branches. All branches having the same name in both ends are considered to be matching. This is the default.

upstream - push the current branch to its upstream branch.

tracking - deprecated synonym for upstream.

current - push the current branch to a branch of the same name.

push.default

定义 git push 应该采取的操作,如果命令行上没有给出 refspec,远程中没有配置 refspec,并且命令行上给出的任何选项都没有暗示任何 refspec。可能的值为:

没什么 - 不要推任何东西。

匹配 - 推送所有匹配的分支。两端具有相同名称的所有分支都被认为是匹配的。这是默认设置。

上游 - 将当前分支推送到其上游分支。

跟踪 - 不推荐使用的上游同义词。

current - 将当前分支推送到同名分支。

回答by VonC

(March 2012): Beware: that "upstream" policy could become the default one soon
(sometime after git1.7.10+)
:

(2012 年 3 月):注意:“上游”政策很快就会成为默认政策
(在 git1.7.10+ 之后的某个时间)

See "Please discuss: what "git push" should do when you do not say what to push?"

请参阅“请大家讨论:什么是‘混帐推’当你不说推我应该怎么办?

In the current setting(i.e. push.default=matching), git pushwithout argument will push all branches that exist locally and remotely with the same name.
This is usually appropriate when a developer pushes to his own public repository, but may be confusing if not dangerous when using a shared repository.

The proposal is to change the default to 'upstream', i.e. push only the current branch, and push it to the branch git pull would pull from.
Another candidate is 'current'; this pushes only the current branch to the remote branch of the same name.

What has been discussed so far can be seen in this thread:

在当前设置(即push.default=matching)中,git push不带参数将推送本地和远程存在的所有同名分支
当开发人员推送到他自己的公共存储库时,这通常是合适的,但在使用共享存储库时,如果没有危险,可能会造成混淆。

建议是将默认值更改为 ' upstream',即仅推送当前分支,并将其推送到 git pull 将从中拉出的分支。
另一个候选人是' current'; 这只会将当前分支推送到同名的远程分支。

到目前为止讨论的内容可以在这个线程中看到:

http://thread.gmane.org/gmane.comp.version-control.git/192547/focus=192694

http://thread.gmane.org/gmane.comp.version-control.git/192547/focus=192694

Previous relevant discussions include:

之前的相关讨论包括:

To join the discussion, send your messages to: [email protected]

要加入讨论,请将您的消息发送至:[email protected]

回答by mipadi

Use the -uoption to git push:

使用该-u选项git push

$ git push -u origin hp1:team/hp1

Then, after that, you can do:

然后,在那之后,您可以执行以下操作:

$ git push

回答by Noel Yap

The following will allow one not to have to specify -u ${branch_name}for the first git push.

以下将允许不必-u ${branch_name}为第一个指定git push

git config "branch.${branch_name}.remote" origin
git config "branch.${branch_name}.merge" "refs/heads/${branch_name}"

Granted, it's much more typing but not when it's in a script that sets up one's workspace. It also doesn't prematurely push the branch to the remote repo.

当然,打字要多得多,但在设置工作区的脚本中则不然。它也不会过早地将分支推送到远程仓库。