简体   繁体   中英

Find which branch HEAD was detached from in Git

After setup of an OpenEmbedded project, the sources are set to detached head state. 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 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

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:

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.

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:

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:

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

Solution 2: find all branches that contain current commit

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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