Checking Which Branch a Local Branch Tracks
A pull or push is easier to reason about once you know which upstream branch your local branch tracks. I check that whenever I come back to a repository with several similarly named branches. The local branch name alone is no guarantee that its upstream has the same name.
Start with git branch -vv. The current branch is marked with an asterisk, and a tracking branch usually appears in square brackets. For the current branch alone, git rev-parse --abbrev-ref --symbolic-full-name '@{upstream}' prints its configured name. The quotes keep the expression intact across shells. If no upstream is configured, the second command reports an error.
One detail is easy to miss: ahead and behind are relative to your local copy of the tracking branch. If you need current information from the remote, run git fetch for the appropriate remote and then check again. Fetch updates the remote-tracking references without merging into the branch you have checked out.
I use the check in vsux, the open-source macOS workspace I built, when moving between projects with different remote setups. I can see the project files and Git status, then run the exact check from the same project terminal before deciding whether to pull.
Comments
Join the discussionNo comments yet. Be the first to comment.