mirror of
https://github.com/jbranchaud/til
synced 2026-07-02 23:58:25 +00:00
Add Programmatically Grab SHA For Head Commit as a Git TIL
This commit is contained in:
@@ -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).
|
||||
|
||||
_1787 TILs and counting..._
|
||||
_1788 TILs and counting..._
|
||||
|
||||
See some of the other learning resources I work on:
|
||||
|
||||
@@ -406,6 +406,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Move The Latest Commit To A New Branch](git/move-the-latest-commit-to-a-new-branch.md)
|
||||
- [Override The Global Git Ignore File](git/override-the-global-git-ignore-file.md)
|
||||
- [Pick Specific Changes To Stash](git/pick-specific-changes-to-stash.md)
|
||||
- [Programmatically Grab SHA For Head Commit](git/programmatically-grab-sha-for-head-commit.md)
|
||||
- [Pulling In Changes During An Interactive Rebase](git/pulling-in-changes-during-an-interactive-rebase.md)
|
||||
- [Push To A Branch On Another Remote](git/push-to-a-branch-on-another-remote.md)
|
||||
- [Quicker Commit Fixes With The Fixup Flag](git/quicker-commit-fixes-with-the-fixup-flag.md)
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
# Programmatically Grab SHA For Head Commit
|
||||
|
||||
When I use `gh browse path/to/some-file.txt`, it opens the browser to that file
|
||||
in GitHub. However, it targets the default branch (`main`) by default which is
|
||||
not very useful as a permalink because what that file looks like on `main` is
|
||||
liable to change.
|
||||
|
||||
There is a `--commit` flag you can use to have it instead open to that file at a
|
||||
specific commit SHA.
|
||||
|
||||
So what SHA do I pass as an argument to that flag?
|
||||
|
||||
Often what I would like to grab is a reference to the current version of the
|
||||
file which is whatever it looks like for the `HEAD` commit. But `HEAD` is
|
||||
another moving target reference. The `git rev-parse` command can translate
|
||||
`HEAD` into a specific SHA though.
|
||||
|
||||
```bash
|
||||
❯ git rev-parse --short HEAD
|
||||
3402428
|
||||
|
||||
❯ git rev-parse HEAD
|
||||
3402428aadc02cfdc9825c8feb593443e72f50cd
|
||||
```
|
||||
|
||||
Either of those will work. I can use a bash command substitution then to tie it
|
||||
all together into a single command:
|
||||
|
||||
```bash
|
||||
❯ gh browse path/to/some-file.txt --commit=$(git rev-parse --short HEAD)
|
||||
```
|
||||
|
||||
See `man git-rev-parse` for more details.
|
||||
Reference in New Issue
Block a user