The git status
command does many things—probably the most important is that it runs two git diff
s, to figure out how your HEAD
commit, your index, and your work-tree relate to each other—but the first few things it does are:
- Print the name of the current branch, if you're on a branch.
- Print counts for ahead and/or behind, if you're on a branch and that branch has an upstream set and the counts are nonzero.
You can disable the ahead/behind counts using --no-ahead-behind
, or enable them with --ahead-behind
. (The default is enabled.)
To compute the counts, Git uses git rev-list --count
. For specifics on how this works, see git branch ahead and behind for local branch? Note that the counting is based on the branch's upstream setting, so if you are on branch B
and its upstream is origin/B
, this is the result of counting commits that are reachable from your branch B
but not from your remote-tracking name origin/B
, and vice versa. If origin/B
is not up to date with B
on origin
, this count is not as useful as you might want; run git fetch origin
to update it as needed.
Each branch has its own separate upstream setting. Any one branch name can have one upstream, or no upstream. The upstream is pretty commonly a remote-tracking name (origin/whatever
) but it can be any name, including other ordinary local branch names. The git branch --set-upstream-to
command, or git branch --unset-upstream
, is the main modern way to manipulate the upstream setting of any existing branch name. (The actual setting is complicated and can be modified through git config
or by editing .git/config
.)
As for the detached HEAD mentioned in comments, a detached HEAD simply means that you are not on a branch. This mode is normal when you have asked for it, either via git checkout --detach
or by using git checkout
on something that is not a branch name. It's also normal, as you mention, when you are in the middle of a rebase operation that has yet to complete. In these cases, modern git status
produces an informative message telling you that you are in this detached HEAD mode, and (to some extent) why.
Since a detached HEAD has no upstream—only branch names have upstream settings—there is never any ahead/behind count for this mode.