28

Recently I created a PR on GitHub, but some tests were failing. And locally even more tests didn't pass. So I tried to figure out what's the issue.

And the thing I found out is that Travis was testing the repo being at other commit I don't have. The ref is refs/pull/81/merge. So, somebody supposedly merged my branch into the master and created corresponding ref. Was it GitHub or Travis?

And the other question, can I list all the refs GitHub repo has?

3 Answers 3

22

If you just want a list of all refs in your repo you can run:

git show-ref

This will print the <SHA> <NAME> of every ref under .git/refs/*

2
  • Not all refs are in .git/refs/* - some may be in .git/packed-refs Sep 26, 2019 at 23:38
  • 2
    This is not what OP asked for -- they asked for refs on the remote; not on the local repository. May 8, 2020 at 12:29
21

I do not know anything about Travis itself, but GitHub does in fact create these refs/pull/number/merge refs. In particular, based on observation—the documentation seems a bit skimpy and I am only a somewhat casual user of GitHub—when you click on "new pull request" in GitHub, GitHub:

  • creates a reference named refs/pull/number/head to save the ID of the commit you are asking someone else to merge for you;
  • attempts to do an automatic merge-and-commit (using git merge), and if that succeeds, creates a second reference named refs/pull/number/merge.

Note that this automatically-created merge commit is not on any branch, it merely has the same pull-request number assigned, and a reference whose name is obvious from the pull-request itself (simply replace head with merge). I believe—but have not tested manually—that the first parent of this merge is the tip commit of the branch you are requesting someone be "on" when they do the same merge, i.e., the code behind the scenes that creates this refs/pull/number/merge reference is (or is close enough to):

commithash=...                         # the commit ID for the pull-request
mergeinto=$(git rev-parse $branchname) # the branch we are to merge into
prnum=...                              # get next pull request number

# create the pull request itself
git update-ref refs/pull/$prnum/pull $commithash

# create the merge ref, if the merge succeeds
git checkout $mergeinto
if git merge -m "..." refs/pull/$prnum/pull; then
    # merge succeeded: create the merge ref
    git update-ref refs/pull/$prnum/merge HEAD
else
    # automatic merge failed, discard it
    git merge --abort
fi

(this particular code sequence messes about with the index, and leaves HEAD detached, so it has to be done with the repository locked and post-work cleanup, or using a temporary work-tree; the actual code sequence is probably different for any number of reasons).

Hence:

So, somebody supposedly merged my branch into the master and created corresponding ref. Was it GitHub or Travis?

Given that GitHub will, William of Ockham would suggest there is no need to invoke Travis. :-)

And the other question, can I list all the refs GitHub repo has?

As long as you have access:

$ git ls-remote

(assuming the remote is a GitHub URL) will show all the exposed references. I have not seen a Web interface way to do this, nor found one in my (light) perusal of the GitHub API documentation.

2
  • I see, git ls-remote must either be run from remote repo, or one can use git ls-remote REMOTE_NAME from within forked repository.
    – x-yuri
    Mar 11, 2017 at 17:19
  • 2
    @x-yuri: more precisely, git ls-rmote looks up the current branch's remote setting unless you give it an argument. The argument can be the name of a remote (like origin or upstream), in which case git ls-remote looks up the URL from that, or may be a URL. Once Git has the URL, it invokes the first part of the usual git fetch operation, requesting a list of all public references from the other Git.
    – torek
    Mar 12, 2017 at 2:32
6

One answer mentions git ls-remote - while this works in general for my use case I needed to list what git thinks the remote has - for ex. when the server's admin cleans up my stale remote repositories and I want to restore them using my local git clones.

To list all known remote refs I used to use ls .git/refs/remotes/<name>/ - turns out in some case it's not sufficient, ex on a git-p4 repository one of my remote's refs were only listed in .git/packed-refs.

Using git branch -r would work but the output is not cleanly parsable and requires grep'ing the desired remote refs.

After a bit of digging I ended up using git for-each-ref:

git for-each-ref --format="%(refname)" refs/remotes/<name>/

NB: git show-ref can also list refs but the pattern is anchored to the end of the full ref name and only full components are matched, so it's only useful if you know what branch name you want or to list all refs and grep them. I wanted something tidy that fits well in a oneliner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.