I have tried making some changes and pushed it up to GitHub. From another computer, I fetched the changes. When I do a git status
, it reports this:
On branch MyTestBranch nothing to commit, working directory clean
This is strange because I'm certain that there were changes fetched. I could even did a merge with those changes fetched!
In fact when I went over to my friend's computer and did a git status
, I could see this:
Your branch is behind 'origin/MyTestBranch' by 1 commit, and can be fast-forwarded.
But why weren't Git reporting something like my origin/MyTestBranch
is how many commits ahead or whether my branch is up-to-date with origin/MyTestBranch
?
My guess at this point (I'm still waiting for git branch -vv
or git rev-parse --symbolic-full-name "@{u}"
results) is that you do not have origin/MyTestBranch
set as the upstream for MyTestBranch
.
To set a branch as the upstream for the current branch, use:
git branch --set-upstream-to upstream
1
which in this case expands to:
git branch --set-upstream-to origin/MyTestBranch
To remove an upstream setting, use git branch --unset-upstream
.
The presence or absence of an upstream setting mainly affects whether git status
can tell you if you are ahead and/or behind, and whether git merge
and git rebase
can do their job with no additional parameters. So it's basically just a convenience setting.
Normally the upstream is set automatically when you first check out a branch by having git checkout
create it based on a remote-tracking branch. For instance, on your first git clone
, Git runs, at the end, the equivalent of:
git checkout master
even though you have no master
yet. Git then discovers that you have origin/master
(and no other remote/master
so that there is no question as to which remote to use), and does the equivalent of:
git checkout -b master --track origin/master
which creates local master
pointing to the same commit as remote-tracking branch origin/master
, and setting origin/master
as the upstream for master
, all in One Big Do-What-I-Mean Fell Swoop.
When you create a new local branch and have not yet pushed it upstream, there is no origin/whatever
remote-tracking branch for your local branch to track.2 In this case, you have to set the upstream manually, or use git push -u ...
: the -u
basically tells git push
to run git branch --set-upstream-to
for you (although it's actually all built in to the C code, at this point).
1If you're stuck with a truly ancient Git (pre-1.8.0) you must use git branch --set-upstream
, which is tricky to get right, or git config
, which is also tricky to get right. If at all possible, upgrade to a modern Git version.
2The set of words here—nouns like branch, nouns with adjectives like local branch and remote-tracking branch, and verbs like set-upstream-to and gerunds like tracking—is rather unfortunate. Git terminology, to put it in a nice short memorable Anglo-Saxon way instead of some polysyllabic neologistic phraseology, sucks rocks.3
3Or other Anglo-Saxon kick-ass word of choice.
Yes, it should, but before it can do that you need to do a git fetch
so that Git knows your current branch is behind the remote branch. Then when you do a git status
you should see that your branch is in fact behind.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With