2026-02-02 git-link

I have recently discovered a great little package called git-link. It is a wonderful addition to Emacs’ Git-related capabilities. Its purpose is to create links to various Git repositories (it supports GitHub, BitBucket, GitLab and several others) based on where the point is.

The main entry points are two functions, git-link and git-link-commit. The former creates a link to the file in the current buffer. By default, the link points to the current line; with a prefix argument of - (a single minus), the link contains no line number. This function is really smart – it works in git-timemachine and Magit buffers. (One issue I found is that when a file name was changed and git-timemachine shows a version from before the change, the link contains the “new” name, pointing to a “resource not found” page on BitBucket. Given how file renames are a major pain in Git, this is neither unexpected nor very disappointing.)

The other function, git-link-commit, creates a link to the commit hash at point, and that’s pretty much it. (Well, there is more – for example, git-link-homepage creates a link to the repository’s homepage, which is nice, but probably less useful. There are also a bunch of user options, the most useful being probably git-link-open-in-browser, which opens the link in a browser in addition to showing it in the echo area and putting it on top of the kill ring. Also, the three functions support some more prefix arguments – see their docstrings for details.)

One of the things I found lacking is a git-link-dwim function which would call git-link-commit if the point is on a commit hash and git-link otherwise. Also, recognizing branch names in addition to commit hashes would be pretty nice.

The former is easy to fix:

(defun git-link-dwim ()
  "Call `git-link-commit' if on a hash or `git-link' otherwise."
  (interactive)
  (let ((word (word-at-point t)))
    (call-interactively
     (if (string-match-p "^[a-fA-F0-9]\\{7,40\\}$" (or word ""))
         #'git-link-commit
       #'git-link))))

The latter would require more work. It could use git to list the branches in the repository, then construct a regex matching all of them (Elisp has the wonderful regexp-opt function which does exactly that!) and use looking-at to determine if the point is on a branch name. This would require the point to be on the beginning of the branch name, though. An alternative would be to use (thing-at-point 'filename t) – syntactically, branch names are just filenames (or at least something very similar), so it could work, too. Either way, this is a slightly larger project I am currently not that interested in, so I’ll stay with my git-link-dwim command.

Anyway, big thanks to sshaw for writing this extremely useful package!

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryGit