git: Too Many Topic Branches

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.

2 thoughts on “git: Too Many Topic Branches”

  1. Mark,

    I’m trying to extend the range of a BT Home Hub 3 using a TP-Link WA701ND, and following your instructions — and I’m struggling.

    Firstly, you say: “Find an ethernet cable, and use that to plug a laptop into the TP Link device.”

    I do that, but I’m uncertain what the purpose of it is. I can’t see the ethernet connection to the TP-Link, only the wireless connection.

    “Configure the laptop to have a static IP address of 198.168.1.100”

    Does it matter whether I use the properties box of the TP-Link or the HomeHub?

    Either way, setting the above makes it impossible to visit http://192.168.1.254. I can’t get a connection.

    The only way I can get a connection, is to reset the static IP address to automatic, but then I can’t access the TP-Link configuration page.

    Catch 22

    Any advice would be much appreciated. I’m sure I’m doing something silly, but setting up networks is not my forte.

    Thanks

    John Davidson

  2. Since I came across your arlctie looking for something else and it is pretty well referenced despite its age, I’ll add my 2 cent of update :)There is an easier way to do this since version 1.7.0 of git (at least more understandable for my poor brain):1- Create the local branch from the current one1git checkout -b new_feature2- Push that branch to the remote1git push origin new_featureAt this stage your local branch is set to push to the remote but pull won’t work3- Set the upstream branch to track1git branch –set-upstream new_feature origin/new_featureNow you can both push to / pull from the remote branch4- If you need to delete the remote branch1git push origin :new_feature

Leave a Reply

Your email address will not be published. Required fields are marked *