Skip to content
Git Basics: Find Commits That Add or Remove a Specific String

Git Basics: Find Commits That Add or Remove a Specific String

October 28, 2026

A setting may have existed under several values. Pickaxe searches changes in a string occurrence count, while a diff-regex search can find value edits that keep that count unchanged.

Prepare a scratch directory

Tested on Linux with Git 2.52.0 and GNU Bash 5.2.37. You need Bash, a text editor, the named runtime, and standard mktemp, rm, and rmdir utilities. The examples use only controlled local data; generated fixture directories are cleaned automatically.

Keep the same terminal open. Stop if setup fails, before saving the source:

start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1

Run the example

Save this as demonstrate-pickaxe.sh:

#!/usr/bin/env bash
set -euo pipefail
export LC_ALL=C
fixture_dir="$(mktemp -d)"
trap 'rm -rf -- "$fixture_dir"' EXIT
export GIT_CONFIG_NOSYSTEM=1
export GIT_CONFIG_GLOBAL=/dev/null
export GIT_AUTHOR_NAME='Example Author'
export GIT_AUTHOR_EMAIL='author@example.invalid'
export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"
export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
export GIT_AUTHOR_DATE='2026-10-28T00:00:00+00:00'
export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
git init -q -b main "$fixture_dir/repo"
cd -- "$fixture_dir/repo"
git config commit.gpgsign false
git config core.hooksPath /dev/null
printf 'mode=basic\n' > app.conf
git add app.conf
git commit -qm 'Start configuration'
printf 'mode=basic\nfeature=off\n' > app.conf
git commit -qam 'Add feature flag'
printf 'mode=basic\nfeature=on\n' > app.conf
git commit -qam 'Enable feature flag'
printf 'Occurrence-count changes:\n'
git log --format=%s -S 'feature=' -- app.conf
printf 'Matching changed lines:\n'
git log --format=%s -G '^feature=' -- app.conf
test "$(git log --format=%s -S 'feature=' -- app.conf)" = 'Add feature flag'

Run it:

bash demonstrate-pickaxe.sh

Observed output:

Occurrence-count changes:
Add feature flag
Matching changed lines:
Enable feature flag
Add feature flag

Read the result

Adding the flag changes the number of feature= occurrences from zero to one. Editing off to on keeps that count at one, so -S does not select it. -G matches added or removed diff lines and therefore selects both commits.

Apply it carefully

The search is restricted to app.conf and reachable history from HEAD. Renames, other branches, and merge history may need broader options. Neither command searches only commit messages; use --grep for message text. Add -p to inspect the matching patch. Run this script from an ordinary shell with no repository-selection variables such as GIT_DIR or GIT_WORK_TREE set. All commits and cleanup stay inside the newly created temporary fixture.

The official reference describes the behavior and options. These checks cover the fixture above rather than every input or platform.

Clean up

Remove the saved source, return to the starting directory, then remove the empty practice directory:

rm -- demonstrate-pickaxe.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dir

If the directory is not empty, inspect its remaining contents rather than deleting unknown files.

Acknowledgements

This post was created with assistance from AI.

Last updated on