From 0c00e47141e0cf2577301ec1e5aa86fc1bd276e9 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sun, 3 May 2026 19:18:14 -0500 Subject: [PATCH] Add Open File To Specific Line In Browser as a GitHub TIL --- README.md | 3 +- .../open-file-to-specific-line-in-browser.md | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 github/open-file-to-specific-line-in-browser.md diff --git a/README.md b/README.md index ca8bb35..59be89f 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1785 TILs and counting..._ +_1786 TILs and counting..._ See some of the other learning resources I work on: @@ -466,6 +466,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Access Your GitHub Profile Photo](github/access-your-github-profile-photo.md) - [List PRs Awaiting Your Review](github/list-prs-awaiting-your-review.md) - [Open A PR To An Unforked Repo](github/open-a-pr-to-an-unforked-repo.md) +- [Open File To Specific Line In Browser](github/open-file-to-specific-line-in-browser.md) - [Target Another Repo When Creating A PR](github/target-another-repo-when-creating-a-pr.md) - [Tell gh What The Default Repo Is](github/tell-gh-what-the-default-repo-is.md) diff --git a/github/open-file-to-specific-line-in-browser.md b/github/open-file-to-specific-line-in-browser.md new file mode 100644 index 0000000..82264dd --- /dev/null +++ b/github/open-file-to-specific-line-in-browser.md @@ -0,0 +1,46 @@ +# Open File To Specific Line In Browser + +Often one of the best ways to point a teammate to a line of code is to share a +GitHub link to a specific file and line number. Sometimes even a specific +commit. + +For the longest time I would manually open GitHub, navigate to that file, and so +forth. The `gh` CLI supports this with the `browse` subcommand and it takes way +less time if you already have the repo in your local filesystem. + +For instance, if I want to point you to line 11 of the `zshrc.local` file in my +`dotfiles` repo, I can run the following command: + +```bash +$ gh browse zshrc.local:11 +``` + +That would open a browser tab to +[https://github.com/jbranchaud/dotfiles/blob/main/zshrc.local?plain=1#L11](https://github.com/jbranchaud/dotfiles/blob/main/zshrc.local?plain=1#L11). + +If I wanted a range of lines, I could change it from `11` to, say, `11-27`: + +```bash +$ gh browse zshrc.local:11-27 +``` + +And I would see this in the browser -- +[https://github.com/jbranchaud/dotfiles/blob/main/zshrc.local?plain=1#L11-L27](https://github.com/jbranchaud/dotfiles/blob/main/zshrc.local?plain=1#L11-L27). + +Both of these URLs are pointing to the `main` branch. If I instead want to +reference a specific commit, I can use the `--commit` flag. + +```bash +$ gh browse zshrc.local:11-27 --commit=f2f9e78d4fc784643f725c88f7a5a7a077e7f261 +``` + +I grabbed that from the latest commit in `git log`. That opens to +[https://github.com/jbranchaud/dotfiles/blob/f2f9e78d4fc784643f725c88f7a5a7a077e7f261/zshrc.local?plain=1#L11-L27](https://github.com/jbranchaud/dotfiles/blob/f2f9e78d4fc784643f725c88f7a5a7a077e7f261/zshrc.local?plain=1#L11-L27). + +Another way of doing that would be to use `git rev-parse HEAD`: + +```bash +$ gh browse zshrc.local:11-27 --commit=$(git rev-parse HEAD) +``` + +See `gh browse --help` for more details.