skip to content

Search

Generating Git Links from the Terminal

3 min read

Quickly generate GitHub or GitLab commit and compare links from the terminal using a script.

I’ve been sharing GitHub and GitLab links a lot lately and thought it might be worthwhile to automate generating them.

My workflow usually involves tidying up commits in the terminal with lots of interactive rebasing, and then sharing them in comments, messages, or Jira tickets.

The Problem

Basically, I wanted to generate commit and compare URLs in the terminal. And then copy the output into a browser, chat, or ticket.

The Solution

I wrote a script called gurl (git URL) to generate commit and compare links from the terminal. You can find the implementation here: cowboy-bebug/dotfiles@8a3f803.

To install it, copy the script or symlink it into your ~/.local/bin:

# make sure ~/.local/bin exists and is in your PATH
mkdir -p ~/.local/bin
echo 'export PATH="$PATH:$HOME/.local/bin"' >> ~/.zshenv  # or .bashrc
 
# symlink the script to your bin directory
ln -s /path/to/gurl ~/.local/bin/gurl

Here’s the help output:

 gurl --help
Usage:
  gurl [<commit>|<range>] [--output <format>]
 
Options:
  -o, --output <format>    Output format: url (default), markdown|md, org|orgmode
  -h, --help               Show this help message
 
Examples:
  gurl https://github.com/org/repo/commit/abc1234
  gurl abc..def https://github.com/org/repo/compare/abc...def
  gurl -o markdown [abc1234](https://github.com/org/repo/commit/abc1234)
  gurl -o org [[https://github.com/org/repo/commit/abc1234][abc1234]]

And a few snippets as examples:

  • Latest commit link:

    gurl
  • Latest commit link in markdown (also works for org syntax):

    gurl -o md
  • you can also pass a commit hash as an argument or pipe into:

    gurl 8a3f803
     
    # Get the 4th most recent commit and generate a link
    git log --skip=3 -1 --format="%h" | gurl
  • the most useful feature, in my opinion, is to print a compare link:

    gurl abc1234..def5677
    # Output: https://github.com/org/repo/compare/abc1234...def5677
     
    echo $(git log --skip=33 -1 --format="%h")..$(git log --skip=30 -1 --format="%h") | gurl
    # Output: https://github.com/org/repo/compare/abc1234...def5677
     
    echo $(git log --skip=33 -1 --format="%h")..$(git log --skip=30 -1 --format="%h") | gurl -o md
    # Output: [repo/[email protected]](https://github.com/org/repo/compare/abc1234...def5677)

One thing to keep in mind about compare links is that the starting commit is exclusive (ending commit is inclusive). So if you want to print the last 3 commits, you need to compare from the 4th-most-recent commit to the latest one (since the starting commit is excluded). This is a limitation (or feature) of how GitHub and GitLab structure their compare link paths.

TL;DR

Use this script to generate commit and compare URLs directly from the terminal in plain, Markdown, or Org format.

# Latest commit URL
gurl
 
# Compare URL
gurl abc123..def456
 
# Markdown link
gurl -o md
 
# Org-mode link
gurl -o org
 
# Compare last 3 commits
git log --skip=3 -1 --format="%h" > from
git log --skip=0 -1 --format="%h" > to
gurl $(cat from)..$(cat to)

This has saved me a ton of time during code reviews and context sharing — feel free to adapt it to your own workflow.