git refusing to fetch into current branch

GitGit Fetch

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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 2 - Git

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 3 - Git

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

Solution 4 - Git

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

Solution 5 - Git

I've had this problem when I thoughtlessly cloned a repository instead of fetching it, so that both repositories were masters. If you have done no work on the remote repository, you can fix things with the basic git commands as follows: (1) delete the remote repository, (2) copy the local repository to where the remote one was, (3) delete the local one, and then (4) set up the local repository using

git init; git fetch $REMOTE_GIT_REPOSITORY_URL  

Then git pull and git push will do the right things. The advantage of avoiding git remote, per Michael's more efficient and principled answer, is that you don't need to think about the semantics of tracking branches.

Solution 6 - Git

I have the same problem. First I try to fetch using this

git fetch [remote_name] [branch_name] 

I had the same problem you mention. After that I tried this simple command.

git pull [remote_name] [branch_name]

Note will fetch and merge the changes. If you using terminal then file open, requiring to commit message. With this message you will push the latest commit. Try this command and finally you will be able to push the request.

git push [remote_name] [branch_name_local] 

Solution 7 - Git

Are you actually typing Git commands into the command line, or are you running the Git executable from your own code?
If you're running it from code, are you sure that Git is trying to fetch into the correct local directory?

There are two possible ways to do this:

  1. Use the options provided by your programming language to set the correct working directory before executing Git
    (C# example, because that's what I'm using)

  2. Always pass the -C parameter to Git to specify the directory with the local repo.


I have a project where I'm calling the Git executable from C# code, and I got the same error message like in the question when I accidentally forgot to set the -C parameter.

Solution 8 - Git

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]...

Solution 9 - Git

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.

Solution 10 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOlivier VerdierView Question on Stackoverflow
Solution 1 - GitRobWView Answer on Stackoverflow
Solution 2 - GitMichael Krelin - hackerView Answer on Stackoverflow
Solution 3 - GitSaiView Answer on Stackoverflow
Solution 4 - GitroshnetView Answer on Stackoverflow
Solution 5 - GitCharles StewartView Answer on Stackoverflow
Solution 6 - GitHafiz Shehbaz AliView Answer on Stackoverflow
Solution 7 - GitChristian SpechtView Answer on Stackoverflow
Solution 8 - GitkxrView Answer on Stackoverflow
Solution 9 - GitVonCView Answer on Stackoverflow
Solution 10 - GitAdit choudharyView Answer on Stackoverflow