简体   繁体   English

git push origin development 和 git push origin HEAD:development 的区别

[英]Difference between git push origin development and git push origin HEAD:development

after some weeks using git as a machine, now I'm on my way to figure out how GIT really works.在使用 git 作为机器几周后,现在我正在弄清楚 GIT 的真正工作原理。 Than I started with a simple thing:比我开始做一个简单的事情:

git checkout -b test origin/development`   //Creating new local branch from development

Create new text file called test.txt

git add . //Set files that will be committed
git commit -m "this is a test" // Committing

And then comes my doubt:然后是我的疑问:

When I try to push doing:当我尝试推动做:

git push origin development

The log says: Everything is up to date日志说:一切都是最新的

If I try to push doing:如果我尝试推动做:

git push origin HEAD:development

It works as it should.它可以正常工作。

Doing my research I found that HEAD means the name of the current branch.做我的研究,我发现 HEAD 表示当前分支的名称。 So, if it is right, it doesn't make any sense to me.所以,如果它是对的,那对我来说没有任何意义。

What is the difference between之间有什么区别

git push origin development

and

git push origin HEAD:development

git push origin development pushes development branch git push origin developmentdevelopment分支

git push origin HEAD:development pushes current branch to remote's development git push origin HEAD:development将当前分支推送到远程的development

Most likely you are not in local development branch.您很可能不在本地development分支。 Consider checking this with考虑检查这个

git status

command.命令。

git push remote-name some-name

attempts to push any local branch named some-name to a remote branch also named some-name resolved at remote-name/some-name .尝试将名为some-name任何本地分支推送到在remote-name/some-name解析的名为some-name的远程分支。 Under the hood, it's looking for branch names under refs at each location (local, remote) that match.在幕后,它在匹配的每个位置(本地、远程)的refs下寻找分支名称。

Since your local copy of development hasn't been modified, you get a message that it's up to date, nothing to push.由于您的本地development副本尚未修改,您会收到一条消息,表明它是最新的,无需推送。

The alternate version替代版本

git push remote-name HEAD:development

skips the part about looking for matching branch names in local and remote refs because you've given it an explicit branch via HEAD .跳过有关在本地和远程refs查找匹配分支名称的部分,因为您已通过HEAD为其指定了显式分支。

It still does use remote refs to determine the remote branch development .它仍然使用远程refs来确定远程分支development But then it uses HEAD of the current branch (your new test branch) for the commits to be pushed.但随后它使用当前分支(您的新test分支)的HEAD来推送提交。

If you want to simply do git push from test branch and correctly push from test to remote/development , you can configure the default behavior of push to the "upstream" setting (note that the term "tracking" is an older term for "upstream" which is preferred).如果您只想从test分支执行git push并正确地从test推送到remote/development ,您可以配置push送到“上游”设置的默认行为(请注意,术语“跟踪”是“上游”的旧术语"这是首选)。

Do

git config --global push.default upstream

to ensure that a plain git push will only attempt to push from the current branch to its registered upstream branch.确保普通的git push只会尝试从当前分支推送到其注册的上游分支。

Without the setting, prior to git version 2.0, the default mode for a plain git push results in attempting to push all local branches to upstream branches with matching names at their configured remotes.如果没有设置,在 git 2.0 版本之前,普通git push的默认模式会导致尝试将所有本地分支推送到在其配置的远程具有匹配名称的上游分支。 For git 2.0 and newer, the default setting is simple which acts like upstream except that it also fails to push if the current branch doesn't share the same name as its configured remote.对于 git 2.0 及更新版本,默认设置很simple ,它的作用类似于upstream ,但如果当前分支与其配置的远程分支不共享相同的名称,则它也无法推送。

The "upstream" setting is a good one because (a) it allows easily pushing branches regardless of whether their names match to their upstream names; “上游”设置是一个很好的设置,因为(a)它允许轻松推送分支,无论它们的名称是否与其上游名称匹配; (b) for versions prior to 2.0, it restricts a bare invocation of git push to affecting only the current branch, so if you're actively developing on multiple branches, git push will no longer unintentionally push other work unrelated to current branch work; (b) 对于 2.0 之前的版本,它将git push的裸调用限制为仅影响当前分支,因此如果您正在多个分支上积极开发, git push将不再无意中推送与当前分支工作无关的其他工作; (c) you will not need the form of git push that specifically names the remote and branch unless you're attempting to push to some branch that is not the configured upstream for your current branch -- and I'd argue this makes the most sense: you should only need to verbosely spell out the branches if you're not pushing from a local branch to its natural, configured upstream target. (c) 您不需要专门命名远程和分支的 git push 形式,除非您尝试推送到某个不是当前分支配置的上游的分支——我认为这是最有效的意义:如果您不是从本地分支推送到其自然配置的上游目标,则只需要详细说明分支。 Even the 2.0 simple setting doesn't satisfy this.即使是 2.0 的simple设置也不能满足这一点。

remote-name is often origin , but can be any configured remote, added via various git commands or directly as in a .gitconfig file. remote-name通常是origin ,但可以是任何配置的远程,通过各种 git 命令添加或直接在 .gitconfig 文件中添加。

git push origin development

will try to push your local development branch.将尝试推动您的本地开发分支。 By this type of git push command, git will think local branch and remote tracking branch have the same branch nanme.通过这种git push命令,git 会认为本地分支和远程跟踪分支具有相同的分支名称。

From manpage of git push来自 git push 的联机帮助页

git push origin master Find a ref that matches master in the source repository (most likely, it would find refs/heads/master), and update the same ref (eg refs/heads/master) in origin repository with it. If master did not exist remotely, it would be created.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM