Vasyl Stepulo
Vasyl Stepulo

Reputation: 1603

Split commit history by branch

I have a script, which gets all commits from selected repo for some period.

To do this, I use command:

 git --no-pager log --oneline --before 2018-07-20 --after 2018-07-18 --all --branches=* --remotes=*

But it gives output, not splited by branch. How can I rewrite this command, to receive something like:

Branch development
MQP-1896 hdveyhbkehbkke
MQP-1895 fdghdfjfgj
Branch qa
MQP-1836 loejemgdsgsnlee
Branch feature
MQP-1833 fghdfghfghfh

Upvotes: 0

Views: 114

Answers (2)

Jaspreet Singh
Jaspreet Singh

Reputation: 409

Couldn't find any thing in documentation where we could get commits listed like this, if anyone find this do comment. You can write a bash script where you checkout all branches and git log on each of them

Sample (can be improved)

array=(`git branch`)
for i in "${array[@]}"
do :
echo $i
git --no-pager log $i --oneline --before 2018-07-20 --after 2018-07-18
done

write this code in file-name.sh and run it using sh file-name.sh

Upvotes: 1

Mark Adelsberger
Mark Adelsberger

Reputation: 45719

git log generally does not split output by branch. You can use --topo-order to order the output in a way that should keep the commits from a branch together[1]. However, this still doesn't show the branch names.

git log --topo-order --oneline --before 2018-07-20 --after 2018-07-18 --all

(Note: The branches=* argument only means to include anything reachable from any branch (any ref under refs/heads/) in the output; this is redundant once you've specified --all, as is remotes-*.)

To get branch information, you could use a different formatting option, such as

git log --topo-order --format="%d %h %s" --before 2018-07-20 --after 2018-07-18 --all

This still isn't exactly what you asked for; the branch name will not get its own line. Rather it appears in () at the beginning of the line for the commit the branch ref points to. Two big caveats:

1) If the branch tip is omitted (e.g. because it's after the --before date) then the branch name won't be output

2) What's in the () is a list of all refs (branch or otherwise) that point directly to that commit

That's about as close as you can get with just a git log command. You could put together a script to iterate over the branches and produce a log for each one. This is similar to what jaspreet's answer proposes, but

1) I don't recommend using git checkout in the script, as the checkouts may lead to issues if you have local uncommitted changes. Instead you could drive the script with something like git for-each-ref refs/heads and use git log <branch-name>

2) The script will end up making assumptions, so you'll have to decide what sort of assumptions are right for your repo and adjust the script accordingly.


[1] "commits from a branch" is speaking pretty loosely, or at least making some implicit assumptions. In git commits aren't "part of a branch" as they might be in some other tools; a branch just points to one commit, and other commits might be reachable from that commit via parent pointers. Being reachable by first parent pointers is roughly similar to being "part of a branch", but not all such commits were necessarily "part of the branch" at the time they were created - and in particular this definition includes the commits from before the branch was created on its "parent branch"[2].

[2] "parent branch" is speaking even more loosely; depending on your branching conventions it might or might not be a phrase that makes sense.

Upvotes: 0

Related Questions