File tree 2 files changed +50
-1
lines changed 2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
10
10
11
11
For a steady stream of TILs, [ sign up for my newsletter] ( https://crafty-builder-6996.ck.page/e169c61186 ) .
12
12
13
- _ 1565 TILs and counting..._
13
+ _ 1566 TILs and counting..._
14
14
15
15
See some of the other learning resources I work on:
16
16
- [ Ruby Operator Lookup] ( https://www.visualmode.dev/ruby-operators )
@@ -304,6 +304,7 @@ See some of the other learning resources I work on:
304
304
- [ Configuring The Pager] ( git/configuring-the-pager.md )
305
305
- [ Copy A File From Another Branch] ( git/copy-a-file-from-another-branch.md )
306
306
- [ 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 )
307
308
- [ Create A New Branch With Git Switch] ( git/create-a-new-branch-with-git-switch.md )
308
309
- [ Delete All Untracked Files] ( git/delete-all-untracked-files.md )
309
310
- [ Determine The Hash Id For A Blob] ( git/determine-the-hash-id-for-a-blob.md )
Original file line number Diff line number Diff line change
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.
You can’t perform that action at this time.
0 commit comments