git refusing to fetch into current branch

Viewed 66814

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
10 Answers

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/*.

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

When you really want to fetch into the current branch - e.g. like fetch -u origin mybranch:mybranch from a twin repo (and its not due to a bogus config like in the OP case), you may more conveniently do:

git pull [origin] --ff-only

This allows only a fast-forward update, extending a linear history. It already updates the index unlike the fetch -u.

In case this fails due to diverged commit history and you want to achieve unity again immediately (instead of creating merge daimonds etc.), you may consider rebasing onto the remote - e.g. by:

git pull --rebase[=interactive]

Which can be broken up into:

git fetch origin mybranch
# inspect the diverge situation 
git rebase origin/mybranch mybranch [-i]...

Note that this error message won't apply just to your current branch, but to any branch checked out as a worktree.

"git fetch"(man) without the --update-head-ok option ought to protect a checked out branch from getting updated, to prevent the working tree that checks it out to go out of sync.

The code was written before the use of "git worktree"(man) got widespread, and only checked the branch that was checked out in the current worktree, which has been updated with Git 2.35 (Q1 2022).

See commit 593a2a5, commit 9fdf4f1, commit 38baae6, commit 8bc1f39, commit c8dd491, commit 7435e7e, commit c25edee, commit 66996be (01 Dec 2021) by Anders Kaseorg (andersk).
(Merged by Junio C Hamano -- gitster -- in commit 13fa77b, 21 Dec 2021)

fetch: protect branches checked out in all worktrees

Signed-off-by: Anders Kaseorg

Refuse to fetch into the currently checked out branch of any working tree, not just the current one.

Fixes this previously reported bug.

As a side effect of using find_shared_symref, we'll also refuse the fetch when we're on a detached HEAD because we're rebasing or bisecting on the branch in question.
This seems like a sensible change.


With Git 2.37 (Q3 2022), the way "git fetch"(man) without --update-head-ok ensures that HEAD in no worktree points at any ref being updated was too wasteful, which has been optimized a bit.

See commit f7400da (16 May 2022) by Orgad Shaneh (orgads).
(Merged by Junio C Hamano -- gitster -- in commit 5ed49a7, 25 May 2022)

fetch: limit shared symref check only for local branches

Signed-off-by: Orgad Shaneh

This check was introduced in 8ee5d73 ("Fix fetch/pull when run without --update-head-ok", 2008-10-13, Git v1.6.1-rc1 -- merge) in order to protect against replacing the ref of the active branch by mistake, for example by running git fetch origin master:master(man) .

(see "Why "git fetch origin branch:branch" works only on a non-current branch?")

It was later extended in 8bc1f39 ("fetch: protect branches checked out in all worktrees", 2021-12-01, Git v2.35.0-rc0 -- merge listed in batch #4) to scan all worktrees.

This operation is very expensive (takes about 30s in my repository) when there are many tags or branches, and it is executed on every fetch, even if no local heads are updated at all.

Limit it to protect only refs/heads/* to improve fetch performance.

fatal: Refusing to fetch into current branch refs/heads/BRANCHNAME of non-bare repository

I have Created a branch BRANCHNAME locally then executed command "git fetch upstream pull/ID/head:BRANCHNAME" and got fatal: Refusing to fetch into current branch refs/heads/BRANCHNAME of non-bare repository

then i deleted the branch and again call the same cmd it ways fine.

Actually i was checking out pull request branch.

Related