Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong count git commits [duplicate]

I have a small bash script:

echo "Total commits: "
git log --all --pretty=format:"%h %ad | %s%d [%an]" --date=short | wc -l
echo "Total no-merge commits: "
git log --all --pretty=format:"%h %ad | %s%d [%an]" --date=short --no-merges | wc -l
echo "Total merge commits: "
git log --all --pretty=format:"%h %ad | %s%d [%an]" --date=short --merges | wc -l

I know that the code is not optimal. The Result of my script:

Total commits:  
1000
Total no-merge commits:  
817
Total merge commits: 
182

Question: why is the sum of no-merge and merge commits (182+817 = 999) lower than the total commits (1000)?

like image 466
Dmitry Avatar asked Dec 17 '14 09:12

Dmitry


People also ask

Does cherry-pick duplicate commits?

The short answer is: as rarely as possible. The reason why you should use cherry-pick rarely is that it easily creates "duplicate" commits: when you integrate a commit into your HEAD branch using cherry-pick, Git has to create a new commit with the exact same contents.

How do I reduce the number of commits?

Let's explain with the help of an example, suppose you have n number of commits and when you apply git squashing on them, you can squash or compress all 'n' commits into a just single commit. Git squash is used to change several large commits into a small single meaningful commit. So, you can make the git log clearer.


1 Answers

The output from those log commands uses \n as a separator, not a terminator, so your wc -l counts are all one short. You really have:

1001 = 818 + 183

commits, which adds up.

From git help log:

The tformat: format works exactly like format:, except that it provides "terminator" semantics instead of "separator" semantics. In other words, each commit has the message terminator character (usually a newline) appended, rather than a separator placed between entries. This means that the final entry of a single-line format will be properly terminated with a new line, just as the "oneline" format does.

like image 51
Joe Avatar answered Sep 27 '22 22:09

Joe