Reputation: 13099
Help! When I try to do a git push origin master
now, I get:
fatal: Unable to create '/home/ubuntu/workspace/.git/refs/remotes/origin/master.lock': File exists.
If no other git process is currently running, this probably means a
git process crashed in this repository earlier. Make sure no other git
process is running and remove the file manually to continue.
Of course, I've found other similar questions (e.g. here), but the upvoted answers in there merely suggest deleting the offending file... but the file does not exist!
I fear that this is happening after I recently tried to clear some space using the following:
$ sudo git reflog expire --all --expire=now
$ sudo git gc --prune=now --aggressive
Could that be the cause? Any suggestions for a fix?
Upvotes: 4
Views: 3558
Reputation: 489858
The problem appears to have stemmed from running the expiration code as the super-user (sudo ...
). When Git adjusted the remote-tracking branch files, they became owned by (and hence only adjustable by) the super-user.
The fix is to put those files back to the correct owner (in this case ubuntu
). One can use a blanket ownership-change, e.g., sudo chown -R ubuntu .git
, or a selective one (change only mis-owned files), e.g., as root (or with sudo
again, this one is a bit more annoying because of the pipeline) find .git -user root -print0 | xargs -0 chown ubuntu
. The only difference, if any, between these would be that chown -R
might update more files' ctime
(inode-change time) fields (by actually running chown
system calls on files already properly owned), which can in turn affect backup systems.
The more general rule is "don't arbitrarily prefix stuff with sudo
"—in this case, there was no reason to put sudo
in front of the git reflog expire
and git gc
commands.
Upvotes: 5