How do I find all heads that point to the same treeish

Let’s say, for example, that I’ve got branches master, foo, and bar.

master
  \--foo
      |
     bar

foo and bar are the same thing. Imagine some work was committed on foo, then the user did git checkout -b bar but never did anything to bar. So, they both point to the same treeish.

Now, lets say time passes and I’ve forgotten what the name of that other branch was that was the same as foo, or maybe I just want to clean up my branches.

What command do I need to execute when on the foo branch, to see all the other branches that point to the same treeish (in this case just bar)?

How / why is this useful? Well, since pull requests on Github are branches that can be tracked, I can look at an old branch I have sitting around, run this command, and have it show me origin/pr/42 and now I have the id of the old pull request (42) to go look at it on Github.

  • How to solve a revert commit conflict
  • Creating Github Repository from command line
  • Key is already in use when trying to add SSH Key to Github
  • I forked a project, then push some changes, can the project owner see my update?
  • git rebase issue reordering
  • Not able to clone through git
  • git push error “error: src refspec xxx does not match any.” using git flow
  • Git log: list all parents commits
  • 3 Solutions collect form web for “How do I find all heads that point to the same treeish”

    A git idiomatic way is to use for-each-ref with rev-parse to find all the references that share the tree-ish. For instance, assuming bash as the shell:

    git for-each-ref --format="%(refname)" | \
    xargs -I refname \
      sh -c '[[ $(git rev-parse HEAD^{tree}) == $(git rev-parse refname^{tree}) ]] && echo refname'
    

    This will list all the refs, including remote refs that share the tree-ish.

    Here’s a sample output:

    $ git branch foo
    $ git for-e...
    refs/heads/foo
    refs/heads/master
    refs/remotes/origin/master
    $ git checkout foo
    $ git branch bar
    $ git for-e...
    refs/heads/bar
    refs/heads/foo
    refs/heads/master
    refs/remotes/origin/master
    $ echo “new content” >> README && git add README && git commit –m “New tree-ish”
    $ git for-e...
    refs/heads/foo
    
    git log --all --simplify-by-decoration --pretty=format:'%T %h %d %s' \
    | awk '
         $3~/,/        { seen[$1]=2 }         # late addition to handle same-commit refs
         ++seen[$1]==1 { save[$1]=$0; next }
           seen[$1]==2 { print save[$1] }
                       { print }
    '
    

    This probably isn’t something fancy but I would get the hash for the commit I’m currently on
    like this for example

    git log -1 --oneline
    

    then take the hash and do

    git branch --all --contains [hash]
    
    Git Baby is a git and github fan, let's start git clone.