brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git show commit:path prints an old version of a file without a checkout

Git
~/tips/tip-git-show-commit-path-old-file

git show v1.0:config/app.php

Prints a file as it was at any commit or tag, without touching your working tree.

Sometimes you only want to read how a file looked before, not restore it. git show with a colon does exactly that.

git show v1.0:config/app.php

The part before the colon is any commit: a hash, a tag, a branch name or something like HEAD~3. The part after it is the path from the root of the repository. Git prints the file content to your terminal and leaves your working tree and index alone.

What I like is that it’s just output, so you can send it anywhere. To compare an old version side by side in an editor:

git show main~5:src/settings.py > /tmp/settings-old.py

Or pipe it into a tool, like git show HEAD~1:package.json | jq .version to see what the version was before the last commit.

The path is relative to the top of the repository, not to the folder you’re in. From inside a subfolder, HEAD:file.txt looks for file.txt at the root. Write HEAD:./file.txt to mean the file next to you. And if the file didn’t exist yet at that commit, git tells you the path doesn’t exist in that revision, which is also a quick way to check when something was added.

Further reading: git-show documentation, gitrevisions documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link