Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-fetch

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?

like image 692
Roman Starkov Avatar asked May 10 '15 00:05

Roman Starkov


2 Answers

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.

like image 152
Didier L Avatar answered Oct 06 '22 23:10

Didier L


I'm not an expert on this, so I've used Google and the man pages to help piece together what's going on.

This question should demystify the FETCH_HEAD aspect of things. It's not really possible for me to tell what the state of your repo is from reading your question, but presumably your FETCH_HEAD points to the latest commit in the HEAD in the remote repo, whatever that might happen to be. You probably already had those commits on your system, and so all git did was update FETCH_HEAD. You probably want to provide a branch instead.

When you add a remote, you get additional functionality, like a default branch and remote-tracking branches. I believe your second command as acting differently because git is employing some implicit logic to do what's typically wanted. Fetching directly from a URL is more of an advanced move, and you get fewer assumptions for that reason.

Moral of the story, if you want things to Just Work, add a remote. Otherwise, be prepared to grapple with more of the underlying details.

like image 37
acjay Avatar answered Oct 07 '22 01:10

acjay