I would just like to document this issue, which arises from an incompatibility between git and cloud syncing services. Storing repositories in Dropbox-synced folders should be avoided.
In a machine with Windows 10 and Windows Subsystem for Linux, if the user has a git repository in a directory managed by Dropbox, the following error might appear when trying to git push
a recent commit:
warning: unable to unlink '/home/<local_repo>/repo/.git/refs/remotes/origin/main.lock': Permission denied
error: update_ref failed for ref 'refs/remotes/origin/main': couldn't set 'refs/remotes/origin/main'
Additionally, on git status
the repository will appear to be ahead of the remote, even while the remote has been updated:
$ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
Further pushes might give a File exists
error (however, the commit is successfully pushed to remote):
$ git push
error: update_ref failed for ref 'refs/remotes/origin/main': cannot lock ref 'refs/remotes/origin/main': Unable to create '/home/<local_repo>/.git/refs/remotes/origin/main.lock': File exists.
Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.
Everything up-to-date
The problem is that you're storing Git data in a directory managed by a cloud syncing service. Git requires that the file system have deterministic behavior, specifically that set by POSIX. When you use this type of cloud syncing service, however, the syncing tool can modify the contents of the working directory or .git
directory in ways that don't reflect what Git has done. As a result, you can end up with situations where you have lock files that reappear, operations that fail (because their lock files are removed or are in use), or duplicate files, among other problems.
It is also well known that using cloud syncing services can and does cause data loss. Git writes files to disk in a particular order to preserve the consistency of the repository, and if they are synced to another machine incompletely or at a point when the repository is changing, the repository can become corrupt. The Git maintainers strongly advise against doing this, and a future version of the Git FAQ will instruct users specifically not to.