Dropbox 干扰 git 为 Linux 推送 Windows 子系统

Dropbox interfering with git push on Windows Subsystem for Linux

我只想记录这个问题,它是由 git 和云同步服务之间的不兼容引起的。应避免将存储库存储在 Dropbox 同步文件夹中。

在具有 Windows 10 和 Windows 子系统 Linux 的机器中,如果用户在 Dropbox 管理的目录中有 git 存储库,则会出现以下错误尝试 git push 最近提交时可能会出现:

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'

此外,在 git status 上,存储库将显示在远程之前,即使远程已更新:

$ 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

进一步推送可能会出现 File exists 错误(但是,提交已成功推送到远程):

$ 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

此问题是由于 git 和 Dropbox 之间的不兼容引起的。当用户创建提交,然后在 Dropbox 仍在重新索引和更新云上的目录时立即执行推送时,Windows 10 将不允许 WSL 上的 Ubuntu 在 Dropbox 正在删除锁定文件时正在访问文件夹。

解决方法是:
1)手动删除锁文件:
rm /home/<local_repo>/repo/.git/refs/remotes/origin/main.lock
2) 等待 Dropbox 重新索引目录(观察任务栏上的 dropbox 图标);
3) git push 再一次。
4) 从 Dropbox 或其他云存储服务同步的任何文件夹中删除 git 管理的存储库 .

问题是您将 Git 数据存储在由云同步服务管理的目录中。 Git 要求文件系统具有确定性行为,特别是 POSIX 设置的行为。但是,当您使用这种类型的云同步服务时,同步工具可以修改工作目录或 .git 目录的内容,但不会反映 Git 所做的事情。因此,您最终可能会遇到以下情况:锁定文件重新出现、操作失败(因为它们的锁定文件已被删除或正在使用)或重复文件等问题。

众所周知,使用云同步服务可能而且确实会导致数据丢失。 Git 以特定顺序将文件写入磁盘以保持存储库的一致性,如果它们未完全同步到另一台机器或在存储库发生更改时,存储库可能会损坏。 Git 维护者强烈建议不要这样做,Git FAQ 的未来版本将特别指示用户不要这样做。