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, 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)
│ ○ 51eef8f5 origin/feature/login
│ ~ (2)
│ │ ○ 2cf4c0c2 origin/release/1.2
├─┼─╯
○ │ fc1afa3b merge-base
~ │ (1)
├─╯
○ 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] [-g] [--no-status] [--color {auto,always,never}] [--no-color]
N |
how many branches to show (default 5) |
-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 |
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
mainlast week andrelease/1.0happens to be two commits behindmain,release/1.0is nearer. Look at the graph when it matters. - Branches that are not in
refs/remotesare ignored. Rungit fetchfirst. - Folded counts on a node with two parents are the commits unique to that node, shown once.
Similar tools
- Scripts based on
git show-branchor on greppinggit log --decoratefororigin/(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-nearestinfers from history and needs no setup.
Development
uv run pytest
License
MIT
Metadata
Release files for git-nearest 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| git_nearest-0.1.0.tar.gz | 10.6 kB | Without provenance | Details for git_nearest-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| git_nearest-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for git_nearest-0.1.0-py3-none-any.whl |
Total release size: 21.8 kB
Release files / git_nearest-0.1.0.tar.gz
| Download URL | git_nearest-0.1.0.tar.gz |
|---|---|
| Size | 10.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7db9c7241828fcc182c0e5043c5a252b2df807b8a96c6fe94c533acd6bd2ed00
|
|
BLAKE2b-256 checksum How to use checksums |
79074b1acec6b4edf3119ed42d49162062b8a661566e09b1ceb8f2ed610a7dff
|
| 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.1.0-py3-none-any.whl
| Download URL | git_nearest-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5aa77bb5dcc57875918054e5248e826a781619a2d545327fb997ca418bdc2731
|
|
BLAKE2b-256 checksum How to use checksums |
c2090d323da1afdc13b8d077489c0a1153f92043b5e06a902f998ef844b1dc21
|
| 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.1.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.