Skip to content

Commit 872a1d2

Browse files
committed
Add Count Number Of Commits On A Branch as a git TIL
1 parent d52a126 commit 872a1d2

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1565 TILs and counting..._
13+
_1566 TILs and counting..._
1414

1515
See some of the other learning resources I work on:
1616
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
@@ -304,6 +304,7 @@ See some of the other learning resources I work on:
304304
- [Configuring The Pager](git/configuring-the-pager.md)
305305
- [Copy A File From Another Branch](git/copy-a-file-from-another-branch.md)
306306
- [Count All Files Of Specific Type Tracked By Git](git/count-all-files-of-specific-type-tracked-by-git.md)
307+
- [Count Number Of Commits On A Branch](git/count-number-of-commits-on-a-branch.md)
307308
- [Create A New Branch With Git Switch](git/create-a-new-branch-with-git-switch.md)
308309
- [Delete All Untracked Files](git/delete-all-untracked-files.md)
309310
- [Determine The Hash Id For A Blob](git/determine-the-hash-id-for-a-blob.md)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Count Number Of Commits On A Branch
2+
3+
The `git rev-list` command will show all commits that fit the given revision
4+
criteria. By adding in the `--count` flag, we get a count of the number of
5+
commits that would have been displayed. Knowing this, we can get the count of
6+
commits for the current branch like so:
7+
8+
```bash
9+
$ git rev-list --count HEAD
10+
4
11+
```
12+
13+
This finds and counts commits from `HEAD` (usually the top of the current
14+
branch) all the back in reverse chronological order to the beginning of the
15+
branch (typically the beginning of the repository). This works exactly as
16+
expected for a the `main` branch.
17+
18+
What about when we are on a feature branch though?
19+
20+
Let's say we've branched off `main` and made a few commits. And now we want the
21+
count.
22+
23+
```bash
24+
$ git rev-list --count HEAD
25+
7
26+
```
27+
28+
Unfortunately, that is counting up the commits on the feature branch but it
29+
keeps counting all the way back to the beginning of the repo.
30+
31+
If we want a count of just the commits on the current branch, then we can
32+
specify a range: from whatever `main` was when we branched to the `HEAD` of
33+
this branch.
34+
35+
```bash
36+
$ git rev-list --count HEAD
37+
3
38+
```
39+
40+
This is the same as saying, I want all commits on `HEAD`, but exclude (`^`) the
41+
commits on `main`:
42+
43+
```bash
44+
git rev-list --count HEAD ^main
45+
3
46+
```
47+
48+
See `man git-rev-list` for more details.

0 commit comments

Comments
 (0)