git refusing to fetch into current branch

60,096

Solution 1

What you're trying to do is to fetch the branch you're workin on. That is, you are on the master branch and you try to update it. That's not possible. It's more common to update the remotes/* branches and then pull it into your local ones. What you want is, perhaps,

git remote add otherrepo thehost:/the/path.git

That will setup repository to be fetched into remotes/otherrepo/*. git fetch otherrepo should do the trick. Alternativeley, you can manually edit your .git/config and set fetch for the remote to something like refs/heads/*:refs/remotes/otherrepo/*.

Solution 2

In case anyone finds this because they specifically want to fetch into the current branch, you can use the --update-head-ok flag. From the docs:

-u
--update-head-ok
By default git fetch refuses to update the head which corresponds to the current branch. This flag disables the check. This is purely for the internal use for git pull to communicate with git fetch, and unless you are implementing your own Porcelain you are not supposed to use it.

In some cases we do want to implement our own porcelain commands, e.g., automation and tooling.

Solution 3

Also this this should work if you are in master branch and wants to get latest try this

git pull origin master

Solution 4

To deal with this when fetching a PR to test locally, just checkout another branch and then fetch the PR.

git checkout -b feature-branch # create a branch to fetch changes into.
git checkout master # or main
git fetch origin pull/5/head:add-feature # fetch pull request by ID
git checkout feature-branch
Share:
60,096
Olivier Verdier
Author by

Olivier Verdier

Updated on December 25, 2021

Comments

  • Olivier Verdier
    Olivier Verdier over 2 years

    I set up a remote repository and I can push new changes to it, but I cannot fetch from it, I always get the (rather cryptic) error message:

    fatal: Refusing to fetch into current branch refs/heads/master of non-bare repository
    fatal: The remote end hung up unexpectedly
    

    What does it mean? What should I do to enable fetching?

    (Note that this remote repo is only used as a backup repo, so it should be pretty much an exact copy of my local repository. I really can't understand why I can push to it but not fetch from it...)

    My config looks like:

    [remote "origin"]
        url = ssh://blablablah
        fetch = +refs/*:refs/*
        mirror = true
    
  • Olivier Verdier
    Olivier Verdier about 14 years
    Thanks! I do not really understand what I am doing, but following your advice it now works fine. I sort of guess that one has to explicitly say where the remote branches will end up in the local repository; I probably had the remote branch sort of overlapping my local branch, although I'm not sure if that makes sense. :-) Thanks anyway!
  • Michael Krelin - hacker
    Michael Krelin - hacker about 14 years
    More or less. The fact that remote refs are in "refs/remotes" is just a convention, but you really don't want to be fetching directly into your "refs/hreads/master". Especially, when your "refs/heads/master" is checked out.
  • qneill
    qneill over 11 years
    A clone is a clone. The "master repo" is purely a convention. The question here is about the behavior of "git fetch remote ref:ref" when HEAD is set to ref.
  • Chris Marisic
    Chris Marisic almost 8 years
    Why doesn't git want to update the head in this case? Why do they not want you using this flag?
  • cdosborn
    cdosborn almost 8 years
    I'm curious as well, why is it okay for fetch to update all other branches except the current?
  • Diomidis Spinellis
    Diomidis Spinellis about 7 years
    I thought the restriction was there to prevent you from loosing work, but I verified that even with the -u flag non-fast-forward commits are rejected: ! [rejected] master -> master (non-fast-forward)
  • cdosborn
    cdosborn over 6 years
    In case you'd like to just throwaway your master, and point it to the origin's master: git fetch -fu origin master
  • Punit Vara
    Punit Vara over 6 years
    @MichaelKrelin-hacker what should I write in place of "thehost"
  • Michael Krelin - hacker
    Michael Krelin - hacker over 6 years
    The name of the host where your repository is. Basically, the thehost:/the/path.git part should be replaced with your repository url.
  • Erik Koopmans
    Erik Koopmans almost 5 years
    Note that this will update the head but not the working directory. To make the working directory match the head, use git reset --hard (careful, this will discard any unsaved changes).