How do I list all remote refs?

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.


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/*


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-refs 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.

Tags:

Git

Github

Refs