Git checkout: “@{-N}” syntax

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

I found this section in the git manpage, but it's kind of an obfuscated explaination:

As a special case, the "@{-N}" syntax for the N-th last branch checks out the branch (instead of detaching). You may also specify - which is synonymous with "@{-1}".

What does it mean?

回答1:

It's saying that if you do:

git checkout @{-1} 

You'll check out the branch you were on before the current branch you have checked out. In addition, it checks it out by name, so you'll actually be on that branch, rather than just checking out that commit in a detached HEAD state. Similarly, you could use git checkout @{-2} to go back "two branch changes ago", and so forth.



回答2:

That somewhat "obfuscated" explanation is clarified with Git 2.16 (Q1 2018).
@{-N} in "git checkout @{-N}" may refer to a detached HEAD state, but the documentation was not clear about it, which has been fixed.

See commit 75ce149 (27 Nov 2017) by Kaartic Sivaraam (sivaraam).
(Merged by Junio C Hamano -- gitster -- in commit 7065665, 13 Dec 2017)

Doc/checkout: checking out using @{-N} can lead to detached state

@{-N} is a syntax for the N-th last "checkout" and not just the N-th last "branch". Therefore, in some cases using git checkout @{-$N} DOES lead to a "detached HEAD" state.
This can also be ensured by the commit message of 75d6e55 (Documentation: @{-N} can refer to a commit, 2014-01-19, Git 1.9-rc1) which clearly specifies how @{-N} can be used to refer not only to a branch but also to a commit.

Correct the misleading sentence which states that @{-N} doesn't detach HEAD.

The git checkout man page now reads:

You can use the "@{-N}" syntax to refer to the N-th last branch/commit checked out using "git checkout" operation.
You may also specify - which is synonymous to "@{-1}.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!