Get the difference in lines of code between 2 commits by Git date

I am new to git and need help with the command.

So, I've cloned a repository in Git, and I want to see the difference in inserts and deletes between the two commits closest to the two dates.

I know how to get history commit by committing between two dates like below:

$ git log --since "JAN 1 2014" --until "DEC 31 2014" --oneline --shortstat origin/master

      

But how can I compare the 2 commits closest to JAN 1 2014 and the second closer to DEC 31 2014 and get the total difference between all files in both versions of the commit? Don't commit and add upp the total difference, but only the difference in commit 1 (Jan 1) and commit 2 (Dec 31) and skip all commits between those commits on the same line, for example

51647f3: 340 files changed, 1316 insertions(+), 6676 deletions(-)

      

It also begs the question, do inserts also include changed rows or only actual newlines?

Thanks in advance.

+3


source to share


1 answer


You can use git diff

and provide a range of commits using the notation ..

:

git diff <firstcommit>..<secondcommit> --shortstat

      

In Git, a commit points to a snapshot of the repository directories and files. When you compare two commits, you are actually comparing the state of the files as they were at different points in time, regardless of how many commits (ie "Snapshots") occurred between them.

You can find the SHA-1 hash of the first commit that happened after or before a specific date starting at origin/master

using git rev-list

:

git rev-list --since="jan 1 2014" --reverse origin/master | head -1
git rev-list --until="dec 31 2014" -n 1 origin/master

      



where head

used in the first case instead of a radio button -n

to select only the first line in the output. The reason is that it --reverse

is applied after filtering parameters :

Note that they are applied prior to commit and commit formatting options such as --reverse

.

Regarding your second question, according to the unified diff format , a modified line is considered an insert ( +

) for a new line and as a delete ( -

) for an old one:

+ This is a modified line
- This is a line

      

Thus, it will count for both.

+3


source







All Articles