49

This is a huge pain in the bum. I've got this a few times before and I don't understand why. 5 mins ago the repo was fine and working, I move some files around (which is all cool and all) and git poops its pants. Any idea why this happens? How can I fix it apart from cloning the repo, moving the files, etc...

Ahmed Nuaman
  • 12,662
  • 15
  • 55
  • 87

13 Answers13

56

Oh dear I'm such a fail. It looks like the problem stems from Flash Builder copying over other .git repos into sub folders. The answer is to remove all .git folders that aren't the repo's one.

Ahmed Nuaman
  • 12,662
  • 15
  • 55
  • 87
  • I didn't know I still have some .git related files from an old submodule lying around - after I removed them, I'm back in black. – electblake Oct 11 '12 at 05:11
  • actually it would probably be necessary to remove `.git` folders from `vendor` folder of only the problematic-ones dependencies. It has happened to me with `@dev` dependency after that target repo got through some rebasing. – hejdav Jun 04 '15 at 07:08
  • 1
    Thanks, this is exactly what happen to me. Flash Builder copied .git folder to bin-debug folder. I have no idea how that happened. Why would Flash Builder copy .git folder to bin-debug? – Caslav Sabani Jul 24 '18 at 08:18
  • I performed find search-location/ -name '.git' which gives me the location of all .git folders in the subdirectories. I removed all except the main one and this solved the issue. – Kumar A. Mar 29 '19 at 10:18
  • It can happen with `git pull` in the submodule. – Friedrich -- Слава Україні Oct 01 '21 at 11:57
12

I ran into this error because of a corrupted/not properly initialized submodule (with its own .git subfolder). I temporarily deleted the submodules folder and used git init in the main project's root. Fixed the problem for me.

Michael Thiel
  • 2,434
  • 23
  • 21
  • 1
    This was my cause as well, but in addition to removing the submodule folders I had to rerun `git submodule init && git submodule update` as well. – J. Perkins Mar 29 '17 at 12:22
10

I know this is an old thread, but I just had the same problem and ended up solving in a different way. The git init didin't work for me. Posting here, in case it's useful to anyone else.

My repository has two submodules. After rebasing I started getting the error fatal: git status --porcelain failed.

The solution was to verify the property worktree in every submodule config file - e.g. <repository-checkout>/.git/modules/<submodule-name>/config.

I had one invalid path for the worktree property. It was linking to an unexisting folder that was changed and merged to master - probably due an error resolving conflicts.

Tom
  • 1,387
  • 3
  • 19
  • 30
8

In case it helps anyone else, I just encountered the same issue and found that running git init in the project root fixed it.

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
6

I had the same issue. Running git status in my root project's directory produced the following error:

fatal: This operation must be run in a work tree
fatal: 'git status --porcelain' failed in submodule js/object-subscribe

Running git status in the affected submodule (js/object-subscribe) would produce this error:

fatal: This operation must be run in a work tree

Running git init in that submodule's folder did it for me.

Web_Designer
  • 72,308
  • 93
  • 206
  • 262
5

In my case after moving moduleA/mySubmodule to moduleB/mySubmodule using git mv moduleA moduleB with git 2.12.2, I ran into the following error:

$ git status
fatal: Could not chdir to '[../]moduleA/mySubmodule': No such file or directory
fatal: 'git status --porcelain' failed in submodule mySubmodule
fatal: 'git status --porcelain' failed in submodule moduleB

Then I did the following (Maybe not in this order)

  1. Manually update .gitmodules entry to moduleB
  2. Enter .git/modules and rename old module folder
  3. Enter moduleB and delete submodules folder
  4. Run git submodule sync and git submodule update

After that, I could run git status again without problems.

AJM
  • 1,317
  • 2
  • 15
  • 30
Checo R
  • 862
  • 14
  • 22
3

Usually, git creates a hidden directory in project's root directory (.git/)

When you're working on a CMS, its possible you install modules/plugins carrying .git/ directory with git's metadata for the specific module/plugin

If you do not want to use git's submodules feature, quickest solution delete all .git directories except root git metadata directory. If you do so, git will not consider those modules as project submodules.

cd /path/to/your/project/code
find ./ | grep ".git/index"

Once located delete ".git" all directories except the root one, but if you deleted it initialize your repo again

yilmi
  • 162
  • 5
1

.git is a file in submodules and points to a directory located in your root .git directory.

In my case, I was mounting a git directory in docker and checking status there. The .git file of this submodule contained an absolute path which was invalid in docker. I edited this .git file to change the gitdir path to a relative path.

Git version: 2.7.4

Shubham Chaudhary
  • 47,722
  • 9
  • 78
  • 80
1

I managed to solve the issue as follows:

Lets assume subfolder is the submodule where I was facing the issue, I moved/deleted the folder then sync and then update...

mv AD_Soft/subfolder AD_Soft/subfolder2
git submodule sync --recursive AD_Soft/subfolder/ 
git submodule update --recursive AD_Soft/subfolder

This solves the issue for me..

Lutz Prechelt
  • 36,608
  • 11
  • 63
  • 88
user750066
  • 43
  • 6
0

For me it was different, nor git init solved the issue nor the worktree was wrong. The error that I had was

fatal: unable to create threaded lstat
fatal: 'git status --porcelain=2' failed in submodule ext/hdf5

The solution for me was git gc.

Zythos
  • 180
  • 2
  • 8
0
git remote -v
#now run the same commands 
git remote add origin remote-repository-url.git
git push origin master

// It's work very well

0

Delete the .git folder from the module. Please keep the backup of that folder if you want to use it future.

0

I don't have any .git folders in my repo, but anytime I copy my new files into my folder to update my app, I am still getting index corrupt porcelain failed errors. I don't understand how updating files would corrupt this or how to fix it. Anyone have further insight on this?

Ryan
  • 3,153
  • 2
  • 23
  • 35
  • So you've only got one .git folder in the whole directory structure? – Ahmed Nuaman Aug 24 '11 at 16:04
  • I figured this out. For some reason, a cake plugin I had in the apps/plugins directory kept causing this to break. I spent like 3 hours replacing each folder until I found the one. I think it was just crappy code I found somewhere and wasn't even using it recently. Deleted that folder and everything is perfect. Thanks. – Ryan Aug 31 '11 at 17:17
  • Is it possible this could happen with a fresh install of any of `npm`'s CoffeeScript modules, specifically the `grunt` ones, even if I haven't created any `cake` plugins myself? – trysis May 25 '14 at 05:23
  • That is no answer at all! An by the way, /this/ is not forum. – doak Feb 17 '19 at 00:24