Search code examples
gitgit-fetch

What's the difference between "git fetch <url>" and "git add remote upstream <url>" followed by "git fetch upstream"?


I was trying to pull changes from upstream into my fork. I tried git fetch https://github.com/someuser/someproject, but it stubbornly refused to do anything. It just said something totally cryptic and did nothing, namely:

From https://github.com/someuser/someproject
 * branch            HEAD       -> FETCH_HEAD

But the moment I added the URL as a named remote, things changed:

> git remote add upstream https://github.com/someuser/someproject.git
> git fetch upstream
remote: Counting objects: 340, done.
remote: Compressing objects: 100% (268/268), done.
remote: Total 340 (delta 145), reused 18 (delta 16), pack-reused 44
... etc ...

So what's the difference? Why did it do nothing when I specified a remote without adding it first? And what exactly was it telling me when I tried to fetch from the URL?


Solution

  • When you fetch with a URL, you also have to specify the <refspec> you want to fetch, i.e. the branch or tag, otherwise it will just fetch the default HEAD of the remote URL as FETCH_HEAD, which is probably not what you want.

    The syntax is an optional + followed by <src>:<dst>. If you omit <dst>, FETCH_HEAD will be used.

    So for example:

    git fetch https://github.com/someuser/someproject refs/heads/master:upstream/master
    

    which will create the upstream/master remote branch locally.

    More advanced options are available.