★ Using keep-sorted to keep your ~/.gitconfig tidy
• 365 words • 2 min • updated
⚠️ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.
I tend to sort my
~/.gitconfig
headings to keep the config tidy.
An example:
ini
[...]
# https://git-scm.com/docs/git-rerere
[rerere]
autoUpdate = true
enabled = true
[status]
# Show individual files in untracked directories.
showUntrackedFiles = all
short = true
branch = true
[submodule]
# Clone new submodules in parallel with as many jobs.
fetchJobs = 0
[...]This was done manually. Until…today.
It has just occurred to me1 I could use keep-sorted with the headings.
Previously:
But…how?!
The usage to sort, say, aliases, is quite straightforward2:
[alias]
# keep-sorted begin
bd = !branch="$(git branch --show-current)" && git default && git branch -D "${branch:-$1}"
blank = desc -m \"blank commit\"
cm = commit
co = checkout
cp = cherry-pick
dc = diff --cached
default = !git checkout main &>/dev/null || git checkout master
desc = commit --allow-empty -n
emerge = !git add -A . && git amend -n && git pushm --force-with-lease
nb = switch --create
[...]
# keep-sorted finishHow can you apply the same technique to headings?
The out-of-the-box utilization is OK-ish, but not great:
From:
ini
# keep-sorted begin
[fetch]
prune = true
[help]
autocorrect = 10
[format]
# Automatically sign-off patches when using format-patch.
signoff = true
# keep-sorted finishTo:
ini
# keep-sorted start
[fetch]
prune = true
[format]
# Automatically sign-off patches when using format-patch.
signoff = true
[help]
autocorrect = 10
# keep-sorted end(including the blank line at the top)
It turns out all we have to do is a little bit of tweaking:
ini
# keep-sorted begin block=yes newline_separated=yes
[fetch]
prune = true
[help]
autocorrect = 10
[format]
# Automatically sign-off patches when using format-patch.
signoff = true
# keep-sorted finish…which then becomes:
ini
# keep-sorted begin block=yes newline_separated=yes
[fetch]
prune = true
[format]
# Automatically sign-off patches when using format-patch.
signoff = true
[help]
autocorrect = 10
# keep-sorted finishHow nice!
Backlinks
- go/keep-sorted (Sep 22, 2025)