git refusing to fetch into current branch
Asked Answered
K

10

106

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
Keldah answered 10/2, 2010 at 12:33 Comment(5)
Can you show your config for the repository that your are fetching into?Malchy
Mmh, how can I see that configuration? I just set up that remote repository using git remote add name server, perhaps with the --mirror option.Keldah
Oliver, the configuration is in .git/config. In particular, Charles is talking about the [remote …] section, I believe.Mizzen
My config looks like that: [remote "origin"] url = ssh://blablablah fetch = +refs/*:refs/* mirror = trueKeldah
You shouldn't be mirror-fetching to the repository you work on. What you want is removing mirror and, basicaly, following my advice given in the answer.Mizzen
M
52

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

Mizzen answered 10/2, 2010 at 12:45 Comment(4)
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!Keldah
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.Mizzen
@MichaelKrelin-hacker what should I write in place of "thehost"Interphase
The name of the host where your repository is. Basically, the thehost:/the/path.git part should be replaced with your repository url.Mizzen
M
100

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.

Malamud answered 6/10, 2013 at 5:17 Comment(5)
Why doesn't git want to update the head in this case? Why do they not want you using this flag?Revenge
I'm curious as well, why is it okay for fetch to update all other branches except the current?Trail
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)Robet
In case you'd like to just throwaway your master, and point it to the origin's master: git fetch -fu origin masterTrail
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).Cilla
M
52

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

Mizzen answered 10/2, 2010 at 12:45 Comment(4)
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!Keldah
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.Mizzen
@MichaelKrelin-hacker what should I write in place of "thehost"Interphase
The name of the host where your repository is. Basically, the thehost:/the/path.git part should be replaced with your repository url.Mizzen
T
13

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

git pull origin master

Tieratierce answered 13/4, 2017 at 18:45 Comment(0)
N
5

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
Nagpur answered 27/8, 2021 at 11:10 Comment(0)
E
2

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.

Elwaine answered 24/12, 2021 at 18:10 Comment(0)
P
0

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.

Patter answered 14/9, 2011 at 12:14 Comment(1)
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.Levona
A
0

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] 
Academia answered 11/4, 2015 at 6:10 Comment(0)
D
0

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.

Dichasium answered 8/9, 2017 at 19:12 Comment(0)
A
0

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]...
Accalia answered 7/3, 2021 at 19:21 Comment(0)
H
-1

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.

Hydrogenate answered 25/2, 2019 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.