拒绝进入当前分支的饭桶

我建立了一个远程存储库,我可以将新的更改推送到它,但是我不能从中获取,我总是得到(相当隐晦的)错误消息:

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

这是什么意思? 我应该做什么来启用抓取?

(注意,这个远程回购只是作为一个备份回购,所以它应该是我的本地存储库的精确副本。我真的不明白为什么我可以推它,但不能从它...)

我的配置是这样的:

[remote "origin"]
url = ssh://blablablah
fetch = +refs/*:refs/*
mirror = true
69049 次浏览

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

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.

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.

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]

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

git pull origin master

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.

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.

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

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

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.