Michael come lately
Michael come lately

Reputation: 9412

Find when a branch was merged

There are lots of scripts floating around on the internet for deleting already-merged branches such as

$ git branch --merged master | grep -v master | xargs -n 1 git branch -d

but I'd like to keep my branches around for a while before doing the cleanup. So:

How can I find when a particular branch was merged? I'd like to be able to get the hash and the date of the merge commit. Extra credit for the ability to pipe several branches in. I'm ultimately going for

$ git branch --merged master | [find dates for each]
    | [compare dates to arbitrary date] | [delete old merged branches]

I realize that the standard practice is to tag/delete branches that you want to keep around a little longer, but if I did that, I'd still be asking this question about the hash and time of the merge commit.

Edit:

I've been looking through these threads, because it strikes me that I'm looking for the child commit of the branch reference. Unfortunately, as mentioned in this comment, --children only adds the children to the commits returned by a log or rev-list, instead of only returning children.

Upvotes: 3

Views: 7666

Answers (2)

pavol.kutaj
pavol.kutaj

Reputation: 539

It may be too trivial, but what about starting with your merge history with something like

git log --merges --name-status --date=iso --pretty=format:"%C(auto)%h%d%Creset %C(cyan)(%cd)%Creset %C(green)%cn <%ce>%Creset %s" 

Couldn't you extract the info from that output?

Upvotes: 1

Michael come lately
Michael come lately

Reputation: 9412

Update 2015-05-15

A better answer, directly referring to parent commits of a branch is this:

$ git rev-list -1 --format=%p <branch_name> | grep -v commit |
  xargs -I {} sh -c 'git rev-list -1 --format="%h %ct" {}' | grep -v commit

and you can still pipe all the branches merged to master into it:

$ git branch -a --merged master | grep -v master | 
  xargs -I {} sh -c 'git rev-list -1 --format=%p {}' | grep -v commit |
  xargs -I {} sh -c 'git rev-list -1 --format="%h %ct" {}' | grep -v commit

This may work even for branches that have become part of your tree from a fast-forward.

If there's an argument you can pass to rev-list that prevents you from having to grep -v commit each time, I'd be interested to know it.

Note: Instead of grep -v master, I search for branch names that match issue. If you are looking to include or exclude specific branches, you should do the same.


Original

To get the hash, it seems you can do the following:

$ git log --ancestry-path --merges --format=%H <branch_name>..master | tail -1

or you could replace the format with whatever format you were looking for. The --merges just reduces the number of lines returned before we pick the last one.

You can pipe the list of merged branches with:

$ git branch -a --merged master | grep -v master |
  xargs -I {} sh -c 'git log --ancestry-path --format=%H --merges {}..master | tail -1'

but I haven't done the actual pruning. =)

Upvotes: 3

Related Questions