Colin Brett Williams
Colin Brett Williams

Reputation: 51

How to change the remote branch that git status checks against?

My team moved servers for our Azure DevOps git repository.

How can I change the default remote that git status will compare against?

I have updated my remote origin to the correct server's url now, but when I run git status my local branch's tracked changes are compared against my old/original remote's main.

I am expecting that, since I have renamed my new remote url to "origin" that git status would check against that specific url and branch, however it does not.

The report I get from git status correctly refers to the remote/branch it is checking, however, it still defaults to the old branch:

git remote -v # check the current remote URLs
origin http://old-server/example/repo
new    http://new-server/example/repo

git status # check differences between my local branch and remote branch
On branch main
Your branch is ahead of origin/main by 4 commits.

git remote rename origin old # rename currently saved remote URLs
git remote rename new origin

git remote -v
origin http://new-server/example/repo
old    http://old-server/example/repo

git status
On branch main
Your branch is ahead of old/main by 4 commits.

I've been using the git remote manual up to this point but I haven not found anything to help with my specific question: https://www.git-scm.com/docs/git-remote

Upvotes: 0

Views: 119

Answers (2)

LeGEC
LeGEC

Reputation: 52081

The "upstream branch" of a branch is just one line in your local configuration file:
look at the content of your local .git/config file, and check all the [branch ...] sections:

...
[branch "master"]
        remote = origin
        merge = refs/heads/master
[branch "dev"]
        remote = old
        merge = refs/heads/dev
...

Calling git branch -u ... (short for git branch --set-upstream ...) just changes that value in your config file, there is no form of extra integrity/diff check.


The upstream branch serves as a default value for a number of commands.

# two most common uses:
$ git pull   # <- with no extra argument: will pull upstream branch
$ git push   # <- with no extra argument: will push to upstream branch

# also worth knowing:
$ git merge    # <- with no extra argument:
$ git rebase   # <-   run action with upstream branch

The thing specific to git status is that, to this day (git version 2.43), there is no direct command line flag to change the upstream used for comparison.

You will note, however, that the only line that depends on that upstream branch is the "ahead/behind" count. The changes part ("to be committed / not staged / untracked") are all made by comparing to the active branch.

For the sake of illustration, one way to get the "ahead/behind" count with any other branch is (source) :

# count on HEAD side is "ahead", count on branch side is "behind"
git rev-list --left-right --count HEAD...any/other/branch

Upvotes: 2

Colin Brett Williams
Colin Brett Williams

Reputation: 51

Answer found Make an existing Git branch track a remote branch?

Thanks to user Dan Moulding

For git 1.8+

git branch --set-upstream-to=origin/main

The "origin" in "origin/main" argument here is the remote that has been assigned the new, correct remote url.

My understanding:

  1. The local main that I am working on has come from the old remote url where I originally got it from cloning that repository.
  2. So, when I run git status it is not checking against a branch of the same name on any remote url that is labeled origin... instead it has been tracking an upstream from where it came from since it was pulled to my local machine.
  3. Therefore, by running the above command we are directly changing this branch's "origin" or upstream.

Concerns:
If this main branch has changed since server changes (from old url to new url) this could cause conflicts and I wonder if the above command will throw an error or if it performs a diff between the upstream changes. If anyone knows the answer to this I would be happy to know.

Upvotes: 3

Related Questions