I was looking for a way to quickly see a state of repository in terms of branches and tags (that is, getting a list of all existing branches and tags, as well as the commits they point to with dates), so as to see if there are later commits than the current HEAD after cloning; and after some research, came up with the following command (git version 1.9.1):
git --no-pager log \
--simplify-by-decoration \
--tags --branches --remotes \
--date-order \
--decorate \
--pretty=tformat:"%Cblue %h %Creset %<(25)%ci %C(auto)%d%Creset %s"
Note that, apparently, --all
can be used instead of --tags --branches --remotes
(and also, tformat
because custom log format omits newline at end of output)
The output is like this:
However, when I look at How can I get a list of git branches, ordered by most recent commit? , most answers use git for-each-ref
, so I'd like to confirm - is there anything the above git log
command would miss, in terms of all branches, tags and remotes? If so, is git for-each-ref
the command I should use, or is there some other command to list them all?
(Bonus question - is it possible to modify this format, to also print the branch in which a given tag was added? Also, any way to get the star|asterisk indicating the current checked out state when using git branch -a
?)
However, when I look at How can I get a list of git branches, ordered by most recent commit?, most answers use git for-each-ref, so I'd like to confirm - is there anything the above git log command would miss, in terms of all branches, tags and remotes?
No. Branches are references in refs/heads/
, tags are references in refs/tags/
, and remotes are references in refs/remotes/
, so if those are what you want, --branches --tags --remotes
does the trick.
If so, is git for-each-ref the command I should use, or is there some other command to list them all?
Given that you also want to examine the commit each reference locates, there's no good reason to use git for-each-ref
here. What for-each-ref
would let you do, that you cannot do with git log
, is identify tags that do not point to (or indirectly to) commits. (A tag or annotated tag can be used to tag a tree or blob. Trees and blob are not commits and do not have date stamps, and it's not clear what you would do with these.)
Given that you further want the commits sorted by commit time-stamp (--date-order
),1 git log
is clearly the right tool.
Bonus question - is it possible to modify this format, to also print the branch in which a given tag was added?
No: this information does not exist. A tag simply points to an object (an annotated tag object if it's an annotated tag, or a commit, tree, or blob if not; if it's an annotated tag, the annotated tag object then points to another object). Branch names simply point to a commit object (and the commit itself points to zero or more previous, aka parent, commits). The objects are (mostly) permanent; all else is changeable. Zero or more reference names (including branch names) may point to any given commit, and the set of names may be updated at any time.
Object reachability—i.e., whether there are any external names that point directly to the object, or to another object through which the object can be found by following chains of parent pointers—determines whether an object survives "garbage collection". This is the only way to discard temporary objects: they are permanent until the garbage collector discovers that they are unreferenced, and discards them.
Also, any way to get the star|asterisk indicating the current checked out state when using git branch -a?)
Not from git log
, but you could do post-processing: read the current branch name out of HEAD
and have some code mark whichever git log
line corresponds. If you choose to do this, you should consider what to do with a "detached HEAD", which occurs when HEAD
does not contain a branch name after all, but instead contains a raw commit hash ID.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With