使用 capistrano 在服务器上部署时如何修复 "cannot lock ref" 错误?

How to fix "cannot lock ref" error while deploying on server using capistrano?

我试图将修补程序部署到我的登台服务器 (DigitalOcean),但出现 "cannot lock ref" 错误。我之前偶然发现了这个错误。我有一个分支 'hotfix' 然后我创建了 'hotfix/some-hotfix' 分支,当我试图将它推到原点时我得到了错误。我通过删除存储库 (BitBucket) 中的分支 'hotfix' 来修复它。但是现在我在临时服务器上部署时得到了它。

堆栈跟踪:

SSHKit::Command::Failed: git exit status: 1
git stdout: Fetching origin
error: cannot lock ref 'refs/heads/hotfix/my-hotfix-branch-name': 'refs/heads/hotfix' exists; cannot create 'refs/heads/hotfix/my-hotfix-branch-name'
From bitbucket.org:username/repo-name
! [new branch] hotfix/my-hotfix-branch-name -> hotfix/my-hotfix-branch-name (unable to update local ref)
error: some local refs could not be updated; try running
'git remote prune origin' to remove any old, conflicting branches
error: Could not fetch origin
git stderr: Nothing written

我尝试了 运行 git remote prune origin 但没有用。 我在这里没有想法

虽然分支名称 大部分 只是字符串——所以 hotfixhotfix/my-hotfix-branch-name 只是显而易见的,即两个不同的名称——部分 Git 坚持(出于好的和坏的原因)将它们视为目录和文件名组件。

(注意:如果您使用的是 Windows,您可以在此处用 \ 在心里替换 /。或者,只需意识到 a/b 在Windows 为 a\b,几乎适用于所有目的。)

如果您有一个名为 README 文件,您的 OS 将不允许您创建另一个名为 README/TOO 的文件。也就是说,实体槽 README 被 "file entity" 占用。您必须先删除或重命名 README,然后创建名为 README 目录 (或文件夹,如果您喜欢该术语)。在此目录(或文件夹)中,您现在可以创建更多文件,或将原始 README 文件移动到该目录中。

Git 对分支名称和远程跟踪名称施加 完全相同的限制。如果存在名为 hotfix 的现有分支,则无法创建名为 hotfix/my-hotfix-branch-name 的分支。您通过首先删除 hotfix 您的 存储库中修复了此问题。您必须在 您打算使用的所有其他 Git 存储库中执行相同的操作: 如果它有 hotfix,请将其删除。然后你可以让另一个 Git 存储库创建 hotfix/my-hotfix-branch-name.

看起来您的登台服务器中有一个 hotfix 分支。您可能必须直接登录登台服务器,或者您可以使用 git push --delete 向其发送删除请求。无论哪种方式,您都必须获得 that Git 才能删除该分支名称。在您自己的存储库上完成的操作没有帮助,因为每个存储库都是独立的。

当问题出现在本地存储库中,但由于 origin/dir/file、运行、git fetch -p origingit remote prune origin 等远程跟踪名称应该可以解决问题。 fetch-p 选项——可以拼写为 --prune——和 git remoteprune 子命令告诉你的 Git 到 删除远程跟踪名称,例如origin/dir,因为服务器有(这里强调过去时)一个名为dir的分支,但从那以后,有人删除了那个分支。您的远程跟踪名称是您 Git 记住上次与他们交谈时他们的分支机构的方式。所以他们 一个 dir 分支,但他们现在没有了;现在他们有一个 dir/file 分支。您必须先让 Git 删除 origin/dir,然后 Git 才能创建 origin/dir/file 远程跟踪名称。

我通过这样做解决了这个问题:

  1. ssh 到您的服务器。
  2. 找到 /repo/ 文件夹(我在部署的应用程序文件夹 ~/apps/app-name/repo 中找到了我的文件夹)并将 cd 放入其中。
  3. 运行 git remote prune origin.

完成这些步骤后,我就可以部署到我的服务器了。

我在执行 git pulll

时遇到了类似的问题
> $ git pull 
> error: cannot lock ref
> 'refs/remotes/origin/release/thing_15': 'refs/remotes/origin/release'
> exists; cannot create 'refs/remotes/origin/release/thing_15' From
> https://git.mycompany.com/projects/myproject

git fetch 使用 -p 选项修复了它(它存在于 git 的较新版本中),请在此处查看文档 https://git-scm.com/docs/fetch-options/2.20.0#fetch-options--p

git fetch -p