Another couple of git tips, that might conceivably be useful to someone somewhere :)
git makes it so easy to create topic branches, that it’s easy to lose track of which branches were for what. Here are a couple of recipes that might help with this:
Order branches by last commit date
I often want to order my branches according to how recently I was working on them. We can approximate that by saying we’d like to order the branches by the commit date of each branch tip, and you can do that with git for-each-ref --sort=committerdate. For example, the shell script:
for C in $(git for-each-ref --sort=committerdate refs/heads --format='%(refname)')
do
git show -s --format="%ci $C" "$C"
done
… produces output like this:
... 2011-02-23 18:57:01 -0500 refs/heads/trainable-seg-gui 2011-03-09 11:25:38 +0100 refs/heads/snt-swing-menus3 2011-03-11 00:44:37 +0100 refs/heads/master
Show branches that introduced changes to particular paths
If that doesn’t turn up the branch I’m looking for, it might be useful to just list those branches that made changes to particular paths (with respect to master). Here’s a similar example of how to do this, in this case looking for all those branches that introduced changes to the path src-plugins/Simple_Neurite_Tracer:
P="src-plugins/Simple_Neurite_Tracer"
for C in $(git for-each-ref --sort=committerdate refs/heads/ --format='%(refname)')
do
git diff master..."$C" --quiet -- "$P" || echo $C
done
… which produces output like this:
... refs/heads/sholl-analysis refs/heads/for-rebasing refs/heads/sholl-analysis-wip refs/heads/sholl-analysis-wip2 ...
Ordering branches by the last time the branch was changed
This came up in a Stack Overflow question – sometimes you might want to know when the branch pointer was last changed, not the date of the last commit. Jefromi’s answer provides a nice recipe that you could alter to order the branches in that way.
Leave a Reply to Yailenis Cancel reply