How to Fix “refusing to fetch into branch” in Git
Running git fetch origin main:main while main is checked out stops with fatal: refusing to fetch into branch 'refs/heads/main' checked out at '...'. Git refuses because the checked-out branch (the one tied to your working files) would be rewritten directly by the fetch. To avoid the error, fetch into origin/main with git fetch origin, then bring it in with git merge --ff-only origin/main, or use git pull.
The error appears when you add main:main to git fetch to update your local main from the remote's latest. If you have changed the fetch configuration (remote.origin.fetch), it can also appear with a plain git fetch with no arguments. I reproduced it on purpose in a test environment, and checked the message and that it goes away after the fix.
The test environment is Ubuntu 24.04, Git 2.43.0, and bash 5.2. I ran the commands with LANG=C to get the English messages. The remote was a bare repository (remote.git) placed inside the same test environment. I did not test fetching from a server such as GitHub, Windows or macOS, or cases where the local and remote both have different commits. In the output, the working folder path is replaced with /path/to.
- 1. Fetching main:main while main is checked out stops with an error
- 2. Fetch into origin/main and then merge
- 3. If the branch is not checked out, main:main can update it
- 4. The error also appears when the branch is checked out in another worktree
- 5. When a plain git fetch shows the error, check remote.origin.fetch
- 6. With –update-head-ok, the files are left out of date
Fetching main:main while main is checked out stops with an error
In a local clone (neko), the checked-out branch is main. The remote has one more commit, "maguro", that is not in the local repository yet. In this state, I tried to fetch the remote's main directly into the local main.
$ git branch --show-current
main
$ git fetch origin main:main
fatal: refusing to fetch into branch 'refs/heads/main' checked out at '/path/to/neko'
The exit code was 128. main stayed at the original "Create menu" commit.
$ git log --oneline -1 main
3a2a7fa メニューを作成
In main:main, the left side is the remote branch and the right side is the local branch to update. The git fetch reference for –update-head-ok says that by default git fetch refuses to update a ref that corresponds to the currently checked-out branch. A fetch only changes where the branch points and does not touch the working files. If it were allowed, the branch would become new while the files stayed old, leaving them out of sync (I tested this in the last section).

