From: Phillip Wood Date: Tue, 14 Nov 2023 12:28:14 GMT Subject: Re: Feature request: git status --branch-only Message-ID: <47fa8400-1e5f-437f-84b8-50bb09580325@gmail.com> In-Reply-To: Hi Ondra On 14/11/2023 10:16, Ondra Medek wrote: > Hello, > I am working on a tol which should fetch changes from a remote > repository on a user click. I want to limit fetch on the current > remote tracking branch (something like "origin/master"), but > surprisingly, it's hard to get it for all corner cases like a fresh > clone of an empty repository or detached head, etc. E.g see this SO > thread https://stackoverflow.com/questions/171550/find-out-which-remote-branch-a-local-branch-is-tracking/52896538 I think you can do this by calling git symbolic-ref --quiet HEAD to get the full refname of the current branch. If HEAD is detached it will print nothing and exit with exit code 1. Then you can call git for-each-ref --format="%(upstream:short)" $refname to get the upstream branch Best Wishes Phillip