This is a long standing issue https://github.com/magit/magit/issues/2708 . I feel that a prompt for deleting the transient file would compensate in most cases. Is there a reliable way to achieve this? Thanks!
I have added the following to my .init.el file:
(defun magit-remove-git-lock-file () "Remove git's index lock file, if it exists." (interactive) (let ((base (magit-toplevel))) (delete-file (concat base "/.git/index.lock"))))
This gives me a function to resolve this condition when it occurs.
As a response to other statements made, please note that when this happens to me, there was no other process running git commands, and the lock file did indeed remain visible on-disk for many minutes, forcing me to delete it either manually or via this code snippet.
Is there a reliable way to achieve this?
No. It is inherently risky to do this because what you are trying to accomplish is to run multiple things in parallel that cannot be run in parallel and the file you want to remove is the file whose purpose it is to prevent that from happening.
Also I don't know how that could be implemented.