git - 在 Git 中查找 HEAD 从哪个分支分离

标签 git

设置 OpenEmbedded 项目后,源将设置为分离头状态。您如何确定从哪个分支(本地或远程) check out 源代码?

是的,您可以检查分支并比较代码。有没有更简单的方法?

我使用的是 Git 1.7.1 版。

$ git config -l
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
remote.origin.url=git://arago-project.org/git/projects/meta-arago-amsdk.git
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.amsdk-06.00.00.00-integration.remote=origin
branch.amsdk-06.00.00.00-integration.merge=refs/heads/amsdk-06.00.00.00-integration

$ git branch -a
* (no branch)
  amsdk-06.00.00.00-integration
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/amsdk-05.06.00.00-integration
  remotes/origin/amsdk-05.07.00.00-integration
  remotes/origin/amsdk-05.08.00.00-integration
  remotes/origin/amsdk-06.00.00.00-integration
  remotes/origin/master
  remotes/origin/master-upstream

最佳答案

方案一:git reflog

如果在你的工作副本进入分离头部状态后你没有移动你的 HEAD 引用,你可以使用 git reflog -1 来找到你的头部在哪里分离于:

git reflog -1
d761f4a HEAD@{0}: checkout: moving from feature to head~5

请注意,我不确定此信息是否适用于旧版本的 Git。

即使您一直在移动您的 HEAD 引用,您仍然可以在 reflog 中的某处找到分离的点:

git reflog | grep checkout

对于无法访问 grep 的用户,您可以在 PowerShell 中使用 findstr,或使用 Git 中内置的这些标志:

git log --walk-reflogs --grep-reflog "checkout" --oneline

解决方案 2:查找包含当前提交的所有分支

查找包含您当前已 checkout 的提交的所有分支(本地和远程跟踪分支):

git branch --all --contains HEAD

然后只检查每个分支的第一个提交,看看给定的提交是否是该分支的头:

git log -1 <branch>

感谢torek for simplified solution .

关于git - 在 Git 中查找 HEAD 从哪个分支分离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18667578/

相关文章:

Git:管理推送事件的文件

git - 如何在 git 中将新的更改与以前的提交结合起来

git - 无法创建文件...权限被拒绝

git - 无法访问远程机器 git 存储库

git - 有了 CI 服务器就一定要在本地跑测试吗?

git - TortoiseGit 到 Gitlab 存储库 HTTP 基本访问被拒绝

git - 如何推送临时更改?

git - 我可以在 TortoiseGit 中使用(双点)范围符号进行修订吗?

git - 如何删除 Git 中的重复文件跟踪

linux - 如果存在 git 命令,则使 bash 命令回退到 git 命令