101

I am using Dropbox to sync a git repository, but now when I try and push I am getting an error:

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

So, it seems that Dropbox detected a conflict and created a copy. Ok, no problem, so I deleted the conflicted file. Still, getting the above git error though.

$ 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`

How can I fix this? Thanks.

Justin
  • 42,716
  • 77
  • 201
  • 296

8 Answers8

178

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
Lane
  • 6,532
  • 5
  • 28
  • 27
42

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.

Marco Leogrande
  • 8,050
  • 4
  • 30
  • 48
  • There are conflicted files everywhere inside of `.git`. I went through and deleted them all, and its fixed. Thanks. – Justin Oct 07 '12 at 23:16
  • 2
    @Justin Glad I could help. If this answer or any other one solved your issue, please mark it as accepted. – Marco Leogrande Oct 07 '12 at 23:28
  • 2
    On Mac OS X use: `find . -name '*conflicted*'` (Edit: format) – xgMz Feb 03 '13 at 23:02
  • 1
    @xgMz Thanks, I will update my answer, as your solution has general validity. – Marco Leogrande Feb 03 '13 at 23:18
  • 1
    With a simple `find` I got hold of a single conflicting file and manually fixed the problem with two `mv`. Doing this slowly and reasoning step by step is the safest route. – pid Jul 14 '14 at 09:47
8

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. :)

Hareen Laks
  • 1,432
  • 2
  • 17
  • 33
  • 1
    worked absolutely perfect for me :) thx - for me dropbox created some strange string filename which was not compatible to "update" – cV2 May 05 '14 at 20:12
  • 1
    This worked for me! There was a file with "conflicted copy" in the name in the /refs/heads folder and deleting that, then pull/push and all is well. Thanks! – DiscDev Jun 22 '14 at 00:02
7

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

Drew
  • 8,675
  • 6
  • 43
  • 41
1

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.

Rajesh Paul
  • 6,793
  • 6
  • 40
  • 57
0

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

poke
  • 369,085
  • 72
  • 557
  • 602
  • Updated output in the original post. – Justin Oct 07 '12 at 22:54
  • Hm, could you try a full `git push origin master`, and also paste the the output of `git branch -a` please? – poke Oct 07 '12 at 23:07
  • Same error here... trying `git branch -a` results in this very error! – trusktr Mar 01 '14 at 07:48
  • The problem is on the remote repository, so pulling or checking out does not solve it. The other answers above refer to fixing directly the files on the remote and work perfectly. YOUR ADVISE IS VERY GOOD but it is not an answer to the problem. – pid Jul 14 '14 at 09:41
0

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
zeeawan
  • 6,667
  • 2
  • 50
  • 56
0

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.

Sithu
  • 4,752
  • 9
  • 64
  • 110