错误: 无法锁定 ref. . ‘ refs/tag’存在; 无法创建‘ refs/tag/

当试图从 github 中提取更改时,我得到了一个奇怪的“ can not lock ref”错误。我尝试过 git gc,并四处寻找类似的错误,但是找不到解决方案。

> git pull
error: cannot lock ref 'refs/tags/v2.8': 'refs/tags' exists; cannot create 'refs/tags/v2.8'
From github.com:k3it/qsorder
! [new tag]         v2.8       -> v2.8  (unable to update local ref)
error: cannot lock ref 'refs/tags/v2.9': 'refs/tags' exists; cannot create 'refs/tags/v2.9'
! [new tag]         v2.9       -> v2.9  (unable to update local ref)
122528 次浏览

error: cannot lock ref 'refs/tags/v2.8': 'refs/tags' exists; cannot create 'refs/tags/v2.8' From github.com:k3it/qsorder

Try deleting your local tag v2.8 and v2.9 then pull again.

$ git tag -d v2.8
$ git tag -d v2.9


$ git pull

If you want to delete all local tags by a command:

$ git tag | xargs git tag -d

Your Git is complaining that a reference (rather than a directory) named refs/tags exists. It's not clear what would create that, but see if git rev-parse refs/tags produces a hash ID. If so, that reference needs to go away:

git update-ref -d refs/tags

after which git fetch should work.

If git rev-parse refs/tags fails (which it should—refs/tags itself should not be a valid name) then this is not the problem and it's not clear what the actual problem is.

For a quick work around you can use

git push --delete origin 'v2.8'

git push --delete origin 'v2.9'

#!/usr/bin/env bash
echo "update-ref delete refs/tags"
log="git-update-ref-errors.log"
script="./git-update-ref-exist-tags-delete.sh"
git_command="git update-ref -d refs/tags"


echo "log errors from ${git_command} to ${log}"
${git_command} 2>&1 | > ${log}
echo "show errors to ${log}"
cat ${log}
echo create ${script}
touch ${script}
echo "add execute (+x) permissions to ${script}"
chmod +x ${script}
echo "generate ${script} from errors log ${log}"
${git_command} 2>&1 | grep 'exists' | sed -n "s:.*\: 'refs/tags/\(.*\)' exists;.*:git tag -d '\1':p" >> ${script}
echo "execute ${script}"
${script}


echo fetch
log="git-fetch-errors.log"
script="./git-fetch-exist-tags-delete.sh"
git_command="git fetch"
echo "log errors from ${git_command} to ${log}"
${git_command} 2>&1 | > ${log}
echo "show errors from ${log}"
cat ${log}
echo create ${script}
touch ${script}
echo "add execute (+x) permissions to ${script}"
chmod +x ${script}
echo "generate ${script} from errors log ${log}"
${git_command} 2>&1 | grep 'exists' | sed -n "s:.*\: 'refs/tags/\(.*\)' exists;.*:git tag -d '\1':p" >> ${script}
echo "execute ${script}"
${script}
git fetch


echo pull
log="git-pull-errors.log"
script="./git-pull-exist-tags-delete.sh"
git_command="git pull"
echo "log errors from ${git_command} to ${log}"
${git_command} 2>&1 | > ${log}
echo "show errors from ${log}"
cat ${log}
echo create ${script}
touch ${script}
echo "add execute (+x) permissions to ${script}"
chmod +x ${script}
echo "generate ${script} from errors log ${log}"
${git_command} 2>&1 | grep 'exists' | sed -n "s:.*\: 'refs/tags/\(.*\)' exists;.*:git tag -d '\1':p" >> ${script}
echo "execute ${script}"
${script}
git pull

The script above will log errors to XXX-errors.log and fix them by generating and running a XXX-exist-tags-delete.sh automatically from the XXX-errors.log using the following commands:

  1. git update-ref -d refs/tags
  2. git fetch
  3. git pull

Running

git remote prune origin

Worked for me. Not sure why this was the issue, but seems like there was a broken reference to a remote branch.

This is what I tried and it worked for me.

git remote prune origin

I was trying to push (/dev/somechanges) and I have a remote branch (/dev) with the same prefix when I choose a new name that is not starting with /dev it worked fine.

In my case, the following helped:

git fetch --prune origin
git pull

First Solution ==>:let's say you have branch called develop and you're trying to create new branch called develop/updatefeature this will cause this error, that was my case so If you remove the develop word from the new branch (updatefeature)I think It should be solved the issue.

Second Solution ==>: use the below command in bash

git remote prune origin

git gc will solve it!

Cleanup unnecessary files and optimize the local repository
Link

This worked for me after all other answers didn't

git update-ref --no-deref -d refs/remotes/origin/branch_name