git push 到指定分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19154302/
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 17:05:43  来源:igfitidea点击:

git push to specific branch

gitversion-controlpush

提问by code_fodder

Even after reading this question: git-push-current-branch, I am still having difficulty figuring out how I should write my git push command. As mentioned in the question link, it's not clear from the documentation.

即使在阅读了这个问题之后:git-push-current-branch,我仍然很难弄清楚我应该如何编写我的 git push 命令。如问题链接中所述,文档中不清楚。

I would like to use my 'real world' example. Following is what I see when I do a git statuscommand on the top level of my branch:

我想使用我的“真实世界”示例。以下是我在git status分支的顶层执行命令时看到的内容:

On branch amd_qlp_tester

Your branch is ahead of 'origin/amd_qlp_tester' by 5 commits.

etc...

在分支 amd_qlp_tester

您的分支领先 'origin/amd_qlp_tester' 5 次提交。

等等...

So my branch name is amd_qlp_testerbut it was "branched" off from the main branch (if I have the terms wrong it's because of my SVN background). But then there is also the name `origin/amd_qlp_testser'

所以我的分支名称是amd_qlp_tester但它是从主分支“分支”出来的(如果我的术语有误,那是因为我的 SVN 背景)。但也有名字`origin/amd_qlp_testser'

So how do I phrase my push command?

那么我该如何表达我的推送命令呢?

Is it any of the following:

是否属于以下任何一种情况:

git push origin/amd_qlp_tester
git push origin amd_qlp_tester
git push amd_qlp_tester
git push origin
git push

采纳答案by Petr Mensik

git push origin amd_qlp_testerwill work for you. If you just type git push, then the remote of the current branch is the default value.

git push origin amd_qlp_tester会为你工作。如果你只输入git push,那么当前分支的远程是默认值。

Syntax of push looks like this - git push <remote> <branch>. If you look at your remote in .git/configfile, you will see an entry [remote "origin"]which specifies url of the repository. So, in the first part of command you will tell Git where to find repository for this project, and then you just specify a branch.

push 的语法看起来像这样 - git push <remote> <branch>。如果您查看.git/config文件中的远程文件,您将看到一个条目[remote "origin"],其中指定了存储库的 url。因此,在命令的第一部分中,您将告诉 Git 在哪里可以找到该项目的存储库,然后您只需指定一个分支。

回答by Imranmadbar

If your Local branch and remote branch is the same name then you can just do it:

如果您的本地分支和远程分支名称相同,那么您可以这样做:

git push origin branchName

When your local and remote branch name is different then you can just do it:

当您的本地和远程分支名称不同时,您可以这样做:

git push origin localBranchName:remoteBranchName

回答by torek

The answers in question you linked-to are all about configuring git so that you can enter very short git pushcommands and have them do whatever you want. Which is great, if you know what you want and how to spell that in Git-Ese, but you're new to git! :-)

您链接到的有问题的答案都是关于配置 git 的,这样您就可以输入非常短的git push命令并让它们做您想做的任何事情。这很好,如果您知道自己想要什么以及如何在 Git-Ese 中拼写,但您是 git 新手!:-)

In your case, Petr Mensik's answeris the (well, "a") right one. Here's why:

在您的情况下,Petr Mensik 的答案是(好吧,“a”)正确的答案。原因如下:

The command git push remoteroots around in your .git/configfile to find the named "remote" (e.g., origin). The config file lists:

该命令在您的文件中查找命名的“远程”(例如,)。配置文件列出:git push remote.git/configorigin

  • where (URL-wise) that remote "lives" (e.g., ssh://hostname/path)
  • where pushes go, if different
  • what gets pushed, if you didn't say what branch(es) to push
  • what gets fetched when you run git fetch remote
  • 远程“居住”的位置(例如,)ssh://hostname/path
  • 推去哪里,如果不同
  • 什么被推送,如果你没有说要推送什么分支
  • 运行时获取什么 git fetch remote

When you first cloned the repo—whenever that was—git set up default values for some of these. The URL is whatever you cloned from and the rest, if set or unset, are all "reasonable" defaults ... or, hmm, arethey?

当你第一次克隆 repo 时——无论何时——git 为其中一些设置了默认值。URL 是您从中克隆的任何内容,其余的,如果设置或未设置,都是“合理的”默认值......或者,嗯,吗?

The issue with these is that people have changed their minds, over time, as to what is "reasonable". So now (depending on your version of git and whether you've configured things in detail), git may print a lot of warnings about defaults changing in the future. Adding the name of the "branch to push"—amd_qlp_tester—(1) shuts it up, and (2) pushes just that one branch.

这些问题的问题在于,随着时间的推移,人们已经改变了对什么是“合理”的看法。所以现在(取决于您的 git 版本以及您是否已详细配置),git 可能会打印很多关于未来默认更改的警告。添加“要推送的分支”的名称amd_qlp_tester——(1)关闭它,(2)只推送那个分支。

If you want to push more conveniently, you could do that with:

如果你想更方便地推送,你可以这样做:

git push origin

or even:

甚至:

git push

but whether that does what you want, depends on whether you agree with "early git authors" that the original defaults are reasonable, or "later git authors" that the original defaults aren't reasonable. So, when you want to do all the configuration stuff (eventually), see the question (and answers) you linked-to.

但是这是否符合您的要求,取决于您是否同意“早期 git 作者”认为原始默认值是合理的,或者“后来的 git 作者”认为原始默认值不合理。因此,当您想要完成所有配置工作(最终)时,请查看您链接到的问题(和答案)。

As for the name origin/amd_qlp_testerin the first place: that's actually a local entity (a name kept inside your repo), even though it's called a "remote branch". It's git's best guess at "where amd_qlp_testeris over there". Git updates it when it can.

首先,对于名称origin/amd_qlp_tester:这实际上是一个本地实体(保存在您的仓库中的名称),即使它被称为“远程分支”。这是 git 对“那边的地方”的最佳猜测amd_qlp_tester。Git 尽可能更新它

回答by code_fodder

I would like to add an updated answer - now I have been using git for a while, I find that I am often using the following commands to do any pushing (using the original question as the example):

我想添加一个更新的答案 - 现在我已经使用 git 一段时间了,我发现我经常使用以下命令进行任何推送(以原始问题为例):

  • git push origin amd_qlp_tester- push to the branch located in the remote called originon remote-branch called amd_qlp_tester.
  • git push -u origin amd_qlp_tester- same as last one, but sets the upstream linking the local branch to the remote branch so that next time you can just use git push/pullif not already linked (only need to do it once).
  • git push- Once you have set the upstream you can just use this shorter version.
  • git push origin amd_qlp_tester- 推送到位于origin远程分支上的远程分支上调用的分支amd_qlp_tester
  • git push -u origin amd_qlp_tester- 与上一个相同,但设置了将本地分支链接到远程分支的上游,以便下次您可以在git push/pull尚未链接的情况下使用(只需执行一次)。
  • git push- 一旦你设置了上游,你就可以使用这个较短的版本。

Note-uoption is the short version of --set-upstream- they are the same.

注意-u选项是--set-upstream- 它们是相同的简短版本。