Skip to main content

git-nearest

Which remote branch is my current branch closest to?

$ git nearest
@  HEAD -> fix-typo  [1 staged, 1 untracked]
↑2 ↓0  origin/main           574e9395
↑8 ↓0  origin/feature/login  51eef8f5
↑9 ↓1  origin/release/1.2    fc1afa3b

The first line shows where HEAD is and the state of the working tree: [clean], or counts such as [2 staged, 3 modified, 1 untracked] (conflicted files turn it red). That is the thing to check before moving the branch somewhere else.

↑ is how many commits HEAD has that the remote branch doesn't, ↓ is how many the remote branch has that HEAD doesn't, and the last column is the merge-base. The closest branch (fewest ↑, then fewest ↓) comes first. Remote branches with the same tip all show up, so you can see ties instead of having one picked for you.

Graph

git nearest -g draws how HEAD and those branches relate, in the style of jj log: newest on top (by commit date, parents never above their children), and runs of ordinary commits folded into a ~ (n) row on the vertical line.

$ git nearest -g
@  ae22273d  HEAD -> fix-typo  [1 staged, 1 untracked]
~  (1)
○  574e9395  origin/main
├─╮
~ │  (3)
│ │ ○  2cf4c0c2  origin/release/1.2
├─┼─╯
○ │  fc1afa3b  merge-base
~ │  (1)
│ ○  51eef8f5  origin/feature/login
│ ~  (2)
├─╯
○  5a42fea0  merge-base
~ (n) = n commits folded between

Only HEAD, the branch tips and the merge-bases between them are drawn. (3) means three commits sit between the nodes above and below it.

Both examples are real output from a synthetic repository built by scripts/demo.py.

Install

Needs Python 3.8+ and git. No other dependencies.

uv tool install git-nearest      # or: pipx install git-nearest

The package installs an executable called git-nearest; git runs any git-<name> on your PATH as git <name>.

Usage

git nearest [N] [-b REV] [-g] [--no-status] [--color {auto,always,never}] [--no-color] [--version]
N how many branches to show (default 5)
-b, --branch REV measure from this branch or commit instead of HEAD; skips the working tree check, which only describes HEAD
-g, --graph draw the graph instead of the table
--no-status skip the working tree check (it runs git status, which can be slow on huge repositories)
--color auto (default) colors only on a terminal and respects NO_COLOR
--version print the version and exit

How it works

git has no notion of a branch's "parent", only of common ancestors, so this is a heuristic, not a lookup. For every ref under refs/remotes it asks git for the ahead/behind counts against HEAD in a single for-each-ref --format='%(ahead-behind:HEAD)' call (git 2.41+; older versions fall back to one rev-list per ref, which is slower on repositories with many branches).

The graph walks the history above the common base once, marks which tips can reach each commit, and keeps only the commits where those sets change. Those are the merge-bases.

Limitations

  • "Nearest" is by commit count. If you branched from main last week and release/1.0 happens to be two commits behind main, release/1.0 is nearer. Look at the graph when it matters.
  • Branches that are not in refs/remotes are ignored. Run git fetch first.
  • Folded counts on a node with two parents are the commits unique to that node, shown once.

Similar tools

  • Scripts based on git show-branch or on grepping git log --decorate for origin/ (the usual Stack Overflow answers) look only at the first decorated ancestor, so they miss ties and give no distance.
  • Git Town records a parent for each branch when it creates the branch. That is exact but only works for branches it knows about; git-nearest infers from history and needs no setup.

Development

uv run pytest

License

MIT

Metadata

Release files for git-nearest 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for git-nearest 0.2.0
File Size Uploaded Provenance Actions
git_nearest-0.2.0.tar.gz 11.5 kB Without provenance Details for git_nearest-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for git-nearest 0.2.0
File Interpreter ABI Platform Actions
git_nearest-0.2.0-py3-none-any.whl Python 3 none any Details for git_nearest-0.2.0-py3-none-any.whl

Total release size: 23.3 kB

Release files / git_nearest-0.2.0.tar.gz

Download URL git_nearest-0.2.0.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4d73fcae91151d37b5da04352715e3a901a4df35c374a04df43ebf762312eeed
BLAKE2b-256 checksum
How to use checksums
5697d1ca51835b2d941a87c8fb8f55b3c5a9dea75d05f89fe8a99ee76dfc6ae5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / git_nearest-0.2.0-py3-none-any.whl

Download URL git_nearest-0.2.0-py3-none-any.whl
Size 11.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
56e9d7d71457e08d6b1b22fda60b03581f0c92c38341d0d74b14788c3934052c
BLAKE2b-256 checksum
How to use checksums
6c75e1cc4e655854c7bb47aaa236b233777e248e5998362f143b6b2879896bae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for git-nearest 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page