Open Git Log Files from the Terminal
Here's a way to open files from a git log of file paths.
Why
Git log is a great starting point to open relevant files. These might be files related to a feature. Or they might be files that you've recently edited.
If you open the git log, it'll list paths to these files for you. Instead of copying and pasting them, you can manipulate the paths so that you can open and edit them directly from terminal.
Scenario: Recent Test Files
I'm in a TypeScript project with .spec.ts
as a test file name convention. Here's a one-liner to open all recent test files that you've committed:
git log --author="Jake Trent" --name-only | rg 'spec.ts' | sort -u | xargs ls -1 2>/dev/null | xargs nvim
Take it, Kronk; feel the power!
Let's break it down:
git log
- show commits in current branch--author="Jake Trent"
- filter log by author name--name-only
- show the file paths touched in the commitrg 'spec.ts'
- use ripgrep to filter to those paths that have the pattern 'spec.ts'sort -u
- sort alphabetically, but more importantly, make paths uniquexargs ls -1
- take all paths and make a one-line command that tries to list each of those paths, outputting their path again2>/dev/null
- removes all error ouput, showing only paths that existxargs nvim
- take all paths and make a one line command to open all the files innvim
editor
Other notes:
- Your pattern of
'spec.ts'
to identify tests or some other file could be totally different. - Instead of starting with
git log
, you might want to see a single commit withgit show --name-only
- Instead of opening all files at once, you might want to open one. To count from the from the front to the 3rd, for example, use
head -3 | tail -1
. To count from the back to the 4th to the last, for example, usetail -4 | head -1
.
Power to the pipers!