0

On my Linux server there were several git branches I wanted to remove. I used git branch -D branche_name and deleted all these branches except for one. For this one branch called feat/implement-h I get the error:

“error: cannot lock ref 'refs/heads/feat/implement-h': Unable to create '/data/web/xxx/xxx/.git/refs/heads/feat/implement-h.lock': Permission denied”.

I recently got access to this Linux server, which has been used for several years but I didn't have access before. Any idea how I could remove this branch?

Nick
  • 67
  • 1
  • 1
  • 9
  • Did you check file ower and permissions? It sounds like something in the `.git` dir is owned by some other user. – Halfgaar May 12 '20 at 13:07

1 Answers1

1

As Halfgaar says it is probably a permissions problem. I have found that files belonging to root tend to accumulate in my git repositories, albeit very slowly. In my case they often seem to be in

.git/objects

and don't seem to cause problems there. However, if a lock file ends up belonging to root that is obviously going to be an issue. You can check for such files with

find ~/your/git/dir -group root -ls

My assumption is that, very occasionally, when Git throws an error, the ownership information can become corrupted.

loris
  • 232
  • 1
  • 12
  • Thanks @Ioris, it returns the 2 lines below. Indeed, root seems to be owner. I'm a newbie to this: how could I solve this? `1204263 4 drwxr-xr-x 2 root root 4096 May 6 16:49 /your/.git/refs/heads/feat/` --- `1201452 4 -rw-r--r-- 1 root root 41 May 6 16:49 /your/.git/refs/heads/feat/implement-h` – Nick May 12 '20 at 14:45
  • As ```root``` you need to change the ownership for the files, e.g. ```chown nick:users /your/.git/refs/heads/feat/implement-h```. The best thing is probably to do this with ```find```, e.g. ```find ~/your/git/dir -group root -exec chown nick:user {} \;```. Here I'm assuming that your primary group is ```users```, but it could be something else (see ```id nick```). I'm also assuming you can log in directly as ```root```. However, if you are, say, logged into Ubuntu as ```nick```, you will need to use ```sudo``` in front of either of the commands above. – loris May 13 '20 at 05:45