0

For most (all?) my life using git I could do git status and it would tell me if there was something to commit in my local repository and if it was up to date with origin.

However, I recently created a repository on github for personal use that I cloned into my local machine in which for some reason that doesn't work.

In that repo, if I issue git status I get the following output

$ git status
# On branch master
nothing to commit, working directory clean

even though I know there are changes to origin. I have to issue git remote update and then issue git status to show that my branch is behind origin.

The thing is that that doesn't happen for my other repositories (even using the same computer, etc), which makes me think that is a local configuration. Something that has the same effect of aliasing the git status that somehow aliases git status to git remote update && git status locally.

So can someone explain what's going on and how I can "fix" this repo to have the same behavior a the others?

Related: git status not showing local repo behind remote

TomCho
  • 3,204
  • 6
  • 32
  • 83
  • 1
    See [Bernardo Duarte's answer](https://stackoverflow.com/a/66159755/1256452) and note that `git remote update` means the same thing (more or less) as `git fetch --all`, i.e., run `git fetch` against all remotes. A simple `git fetch` will fetch from the (single) correct remote for the current branch and is therefore sometimes faster, though if you have only one remote—as most people do—it winds up doing the same thing. My only guess as to why you haven't had to do this with other repositories is that they were already sync-ed (fetched earlier). – torek Feb 11 '21 at 21:48

3 Answers3

3

I had your issue, and resolved with :

git push origin <branch> -u

It push the commits and setup track of the remote on the same time.

A git status will now show also if you are behind / ahead or up to date. :)

Cristian F.
  • 328
  • 2
  • 12
1

Quoting torek's comment:

Git does not contact the remote unless/until you specifically ask it to, with git ls-remote (a read-only operation that updates nothing in your copy), git fetch (update your copy of their stuff without merging), or git pull (update your copy and then run git merge to add their changes to your current branch, after which you're no longer behind).

To find if your branch s behind or ahead of your remote, you can just simply request git fetch at any time to sync up your local repository with your remote.

git status is not supposed to check on your remote, as per the docs, this is what it is supposed to do:

Displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the working tree and the index file, and paths in the working tree that are not tracked by Git.

Bernardo Duarte
  • 4,074
  • 4
  • 19
  • 34
0

You get the ahead ... behind ... message if your local branch is set up to track a remote branch.

To check if your local master branch tracks any branch, you can

  • open your .git/config file in an editor, and see if you have a [branch "master"] section,
  • run git config --get branch.master.merge in your terminal and see if it outputs anything

To set your local branch to track a remote branch, simply run :

git branch -u origin/master

To see what remote branches you currently know of, run :

git branch -r

Github changed the name of the default branch on its repo from master to main.

Depending on how you linked your local repo and github's remote, you may simply have a local master branch, but no branch named master on the remote side.

LeGEC
  • 46,477
  • 5
  • 57
  • 104