4

How can I get the names of all branches that someone made a commit to in the last month?

Alok Swain
  • 6,409
  • 5
  • 36
  • 57
bloodydziq
  • 53
  • 5
  • Not sure, but I feel it might need some custom logic. Git does not provide this out of the box. A reporting API on top of Git is a good idea. – Sid Nov 28 '16 at 12:28

5 Answers5

2

Disclaimer: This is not a full-baked answer, just an idea.


A little technical background before the actual answer.

A Git branch is just a pointer to a commit. It can change from one history branch to another and, apart from an entry in the branch's reflog (if it is enabled), the movement of a branch doesn't leave any trace in the repository.

To make it clear, let's say the repo looks like this:

               +-- branch X
               v
A -- B -- C -- D 
       \
        - E -- F
               ^
               +-- branch Y

This simple sequence of commands:

git checkout X
git reset --hard F
git checkout Y
git reset --hard D

makes branches X and Y swap their places. Now the repository looks like this:

               +-- branch Y
               v
A -- B -- C -- D 
       \
        - E -- F
               ^
               +-- branch X

The commit E, for example, now belongs to branch X and cannot be reached from branch Y but it belonged to branch Y (and couldn't be reached from branch X) when it was created.

This makes your question indeterminate.


The answer

The command to use is git log with some parameters:

--since=<date>
--after=<date>

Show commits more recent than a specific date.

--until=<date>
--before=<date>

Show commits older than a specific date.

--author=<pattern>
--committer=<pattern>

Limit the commits output to ones with author/committer header lines that match the specified pattern (regular expression).

--all

Pretend as if all the refs in refs/ are listed on the command line as <commit>.

--walk-reflogs

Instead of walking the commit ancestry chain, walk reflog entries from the most recent one to older ones.

The command line should be something like this:

git log --since='1 month ago' --author=john.doe@example.org --all 

You can put the complete name or email address of the author or just a fragment of it in the --author argument.

--all tells git log to search the commits accessible using all the current branches

If you add --walk-reflogs to the command line and replace --all with a specific branch name (f.e. Y in the example from the first part of this answer), Git follows the movement history of the Y branch (recorded in the reflog) and returns commits F, E, B and A. Without --walk-reflogs Git follows the commits accessible from the current position of branch Y and returns the commits D, C, B and A (assuming all of them match the other conditions).

Be aware that adding --walk-reflogs can return incomplete results or nothing. The branch's reference log contains the movement of the branch (because of commit, reset, rebase and other commands) on the local repository only, it can be disabled or cleared

Community
  • 1
  • 1
axiac
  • 68,258
  • 9
  • 99
  • 134
  • thx for your help :D I manage to write something like this with help from someone else answer git log --since='2016-11-01' --author=piotr.stolarczyk@novo-technologies.com --all --format="%H" | while read i; do git branch --contains $i; done | sort -u this is what I was looking for :) – bloodydziq Nov 29 '16 at 12:52
1

Is that enough: git branch -av? It also shows potential remote branches. -a stands for all, -v for verbose. See here for more details.

Edit: I think this answer gives a good overview over further possibilities. However, the branch creator is not tracked. Depending on the question git blame file might be interesting...

Community
  • 1
  • 1
Christoph
  • 6,841
  • 4
  • 37
  • 89
1

You can use the "--since" option to git log to select commits within the last month:

git log --since="`date -d'1 month ago'`"

Then for each of those revisions you can use "git branch --contains" to show which branches it was in. e.g:

git branch --contains 00735a997d0bba684dbf62552eea83b9491ac26b

You can put this all together in a single line which will do it all for you and de-duplicate the output:

git log --since="`date -d'1 month ago'`" --format="%H" | while read i; do git branch --contains $i; done | sort -u
sheltond
  • 1,877
  • 11
  • 15
1

Actually it is not possible.

You can find out in which branches commits of a user are contained that have an author date within the last month or that have a commit date within the last month. Both is potentially not very accurate.

What you cannot do is to determine to which branch a commit was made in Git. A Branch is just a post-it note on a commit that moves on to the next commit automatically if the branch is checked out. But you can peel-off the post-it and place it anywhere you like at any time. So you cannot determine to which branch a commit was done and as commit and author dates can be arbitrarily set you cannot even reliably use those dates.

You can maybe do such a thing if you have strict commit policies and they are obeyed by all people.

Vampire
  • 35,631
  • 4
  • 76
  • 102
0

You can use since & untill flag to see the commits of a date range. And use author flag to filter the someone's commits.

$ git log --since=2.month.ago --until=1.month.ago --author=<user>    # relative time
$ git log --since=2000-01-01 --until=2012-12-21 --author=<user>      # absolute

Then if you want to know that which branch(es) contains a specific commit.

$ git branch --contains <commit-sha>          # show the branch list(s) containing the commit 
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73