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.

  • What are best practices for representing a git project's dependency on a local package with its own git repo?
  • git, how to overwrite commits
  • TFS2013 - Clone build definition for team project using git
  • How to cancel a local git commit
  • Simplest git workflow for commits signing?
  • How can I delete all of my Git stashes at once?
  • 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 put Emacs into magit's commit edit mode from emacsclient
  • GIT Bash asking for my user credentials when performing a push using SSH
  • Repo sync hangs
  • What should be committed to a repo?
  • creating a repo in gitlab using CLI
  • Accidentally merged github repo into personal project
  • 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.