Cannot remove .git: Directory not empty

Viewed 12427

Unable to remove .git

I have a git repository, Rift, that I am trying to remove. On attempting to run rm -rf, I get the error: rm: cannot remove 'Rift/.git/objects/pack': Directory not empty.

When I navigate to the bottom of the directory tree, I find a hidden file, called .fuse_hidden followed by a string of numbers and letters, possibly hexadecimal. I can manually remove this file, but as soon as I remove it, another with a different string of numbers and letters appended to it is created in it's place.

I have tried rm .git/objects/pack/* && rm -rf .git, sudo rm -rf .git, chmod -w .git/objects/path and killall git, none of which have had any success.

enter image description here

4 Answers

You can use the fuser command.

The fuser is a command line utility intended to locate processes based on the files, directories, or a socket a particular process is accessing. It helps a system user identify processes using files or sockets.

Use the fuser command on the .git directory, to find all of the process ids which are accessing the directory.

fuser .git

Afterwards you can use the -k parameter to kill the processes, and then you should be able to delete the directory.

fuser -k .git

Another solution I have recently found is to use the lsof command, using the afflicted filename as a parameter. For example:

#This will give you information about what process has the file open
[root@host]$ lsof .git/objects/pack/.fuse_hidden000000000f1f
#This will kill the process locking the file
[root@host]$ kill -s 9 $(lsof -t .git/objects/pack/.fuse_hidden000000000f1f)

The -t switch tells lsof to use "terse" mode, only returning the PID. This PID is then passed to kill using the -s 9 flag, meaning to send the signal "SIGKILL" instead of the default more polite request "SIGTERM" or potentially "SIGHUP"

For linux user if you're facing this issue use below command on your terminal

rm -r directory_name

Then after running this it will prompt you again and again whether you want to remove a write protected file, enter yes and press enter , it will be deleted soon

use below command on your terminal

sudo rm -r directory_name

Related