Skip to content

Instantly share code, notes, and snippets.

@Skenvy
Created August 4, 2026 07:40
Show Gist options
  • Select an option

  • Save Skenvy/545fe6552980f9be7f2666b4da30ffeb to your computer and use it in GitHub Desktop.

Select an option

Save Skenvy/545fe6552980f9be7f2666b4da30ffeb to your computer and use it in GitHub Desktop.
~/.gitconfig forensics

I use a highly customised process for regenerating my ~/.gitconfig. https://github.com/Skenvy/dotfiles/blob/home/git/config/README.md --

Every bashrc run re-runs a "git config apply" script. https://github.com/Skenvy/dotfiles/blob/home/git/config/apply.sh --

Despite my intended target config not really changing, wsl will usually leave my file untouched -- this script on 95% of startups will report no changes found. But every so often, if wsl is not started for a few days, it reports a change, which is typically some [user] lines missing.

It's an odd issue.

This script is to help debug it. It just dumps the current state of the ~/.gitconfig in to some cache logs. It's only useful if you're also routinely scripting the state of your ~/.gitconfig. Uselss otherwise.

I add this script in my ~/.includes/.pre/gitconfig-forensics.sh and source-existing-file ~/.include/.pre/gitconfig-forensics.sh from my ~/.includes/.pre/.bashrc that gets hooked at the start, so it logs the state before changing it with the apply script at the end of the bashrc.

# ============================================================================
# ~/.gitconfig Forensics
#
# Runs BEFORE anything else in .bashrc so we can inspect ~/.gitconfig before
# any repair scripts touch it.
# ============================================================================
FORENSICS_DIR="$HOME/.cache/gitconfig-forensics"
mkdir -p "$FORENSICS_DIR"
TIMESTAMP="$(date +%Y-%m-%dT%H:%M:%S%z)"
SAFE_TIMESTAMP="$(date +%Y%m%d-%H%M%S)"
LOG="$FORENSICS_DIR/$SAFE_TIMESTAMP.log"
{
echo "=================================================================="
echo "Timestamp: $TIMESTAMP"
echo "Hostname: $(hostname)"
echo "User: $USER"
echo "HOME: $HOME"
echo "PWD: $PWD"
echo
echo "==================== Environment ===================="
echo "WSL_DISTRO_NAME=${WSL_DISTRO_NAME:-<unset>}"
echo "WSL_INTEROP=${WSL_INTEROP:-<unset>}"
echo "SHELL=$SHELL"
echo
echo "==================== Kernel ===================="
uname -a
echo
echo "Boot ID:"
cat /proc/sys/kernel/random/boot_id
echo
echo "Uptime:"
uptime
echo
echo "==================== Git ===================="
command -v git
git --version
echo
echo "==================== ~/.gitconfig metadata ===================="
ls -li ~/.gitconfig
echo
stat ~/.gitconfig
echo
printf "inode mtime ctime size\n"
stat -c "%i %Y %Z %s" ~/.gitconfig
echo
sha256sum ~/.gitconfig
echo
echo "==================== File contents ===================="
cat ~/.gitconfig
echo
echo "==================== Git resolved config ===================="
git config --global --show-origin --list
echo
echo "==================== User section ===================="
git config --global --show-origin --get-regexp '^user\.' || true
echo
echo "==================== GPG section ===================="
git config --global --show-origin --get-regexp '^gpg' || true
echo
echo "==================== Commit section ===================="
git config --global --show-origin --get-regexp '^commit' || true
echo
echo "==================== Include section ===================="
git config --global --show-origin --get-regexp '^include' || true
echo
echo "==================== Mounts ===================="
mount
echo
echo "==================== Proc root ===================="
readlink /proc/self/root
echo
} > "$LOG" 2>&1
# Keep a copy of exactly what existed before any repair script.
cp -a ~/.gitconfig "$FORENSICS_DIR/$SAFE_TIMESTAMP.gitconfig"
# Append a compact history for easy inspection.
printf "%s | inode=%s mtime=%s ctime=%s size=%s sha=%s\n" \
"$TIMESTAMP" \
"$(stat -c '%i' ~/.gitconfig)" \
"$(stat -c '%Y' ~/.gitconfig)" \
"$(stat -c '%Z' ~/.gitconfig)" \
"$(stat -c '%s' ~/.gitconfig)" \
"$(sha256sum ~/.gitconfig | cut -d' ' -f1)" \
>> "$FORENSICS_DIR/history.log"
# Keep only the most recent 200 logs and snapshots.
(
cd "$FORENSICS_DIR" || exit
ls -1tr *.log 2>/dev/null | head -n -200 | xargs -r rm -f
ls -1tr *.gitconfig 2>/dev/null | head -n -200 | xargs -r rm -f
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment