24

If I am on a local branch that is not tracking any remote branch and I give the command

git fetch

Given I have several remotes defined in $GIT_DIR/config, from which remote is the fetch done?

I tried to find out from the man page, but this point is unclear to me.

Additionally: How can I change this default remote without making the local branch tracking?

Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158

4 Answers4

19

If you have multiple remote repositories, and don't specify any remote repository name, origin will be used by default. If there is no remote repository named origin, then it will error out saying

fatal: No remote repository specified.  Please, specify either a URL or a
remote name from which new revisions should be fetched.

Additionally: How can I change this default remote without making the local branch tracking?

You can rename that repository name to 'origin' to make it default.

Careful: this won't work if the current branch already has an upstream specified on a different remote.
From git help fetch:

When no remote is specified, by default the origin remote will be used, unless there’s an upstream branch configured for the current branch.

In this case, you can change the upstream branches to use origin by editing the remote fields for branches configured in .git/config.

kyb
  • 7,233
  • 5
  • 52
  • 105
0xc0de
  • 8,028
  • 5
  • 49
  • 75
  • 2
    Is there no alternative to changing the name of the remote to origin? – Klas Mellbourn May 30 '13 at 15:05
  • 7
    It's a little inflexible to have git assume "origin" to be the default remote. I always rename my remotes to be more meaningful like "github", "internal" etc. And yes, when "origin" remote does not exist, git will scream "fatal: No remote repository specified." – Devy Mar 25 '15 at 18:23
  • 1
    @KlasMellbourn: you can set the remote for your tracking branches to use a different remote than `origin`, and then that remote will be used by default when you run `git fetch` with one of those branches checked out. – ntc2 Jan 28 '17 at 03:09
  • **Caution:** I have had `git fetch` fail **silently** in certain Git versions when the only remote was with a name other than `origin`. There was no fatal error or any message at all. It just exited immediately to the prompt. Once I renamed the remote to `origin`, the `fetch` command actually fetched from the remote. – ADTC Nov 30 '22 at 12:20
4

In your project folder, when you initialize git in the first step, .git folder is created.

Look into this folder for a file named config, it contains all the branch info. origin is used as the default.

  [remote "origin"]
      fetch = +refs/heads/*:refs/remotes/origin/*
      url = git@github.com:project.git

So the code is fetched from the url listed here.

Bijendra
  • 9,467
  • 8
  • 39
  • 66
  • No need to do this. You can just run `git remote -v` (to see all) or `git remote get-url origin` (to check for default). – ADTC Nov 30 '22 at 13:05
2

It will fetch of the origin remote. This is the frist remote you performed the GIT clone command on.

  • 3
    You may never have performed a `git clone` on the repo, but added remotes (called 'origin' or otherwise) after the fact. – mwfearnley Sep 01 '16 at 09:58
0

Here are some aliases which will give strings which can be used programatically:

branch-name = "symbolic-ref --short HEAD"  # https://stackoverflow.com/a/19585361/5353461
branch-remote-fetch = !"branch=$(git branch-name \"$1\") && git config branch.\"$branch\".remote || echo origin #"
branch-remote-push  = !"branch=$(git branch-name \"$1\") && git config branch.\"$branch\".pushRemote || git config remote.pushDefault || git branch-remote-fetch #"
branch-url-fetch = !"remote=$(git branch-remote-fetch \"$1\") && git remote get-url        \"$remote\" #"  # cognizant of insteadOf
branch-url-push  = !"remote=$(git branch-remote-push  \"$1\") && git remote get-url --push \"$remote\" #"  # cognizant of pushInsteadOf

If you want to find the remote for another branch, then replace branch-name above with the following with allow for a single argument to be passed:

branch-current = "symbolic-ref --short HEAD"  # https://stackoverflow.com/a/19585361/5353461
branch-names = !"[ -z \"$1\" ] && git branch-current 2>/dev/null || git branch --format='%(refname:short)' --contains \"${1:-HEAD}\" #"  # https://stackoverflow.com/a/19585361/5353461
branch-name = !"br=$(git branch-names \"$1\") && case \"$br\" in *$'\\n'*) printf \"Multiple branches:\\n%s\" \"$br\">&2; exit 1;; esac; echo \"$br\" #"
Tom Hale
  • 40,825
  • 36
  • 187
  • 242