Question

How does git know when a branch's upstream is gone?

Say I have a branch on git, my-branch. I push it to a remote, then delete it from said remote, and leave my local copy remaining.

git checkout -b my-branch
git push origin my-branch 
git push origin --delete my-branch

Git then seemingly knows nothing of an "origin/my-branch", but gives indications that it knows that there was once an origin/my-branch, such as in .git/COMMIT_EDITMSG:

# Your branch is based on 'origin/my-branch', but the upstream is gone.

My question is, how is git deducing that there once was an origin/my-branch that is now gone? Where is this information stored/computed from?

 5  40  5
1 Jan 1970

Solution

 7

The mapping between your local branch and the remote branch is stored in .git/config like

[branch "my-branch"]
    remote = origin
    merge = refs/heads/my-branch

There is directory which tracks local branches

.git/refs/heads/{branch-name}

Lastly there is directory that tracks branches per remote

.git/refs/remotes/origin/{branch-name}

Therefore if you have a branch present in config but that branch is missing from the corresponding remote, then Git knows it has been deleted upstream but used to have a mapping.

2024-06-26
Cory Kramer