What is the right command to check a local tag vs the same on remote?

Viewed 50

I have a script that periodically compares a local branch with its remote counterpart to see if there's any update.

To do this I use the following:

  1. update the... remote references? with git remote update
  2. get the local commit with git rev-parse @
  3. get the corresponding remote (upstream) commit with git rev-parse @{u}
  4. compare the two hashes

and they appear quite efficient, especially git remote update which is very fast in retrieving information from the remote repository.

Now I want to do a similar thing with a tag (I'm evaluating the possibility to use them to mark special commits and be able to move that mark back and forth), and using a specific one, i.e. tagged, much like I was checking a specific branch in the first part.

But I did not find a similarly efficient sequence of commands. Let me be clear: I'm fine if a command takes several seconds to complete, I would just like to use the lightiest possible one. E.g. before finding git remote update, I was using fetch, which allowed me to accomplish the same result, but with much more burden (and network, repository, etc. load).

The tag is created and moved with these:

  1. git tag tagged <hash> -f
  2. git push --tags -f

While inspecting (obtaining the commit hash in) the working copy is as easy as before (with git rev-parse tagged), I don't know the command to check the same on the remote.

Since I'm using this in a script, a command with a single output would be the best.

The only one which seems to come closer is ls-remote, that I tried like this: git ls-remote --tags origin tagged; but it's a little slow and maybe it is doing more than I need.

The other command I tried is git rev-parse --tags, but it outputs a list, and doesn't seem to allow specifying to query the remote (of course with branches is easier, since they have an upstream associated).

1 Answers

Since you're scripting this, and it sounds like you want to loop over all the tags, I would run

git ls-remote --tags origin

once without specifying a tag, and loop over the output locally in your script.

The time to run that command and get all tag revs listed at once is only trivially longer than just getting one, so this approach should give you the optimization you're looking for. I just tested in a repo with 50 tags, and I fetched all 50 tags in 0.45s, while it took 0.37s to fetch just one tag, which would mean a total of 18.5s for all 50 tags.

The optimization most likely comes from establishing the connection to the server only once, as I'm sure that's the slowest part in all this.

Related