Skip to content

Commit a15e1ea

Browse files
committed
Add blog post for git wrapper script
1 parent db419a1 commit a15e1ea

1 file changed

Lines changed: 158 additions & 0 deletions

File tree

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
path: /blog/bash-script-before-reading-code
3+
title: 'The Bash Script I Use Before Reading Any Code'
4+
date: '2026-06-19'
5+
---
6+
7+
I saw this awesome blog post [The Git Commands I Run Before Reading Any Code](https://piechowski.io/post/git-commands-before-reading-code/) detailing some git commands to help you navigate a new code base. Well, I decided to make a wrapper bash script around those commands.
8+
9+
## The Script
10+
11+
Create a file `git-investigate` (no extension), mark it as executable (`chmod +x git-investigate`), and then add `git-investigate` to your `PATH`.
12+
13+
This automatically registers the git subcommand `git investigate COMMAND`.
14+
15+
```bash
16+
#!/usr/bin/env bash
17+
18+
# A git subcommand for investigating a new code base
19+
#
20+
# Credit to https://piechowski.io/post/git-commands-before-reading-code/ for the original git commands
21+
22+
set -euo pipefail
23+
24+
FIRST_ARG=${1:-""}
25+
26+
limit=${LIMIT:-20}
27+
since=${SINCE:-1 year ago}
28+
29+
echoerr() { printf "%s\n" "$*" >&2; }
30+
31+
if [ "$FIRST_ARG" = "churn" ]; then
32+
echoerr "A sorted list of most changed files in a given period of time"
33+
echoerr ""
34+
git log --format=format: --name-only --since="$since" | sort | uniq -c | sort -nr | head -$limit
35+
elif [ "$FIRST_ARG" = "contributors" ]; then
36+
git shortlog -sn --no-merges
37+
elif [ "$FIRST_ARG" = "bugs" ]; then
38+
echoerr "A sorted list of files with commits related to bugs or fixes"
39+
echoerr ""
40+
git log -i -E --grep="fix|bug|broken" --name-only --format='' | sort | uniq -c | sort -nr | head -$limit
41+
elif [ "$FIRST_ARG" = "velocity" ]; then
42+
echoerr "A sorted list of commit counts by month"
43+
echoerr ""
44+
git log --format='%ad' --date=format:'%Y-%m' | sort | uniq -c
45+
elif [ "$FIRST_ARG" = "firefighting" ]; then
46+
echoerr "A list commits relating to reverts, hotfixes, emergencies, or rollbacks"
47+
echoerr ""
48+
git log --oneline --since="$since" | grep -iE 'revert|hotfix|emergency|rollback'
49+
elif [ "$FIRST_ARG" = "credit-to" ]; then
50+
echoerr "Credit To:"
51+
echoerr ""
52+
echo "https://piechowski.io/post/git-commands-before-reading-code/"
53+
else
54+
echo "Usage: $0 {churn|contributors|bugs|velocity|firefighting|credit-to}"
55+
echo ""
56+
echo "Commands"
57+
echo ""
58+
echo "- churn: A sorted list of most changed files in a given period of time"
59+
echo "- contributors: A sorted list of the most active committers"
60+
echo "- bugs: A sorted list of files with commits related to bugs or fixes"
61+
echo "- velocity: A sorted list of commit counts by month"
62+
echo "- firefighting: A list commits relating to reverts, hotfixes, emergencies, or rollbacks"
63+
echo "- credit-to: Link to the original blog post detailing these git commands"
64+
echo ""
65+
echo "Environment Variables"
66+
echo ""
67+
echo "LIMIT -- Limits the number of results in 'churn' and 'bugs'"
68+
echo "SINCE -- Filters result by a natural time string e.g. 1 year ago in 'churn' and 'firefighting'"
69+
exit 1
70+
fi
71+
72+
```
73+
74+
## Usage
75+
76+
```bash
77+
cd myproject
78+
79+
git investigate
80+
81+
# Usage: git-investigate {churn|contributors|bugs|velocity|reverts-by-month|credit-to}
82+
83+
# Commands
84+
85+
# - churn: A sorted list of most changed files in a given period of time
86+
# - contributors: A sorted list of the most active committers
87+
# - bugs: A sorted list of files with commits related to bugs or fixes
88+
# - velocity: A sorted list of commit counts by month
89+
# - reverts-by-month: A list of revert commit count by month
90+
# - credit-to: Link to the original blog post detailing these git commands
91+
92+
# Environment Variables
93+
94+
# LIMIT -- Limits the number of results in 'churn' and 'bugs'
95+
# SINCE -- Filters result by a natural time string e.g. 1 year ago in 'churn' and 'reverts-by-month'
96+
97+
SINCE='10 years ago' LIMIT=10 git investigate churn
98+
99+
# A sorted list of most changed files in a given period of time
100+
101+
# 75
102+
# 17 src/pages/about.js
103+
# 14 src/layouts/index.js
104+
# 11 src/pages/talks.js
105+
# 10 src/pages/projects.js
106+
# 7 src/html.js
107+
# 7 package-lock.json
108+
# 5 yarn.lock
109+
# 4 src/pages/training.js
110+
# 4 src/components/nav-menu.js
111+
112+
git investigate contributors
113+
114+
# 70 Kylee Tilley
115+
# 2 dependabot[bot]
116+
117+
LIMIT=10 git investigate bugs
118+
119+
# A sorted list of files with commits related to bugs or fixes
120+
121+
# 7 src/pages/about.js
122+
# 4 src/layouts/index.js
123+
# 3 src/pages/talks.js
124+
# 1 src/pages/projects.js
125+
# 1 src/pages/index.js
126+
# 1 src/pages/blog.js
127+
# 1 src/components/nav-menu.js
128+
# 1 src/components/header.js
129+
# 1 package-lock.json
130+
# 1 gatsby-node.js
131+
132+
git investigate velocity
133+
134+
# A sorted list of commit counts by month
135+
136+
# 1 2018-07
137+
# 2 2019-08
138+
# 7 2019-11
139+
# 3 2020-02
140+
# 2 2020-03
141+
# 1 2020-05
142+
# 15 2022-12
143+
# 4 2023-05
144+
# 1 2024-07
145+
# 40 2025-02
146+
147+
SINCE='10 years ago' git investigate firefighting
148+
149+
# A list commits relating to reverts, hotfixes, emergencies, or rollbacks
150+
151+
# e60c355 Revert "Upgrade npm dependencies"
152+
153+
git investigate credit-to
154+
155+
# Credit To:
156+
157+
# https://piechowski.io/post/git-commands-before-reading-code/
158+
```

0 commit comments

Comments
 (0)