git push origin master:refs/heads/master 这是做什么的

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

git push origin master:refs/heads/master what does this do

gitgitolite

提问by MrNemus

When I create a new repo on my gitolite repository I always have to enter the following command before I can start pushing code to the server.

当我在我的 gitolite 存储库上创建一个新的存储库时,我总是必须输入以下命令才能开始将代码推送到服务器。

git push origin master:refs/heads/master

git push origin master:refs/heads/master

What does it do ?

它有什么作用 ?

My guess is that is has to do with the head reference not sure. Could someone explain it to me?

我的猜测是这与不确定的头部参考有关。有人可以向我解释一下吗?

回答by Lily Ballard

There's three parts to this command:

该命令分为三部分:

git push

This invokes the push command

这将调用 push 命令

origin

This names the remote to which you are pushing. This is either one of the named remotes stored in .git/config (you can list these with git remote), a URL, or the token .which means the current repository.

这会命名您要推送到的遥控器。这是存储在 .git/config 中的命名遥控器之一(您可以使用 列出它们git remote)、URL 或.表示当前存储库的令牌。

master:refs/heads/master

This is called a "refspec", and you can read about it in the man page for git push. But in general, it's comprised of two parts, separated by a colon. The first part is the name of a local branch, and the second part is the name of a branch on the remote repository (in this case, origin). This particular refspec could be shortened to master:master.

这称为“refspec”,您可以在git push. 但总的来说,它由两部分组成,用冒号隔开。第一部分是本地分支的名称,第二部分是远程存储库上的分支名称(在本例中为origin)。这个特定的 refspec 可以缩短为master:master.

In general, one can shorten refspecs even further. Just specifying masteras the refspec is equivalent to using the same name on the remote, so masteris the same as master:master.

一般来说,可以进一步缩短参考规格。仅指定master为 refspec 等效于在遥控器上使用相同的名称,因此mastermaster:master.

回答by manojlds

master:refs/heads/masteris a refspec.

master:refs/heads/master是一个参考规范。

refspecsare of the form +<src>:<dst>

refspecs是形式 +<src>:<dst>

So here master is the ref on your local repo that you are pushing to the refs/heads/masterrefspec on the remote (origin). masteris short for refs/heads/masteractually.

所以这里的 master 是你本地 repo 上的 ref,你正在推送到refs/heads/master远程(原点)上的 refspec。实际上的master缩写refs/heads/master

In fact, you can just do git push origin masterwhereby it will push master on your local to master on remote. Only when you want to push to a different ref do you need to explicitly specify the destination ref.

事实上,你可以这样做git push origin master,它将本地的 master 推送到远程的 master。只有当你想推送到不同的引用时,你才需要明确指定目标引用。

Also just git pushhas default behaviour too, which probably wouldn't have been the case before you did the first push and created a branch (master ) on the remote. So it would have seemed like you need to do the command you had mentioned. Refer to the manual

也只有 git push默认行为,在您进行第一次推送并在远程创建分支( master )之前可能不会出现这种情况。因此,您似乎需要执行您提到的命令。参考说明书

回答by Ryan Stewart

The default behavior of git push, which is presumably what you describe as "pushing code to the server", is to only push local branches that have a matching branch, by name, on the remote you're pushing to. When you create a new repo, it has no branches in it, so a simple git pushwill push nothing. You have to explicitly push a branch by name first. Thereafter, the default behavior will work as you expect it to.

的默认行为git push,大概是您所描述的“将代码推送到服务器”,是仅推送具有匹配分支的本地分支,按名称,在您推送到的远程上。当你创建一个新的 repo 时,它里面没有分支,所以一个简单的git push不会推送任何东西。您必须首先按名称显式推送分支。此后,默认行为将按您的预期工作。

P.S. Actually, you only have to git push origin master. What it does is push your local master to the gitolite repo as master, since you didn't specify a different name. If you said git push origin master:foo, then the branch you locally call "master" would be known as "foo" on gitolite.

PS 其实,你只需要git push origin master. 它的作用是将您的本地 master 作为 master 推送到 gitolite repo,因为您没有指定不同的名称。如果您说git push origin master:foo,那么您在本地称为“master”的分支在 gitolite 上将被称为“foo”。

P.P.S. You can switch default push behavior between "nothing", "matching" (the default), "trackings"/"upstream", and "current". See the settings for "push.default" on the git-config man page.

PPS 您可以在“无”、“匹配”(默认)、“跟踪”/“上游”和“当前”之间切换默认推送行为。请参阅git-config 手册页上的“push.default”设置。

回答by Adam Dymitruk

It sets up tracking for you. You can use the shorthand for this:

它会为您设置跟踪。您可以为此使用简写:

git push origin master

The part after the colon is the name of the branch on the remote repo. If you omit it, git assumes you want the same name.

冒号后面的部分是远程仓库上的分支名称。如果省略它,git 假定您想要相同的名称。

Hope this helps.

希望这可以帮助。