git - 以编程方式获取 git 将使用 `git checkout -` 跳转到的修订版

标签 git

如果我使用 git checkout -,git 会跳回到我之前所在的版本。如果它是分离的 HEAD(在这种情况下 HEAD 将在同一修订版中分离)或分支(在这种情况下 HEAD 将返回到该分支上),则为真。

我希望能够知道我刚刚所在的版本,如果我运行它,git 将跳转到哪个版本,理想情况下,无需实际更改 HEAD。

因此,如果 HEAD 在一个分支中是最后一个,我想获取该分支的名称;如果不是,我想获得该提交的 SHA。

最佳答案

@{-1} 对应于感兴趣的修订。来自 git-checkout 手册页:

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}.

(强调我的)

但是,我不清楚您是否可以恢复上次 checkout 的分支名称(如果有)。 git-reflog 手册页提到了这一点

the HEAD reflog records branch switching

但 Git 似乎没有提供任何功能来利用该信息...

关于git - 以编程方式获取 git 将使用 `git checkout -` 跳转到的修订版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50020556/

相关文章:

ruby - GIt 部署 + 配置文件 + Heroku

git - 如何在交互式 rebase 压缩后保留提交 gpg 签名?

Git SVN 错误 : a Git process crashed in the repository earlier

即使我是项目所有者,GitLab 也拒绝推送 master

git - 如何进行递归 'git checkout'?

windows - 滚动时 msysgit 中的乱码文本

git - 正在进行中。无法提交。如何进行或停止(abort)?

c++ - 为团队的其他成员恢复对 Eclipse 项目库的更改

Gitlab推送不起作用, pull 完美

git - 如何在 Git 中获取特定提交的父级