Search code examples
gitrepository

No origin mentioned in git status, but pushing and fetching work


I have local git repository and one remote repository on gitlab.com. As opposite to another similar situation, the command git status print out nothing about origin.

The output of git status -uno (there are many untracked files, so I skip them)

On branch master
nothing to commit (use -u to show untracked files)

where us elsewhere it is

On branch master
Your branch is up to date with 'origin/master'.

Do you know how to get the expected behavior (the second one)?

I suspect it can be the result of set the origin twice. During the initialization, I did

$ git push --set-upstream https://gitlab.com/<username>/projectA.git
$ git push --set-upstream https://gitlab.com/<username>/projectB.git
$ git remote add origin https://gitlab.com/<username>/projectB.git

I removed origin and set it again:

$ git remote rm origin
$ git push --set-upstream https://gitlab.com/<username>/projectB.git
$ git remote add origin https://gitlab.com/<username>/projectB.git

I tested how origin looks like via git remote -v. It shows me nothing after removing the origin, in every other moment the result was (and is now):

origin  https://gitlab.com/<username>/projectB.git (fetch)
origin  https://gitlab.com/<username>/projectB.git (push)

git fetch behaves in other way I know. It returns

$ git fetch -v
POST git-upload-pack (102 bytes)
From https://gitlab.com/<username>/projectB
 * branch            master     -> FETCH_HEAD

For standard situation I get

$ git fetch -v
POST git-upload-pack (155 bytes)
From https://gitlab.com/<username>/projectC
 = [up to date]      main       -> origin/main

For completness, I did git gc and git prune in some moment. This is something what I did first time, so I have no experience if it has some side-effects related to this problem.


Solution

  • To "fix" your issue:

    once you have defined a named remote (in your question: origin), and fetched its branches, you can :

    git branch -u origin/main
    

    You need to use named remotes in order to keep a remote tracking branch. When you use explicit urls, git will not store the branches it fetches locally (or the branch you update by running git push), so you find yourself with nothing to point to, even when specifying --set-upstream.

    If you routinely need to push or pull from two different remotes, give them different names :

    # for example :
    git remote add origin https://gitlab.com/<username>/projectB.git
    git remote add upstream https://gitlab.com/<username>/projectA.git