简体   繁体   English

在Git中查找HEAD分离的分支

[英]Find which branch HEAD was detached from in Git

After setup of an OpenEmbedded project, the sources are set to detached head state. 设置OpenEmbedded项目后,源将设置为分离头状态。 How do you determine from which branch, either local or remote, the source code is checked out from? 如何确定从哪个分支(本地或远程)检出源代码?

Yes you can check out the branches and and compare the code. 是的,您可以查看分支并比较代码。 Is there a simpler approach? 有更简单的方法吗?

I'm using Git version 1.7.1. 我正在使用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

Solution 1: git reflog 解决方案1:git reflog

If you haven't been moving your HEAD reference around since your working copy entered detached head state, you could use git reflog -1 to find where your head was detached at: 如果你没有移动你的HEAD引用,因为你的工作副本进入了分离头状态,你可以使用git reflog -1来找到你的头被分离的位置:

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

Note that I'm not sure if this information is available in older versions of Git. 请注意,我不确定这些信息是否在旧版本的Git中可用。

Even if you have been moving your HEAD reference around, you might still be able to find the point where you detached somewhere in the reflog: 即使你一直在移动你的HEAD引用,你仍然可以找到你在reflog中某处分离的点:

git reflog | grep checkout

For users that don't have access to grep , you can use findstr in PowerShell, or use these flags built into Git: 对于无权访问grep ,可以在PowerShell中使用findstr ,或者使用Git中内置的这些标志:

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

Solution 2: find all branches that contain current commit 解决方案2:查找包含当前提交的所有分支

Find all branches (both local and remote-tracking branches) that contain the commit you currently have checked-out: 查找包含您当前已签出的提交的所有分支(本地和远程跟踪分支):

git branch --all --contains HEAD

Then just check the first commit of each branch to see if the given commit is the head of that branch: 然后只检查每个分支的第一次提交,看看给定的提交是否是该分支的头部:

git log -1 <branch>

Thanks to torek for simplified solution . 感谢torek提供的简化解决方案

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

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