Search code examples
gitgit-fetchgit-statusremote-repository

Git fetch not pulling changes when using URL


I'm trying to set up a service that will update my repository automatically when it detects changes on the remote branch. Before anyone mentions it, I'm aware that embedding the password in the URL is bad practice. I'm currently using:

git fetch https://<user>:<pass>@github.com/<org>/<repo>

Which gives an output of:

From https://github.com/<org>/<repo>
 * branch            development -> FETCH_HEAD

When I run git status after doing the fetch, it doesn't detect the most recent changes.

If I run a normal git fetch without the URL and supply the username and password manually, I get a different output:

From https://github.com/<org>/<repo>
   96353f7..e88782c  development -> origin/development

And then running git status works as predicted and detects that there are changes on the remote branch.

I'm not very experienced with Git and I feel like I'm missing something very obvious. Where is my error?

Edit

So it looks like the missing piece was for me to specify the local and remote branch after the URL:

git fetch https://<user>:<pass>@github.com/<org>/<repo> development:origin/development

As the accepted answer points out: if you don't specify the remote name, git puts the remote changes in the default FETCH_HEAD. This is resolved by specifying the local and remote branches explicitly.

I'm still a little confused as to why git fetch knows to grab from the origin remote by default, but specifying the URL changes this default. But it's working now in any case


Solution

  • Git remotes are a name associated with a URL. The default name is origin and the URL is usually whatever you cloned from.

    Git stores a snapshot of remote repositories in "remote tracking branches". These are of the form <remote-name>/<branch>. When you git fetch origin Git goes and fetches changes from the URL associated with origin and puts them into remote branches. development -> origin/development is telling you that it put the remote's development branch into your origin/development remote branch.

    When you run git fetch https://<user>:<pass>@github.com/<org>/<repo> and give it just the URL, no remote name, Git doesn't have a name for its remote tracking branches. So it just sticks them in an ephemeral FETCH_HEAD which records your last git fetch. That's what development -> FETCH_HEAD is telling you.


    Regardless of any of this, git status will show no change. git fetch never changes your local branches nor your index nor your checkout. It will only update your remote tracking branches. This makes it safe to habitually run git fetch to update your snapshot of the remote repository.

    What you're looking for to update your local copy of a remote branch is git pull.


    Finally, to avoid having to type in your username and password every time, or worse hard coding them into the URL, set up a Git credential helper to securely store your username and password.