Fetch into origin/main and then merge
Normally, a remote branch is fetched into the remote-tracking branch (origin/main). Running git fetch origin without main:main produced no error and updated origin/main.
$ git fetch origin
From /path/to/remote
3a2a7fa..e1134c1 main -> origin/main
$ git log --oneline -1 origin/main
e1134c1 まぐろ
Next, git merge --ff-only origin/main brings it into the checked-out branch. The local main moved up to "maguro" and matched origin/main.
$ git merge --ff-only origin/main
Updating 3a2a7fa..e1134c1
Fast-forward
menu.txt | 1 +
1 file changed, 1 insertion(+)
$ git log --oneline -1 main
e1134c1 まぐろ
$ git status --short --branch
## main...origin/main
As the git merge reference for –ff-only describes, --ff-only merges only when it can be a fast-forward, and otherwise stops without making a merge commit. If you only want to bring your local main up to the same commit as the remote, this is enough.
If you want to fetch and merge in one step, use git pull. With one more commit ("saba") on the remote, git pull --ff-only origin main ran the fetch and the fast-forward in sequence, and no error appeared.
$ git pull --ff-only origin main
From /path/to/remote
* branch main -> FETCH_HEAD
e1134c1..92e470a main -> origin/main
Updating e1134c1..92e470a
Fast-forward
menu.txt | 1 +
1 file changed, 1 insertion(+)
$ git log --oneline -1 main
92e470a さば
The differences between what git fetch and git pull change, and how far, are covered in a separate article, "How to compare git fetch and git pull output side by side."
If the branch is not checked out, main:main can update it
The main:main form itself is not the problem. The error only happens when the branch being updated is the checked-out one. After switching to a separate feature branch, the same git fetch origin main:main succeeded, and the local main moved up to the new "aji" commit.
$ git switch -c feature
Switched to a new branch 'feature'
$ git fetch origin main:main
From /path/to/remote
92e470a..b0d24fb main -> main
92e470a..b0d24fb main -> origin/main
$ git log --oneline -1 main
b0d24fb あじ
This form is useful when you want to keep working on another branch and just update the local main without switching. When I switched back to main later, it matched origin/main.
$ git switch main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
In this test, the remote main only had new commits added on top, so it could be updated as a fast-forward. The git fetch reference examples say that a refspec with a leading + is updated even when it is not a fast-forward, and a refspec without + is not updated when it is not a fast-forward.
The error also appears when the branch is checked out in another worktree
If main is checked out in another folder with git worktree, you get the same error even when the current folder is on feature. The last path in the message showed the folder of the worktree where main was open.
$ git worktree list
/path/to/neko 92e470a [feature]
/path/to/neko-main b0d24fb [main]
$ git fetch origin main:main
fatal: refusing to fetch into branch 'refs/heads/main' checked out at '/path/to/neko-main'
In this case, bring it in from the folder shown in the message. Running git -C ../neko-main pull --ff-only origin main moved main up to the new "iwashi" commit.
$ git -C ../neko-main pull --ff-only origin main
From /path/to/remote
* branch main -> FETCH_HEAD
b0d24fb..0153fd7 main -> origin/main
Updating b0d24fb..0153fd7
Fast-forward
menu.txt | 1 +
1 file changed, 1 insertion(+)
$ git log --oneline -1 main
0153fd7 いわし
If you no longer need that worktree, you can also remove it with git worktree remove. After removing it, with "kajiki" added to the remote, git fetch origin main:main ran without error and advanced main.
$ git worktree remove ../neko-main
$ git fetch origin main:main
From /path/to/remote
0153fd7..eab1327 main -> main
0153fd7..eab1327 main -> origin/main
$ git log --oneline -1 main
eab1327 かじき
When a plain git fetch shows the error, check remote.origin.fetch
If the error appears even though you did not write main:main in the command, check the fetch configuration. As the git fetch reference section on configured remote-tracking branches describes, a git fetch with no branch specified uses the refspecs (rules for which branches are fetched and where they go) written in remote.origin.fetch.
In the test environment, I changed this setting to +refs/heads/*:refs/heads/* (fetch remote branches directly into the local branches with the same names). After that, a plain git fetch gave the same error.
$ git config remote.origin.fetch '+refs/heads/*:refs/heads/*'
$ git fetch
fatal: refusing to fetch into branch 'refs/heads/main' checked out at '/path/to/neko'
I restored the value that a fresh clone sets, +refs/heads/*:refs/remotes/origin/*. Fetching then went to origin/main, the error went away, and git merge --ff-only origin/main brought the changes in, as in the earlier method.
$ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
$ git config --get-all remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
$ git fetch
From /path/to/remote
eab1327..acc4b0f main -> origin/main
$ git merge --ff-only origin/main
Updating eab1327..acc4b0f
Fast-forward
menu.txt | 1 +
1 file changed, 1 insertion(+)
With –update-head-ok, the files are left out of date
git fetch has an option, --update-head-ok (-u), that skips this check. However, the official reference says it is meant for git pull to use internally, and unless you are building your own tools on top of Git, you should not use it.
When I ran git fetch --update-head-ok origin main:main while main was checked out, no error appeared and main moved up to the new "tara" commit. However, the content of menu.txt was still the old one, with no "tara" line.
$ git fetch --update-head-ok origin main:main
From /path/to/remote
acc4b0f..f6f8770 main -> main
acc4b0f..f6f8770 main -> origin/main
$ git log --oneline -1 main
f6f8770 たら
$ cat menu.txt
かつお
まぐろ
さば
あじ
いわし
かじき
さんま
git status --short showed menu.txt as a staged change (M ). git diff --cached shows a change that removes the "tara" line, which the new commit added.
$ git status --short
M menu.txt
$ git diff --cached
diff --git a/menu.txt b/menu.txt
index b19d453..46dbaa4 100644
--- a/menu.txt
+++ b/menu.txt
@@ -5,4 +5,3 @@
いわし
かじき
さんま
-たら
As git diff --cached shows, the stage contains a change that removes the "tara" line. Leave updating the checked-out branch to git merge or git pull.
In the test environment, I confirmed there were no other edited files, then ran git reset --hard main to reset the stage and working files to match main. git reset --hard discards uncommitted changes to tracked files.
$ git reset --hard main
HEAD is now at f6f8770 たら
$ git status --short
$ cat menu.txt
かつお
まぐろ
さば
あじ
いわし
かじき
さんま
たら





