致命的 Git: Reference 具有无效的格式: ‘ refs/head/master

我正在使用 Dropbox来同步一个 git存储库,但是现在当我尝试和 push同步时,我得到了一个错误:

fatal: Reference has invalid format: 'refs/heads/master (MacBook-Pro's conflicted copy 2012-10-07)'

因此,Dropbox 似乎检测到了一个冲突并创建了一个副本。好的,没问题,所以我删除了冲突文件。尽管如此,还是得到了上面的 git 错误。

$ git checkout master
M   index.html
Already on 'master'
$ git add .
$ git commit -a -m "Cleanup repo"
[master ff6f817] Cleanup repo
1 file changed, 5 insertions(+), 5 deletions(-)
$ git push
fatal: Reference has invalid format: 'refs/heads/master (MacBook-Pro's conflicted copy 2012-10-07)'
The remote end hung up unexpectedly`

我该怎么办? 谢谢。

33476 次浏览

Try a git checkout master first to get on the healthy, well-named branch.

The conflicted file could be in multiple places, I would look into:

.git/logs/refs/remotes/origin/
.git/logs/refs/heads/
.git/refs/remotes/origin/
.git/refs/heads/

Or you might look everywhere in the .git subdirectory: find . -name '*conflicted*'

Or, otherwise, list the active branches with git branch -a and delete (git branch -d) anything suspicious.

I was able to delete all the conflicted files from my .git folder, but I continued to get errors about files that no longer existed.

The fix for me was opening up .git/refs/packed_refs and deleting lines that contained the text "conflicted".

make a backup of your repo if you aren't sure about this one, because these commands are irreversible.

first, go to your repo directory.

cd myrepo

then recursively search for the conflicted files and delete them

find . -type f -name "* conflicted copy*" -exec rm -f {} \;

lastly, remove any "conflicted" references from git's packed-refs file

awk '!/conflicted/' .git/packed-refs > temp && mv temp .git/packed-refs

This also happen to our team when my colleague push his changes and shutdown the PC before Dropbox get updated.

I solved it so simply.

Just deleted the conflicted copy. (XXXX's conflicted copy yyyy-mm-dd)

And pull it normally.

Note that my colleague had the changes before messed up. And he push his changes again. This time no shutdown. :)

I was getting same error

fatal: Reference has invalid format: 'refs/heads/somebranch (1)'

for the following command

git branch

Then, I searched for erroneous name (branch name followed by (1) ) using the command

find . -name 'somebranch (1)'

And it showed the following result

./.git/refs/heads/somebranch (1)

Which is some duplicated version of somebranch IMO. So, I removed this by executing the find command following by delete

find . -name 'somebranch (1)' -print -exec rm -rf {} \;

Then the branch command run successfully

git branch

For me it was giving error: fatal: Reference has invalid format: 'refs/tags/r0.2:3'

You can go to /.git/packed_refs file and delete the line for refs/tags/r0.2:3

Then it started working. But why it happened in the first place I don't know.

I encounterd the similar error such as

fatal: Reference has invalid format: 'refs/heads/user-search-api (Sithu's conflicted copy 2016-01-08)'

Simply deletion of the file .git/refs/heads/user-search-api (Sithu's conflicted copy 2016-01-08) in the remote Dropbox repository did solve the problem.