Get git commits between "cuts" of a reference branch

80 views Asked by At

We have a master branch from which we periodically branch out ("cut") release branches. All the release branches are guaranteed to have the prefix release/.

Suppose that at some time in the past we cut release branch release1. A few commits are added to master and now we want to cut release branch release2.

Is there a way to retrieve the list of commits between those cuts? There might have been arbitrarily many commits, so simply doing a git log -10 to get the 10 latest commits will not quite do. I also don't have any guarantees about when exactly the last cut was made. I don't know if it happened yesterday, last hour, last year...

3

There are 3 answers

2
Jason On BEST ANSWER

We are quite fortunate to be mechanically creating release branches through a dedicated Jenkins job that makes them in the format release/yyMMdd-HHmmss. Consequently:

$ git fetch
$ LAST_RELEASE=`git branch -a | grep -E origin/release/[0-9]{6}-[0-9]{6} | sort | tail -1
$ git log $LAST_RELEASE..master

does the trick.

0
Useless On

Firstly, you should tag your branch points. It'll make life much easier in the future.

Secondly, "cut" isn't git terminology - you're not making a master for producing vinyl LPs. You're creating release branches, right? This operation is called branching.

Anyway, if you want to find the last common ancestor of your current master and a given release branch (which will be the branch point you ought to have tagged)

$ git merge-base master release1

will tell you. The list of commits is just

$ git rev-list master ^$(git merge-base master release1)

or whatever

1
Tushar Shahi On

Read up on limiting commits from git log.

Use --since and --until:

git log --since "t1" --until t2"

The date format are quite flexible, words like yesterday are also taken.

One example:

git log --since "2023-11-06" --until "2023-12-06"