git push: refs/heads/my/subbranch 存在,无法创建

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22630404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:55:25  来源:igfitidea点击:

git push: refs/heads/my/subbranch exists, cannot create

git

提问by Snyf

Is this not possible to create somme sub-sub-folder in a repo on a server?

这不可能在服务器上的 repo 中创建 somme 子文件夹吗?

if i do:

如果我做:

git push origin dev/master 

everything work find

一切工作找到

but if i do

但如果我这样做

git push origin dev/sub/master

i got this:

我懂了:

error: 'refs/heads/dev/sub' exists; cannot create 'refs/heads/dev/sub/master'

i checked with "git branch -r" and directly with ssh, there isn't dev/sub folder already created.

我使用“git branch -r”并直接使用 ssh 进行了检查,但尚未创建 dev/sub 文件夹。

what's wrong?

怎么了?

回答by torek

It's not a folderthat exists, it's a branch. (Well, there may be a folder/directory involved somewhere—or maybe not, as references get "packed" and stop existing as files within directories.)

它不是一个存在的文件夹,它是一个分支。(嗯,某处可能涉及文件夹/目录——或者可能没有,因为引用被“打包”并停止作为目录中的文件存在。)

  • If branch bexists, no branch named b/anythingcan be created.
  • Likewise, if branch dev/bexists, dev/b/ccannot be created.
  • 如果分支b存在,b/anything则不能创建命名的分支。
  • 同样,如果分支dev/b存在,dev/b/c则无法创建。

This is a git internal limitation. In this particular case, remote originhas a branch named dev/sub(regardless of whether you have it or not, the important thing is whether the remote has it). In order to create, on origin, a branch named dev/sub/master, you must first delete the branch named dev/subon origin:

这是 git 内部限制。在这种特殊情况下,远程origin有一个名为的分支dev/sub(不管你有没有,重要的是远程有没有)。为了在 on 上创建origin一个名为 的分支dev/sub/master,您必须首先删除名为dev/subon的分支origin

git push origin :dev/sub

(Of course, deleting this branch may delete something important over there, so be sure you know what you are doing. Generally, you might want to git fetch originfirst, capturing their dev/subas your origin/dev/sub. You can then make a local branch named dev/renamed-subpointing to the same commit, create dev/renamed-subon the remote, delete the remote dev/sub, and then create dev/sub/masteron the remote.)

(当然,删除此分支可能会删除那里的一些重要内容,因此请确保您知道自己在做什么。通常,您可能希望git fetch origin首先将其捕获dev/sub为您的origin/dev/sub. 。然后您可以创建一个名为dev/renamed-sub指向同一个提交的本地分支,dev/renamed-sub在遥控器上创建,删除遥控器dev/sub,然后dev/sub/master在遥控器上创建。)



If you can log in on the remote (the system that originis hosted on), you can go into the repository over there and simply rename the local dev/subbranch. (Based on comments below, I suspect that there's a broken auto-deploy script over there as well, which probably should be fixed to only deploy "deployable" branches, rather than everything that gets pushed. But I am just guessing here.)

如果您可以登录远程(origin托管的系统),您可以进入那里的存储库并简单地重命名本地dev/sub分支。(根据下面的评论,我怀疑那里也有一个损坏的自动部署脚本,可能应该修复它以仅部署“可部署”分支,而不是所有被推送的分支。但我只是在这里猜测。)

回答by meridius

I was in a state when I couldn't even fetch because my repo had info about non-existing remote branches I didn't even had checked out. I solved it by running combination (thanks @torek) of:

我处于一种什至无法获取的状态,因为我的回购有关于不存在的远程分支的信息,我什至没有签出。我通过运行以下组合(感谢@torek)解决了它:

  • git branch -rlist local copies of remote branches
  • git ls-remotelist remote branches
  • git fetch --prune originupdate local copies of remote branches (this actually didn't help me)
  • git remote prune originremove info about removed remote branches (this did)
  • git branch -r列出远程分支的本地副本
  • git ls-remote列出远程分支
  • git fetch --prune origin更新远程分支的本地副本(这实际上对我没有帮助
  • git remote prune origin删除有关已删除远程分支的信息(确实如此

回答by MEZIDI Hamza

For me -->

对我来说-->

Error =

错误 =

fatal: cannot lock ref 'refs/heads/release/wl/2.3': 'refs/heads/release/wl' 
exists; cannot create 'refs/heads/release/wl/2.3'

Solution =

解决方案 =

$~ git update-ref -d refs/heads/release/wl
$~ git checkout release/wl/2.3

回答by awreccan

The currently accepted answerdidn't help me because I didn't have a ref on the remote repo to delete - it was purely on my local! So if you're in that situation, here's what to do:

目前接受的答案没有帮助我,因为我没有在远程回购一个裁判删除-这纯粹是我的地方!因此,如果您处于这种情况,请执行以下操作:

This is the issue I was facing:

这是我面临的问题:

$ git fetch origin
error: cannot lock ref 'refs/remotes/origin/fix/sub-branch': 
'refs/remotes/origin/fix' exists; cannot create 
'refs/remotes/origin/fix/sub-branch'
From <repo URL>
 ! [new branch]      fix/sub-branch          -> origin/fix/sub-branch
 (unable to update local ref)

I tried the accepted answer's suggestion but got this:

我尝试了接受的答案的建议,但得到了这个:

$ git push origin :fix
error: unable to delete 'fix': remote ref does not exist
error: failed to push some refs to <repo URL>

So the ref didn't even exist on origin- it was clearly just hanging around somewhere on my local repo. So I ran $ git remote show me, which produced:

所以 ref 甚至不存在origin- 它显然只是在我本地仓库的某个地方闲逛。所以我跑了$ git remote show me,它产生了:

Remote branches:
...
refs/remotes/origin/fix             stale (use 'git remote prune' to remove)
...

Which then made the solution clear:

然后让解决方案变得清晰:

$ git remote prune origin
Pruning origin
URL: <redacted>
 * [pruned] origin/fix

With this, the problem disappeared:

这样,问题就消失了:

$ git fetch origin
remote: Counting objects: 5, done.
remote: Total 5 (delta 2), reused 2 (delta 2), pack-reused 3
Unpacking objects: 100% (5/5), done.
From <repo URL>
 * [new branch]      fix/sub-branch          -> origin/fix/sub-branch

回答by kenorb

Try this command to fix it:

试试这个命令来修复它:

git gc

to run a number of housekeeping tasks within the current repository and remove unreachable objects (by invoking git pruneand git fsck --unreachable).

在当前存储库中运行许多内务管理任务并删除无法访问的对象(通过调用git prunegit fsck --unreachable)。

Read more: git help gcand git help prune

阅读更多:git help gcgit help prune

回答by Tomer Bar-Shlomo

#!/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 '':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 '':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 '':p" >> ${script}
echo "execute ${script}"
${script}
git pull

echo push
log="git-push-errors.log"
script="./git-push-exist-tags-delete.sh"
git_command="git push"
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 '':p" >> ${script}
echo "execute ${script}"
${script}
git push

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:

上面的脚本将错误记录到 XXX-errors.log 并通过使用以下命令从 XXX-errors.log 自动生成和运行 XXX-exist-tags-delete.sh 来修复它们:

  1. git update-ref -d refs/tags
  2. git fetch
  3. git pull
  4. git push
  1. git update-ref -d 参考/标签
  2. 混帐

回答by alexkb

As a windows user, none of the solutions thus far solved the problem for me. The reason I was seeing this error was because (using the OP's branch names) I was trying to create a branch dev/subbut someone else had created a branch called Devand as we all know, windows has a case insensitive file system.

作为 Windows 用户,到目前为止,没有一个解决方案为我解决了这个问题。我看到此错误的原因是(使用 OP 的分支名称)我试图创建一个分支,dev/sub但其他人创建了一个名为的分支Dev,众所周知,Windows 有一个不区分大小写的文件系统。

So when windows tried to pull down dev/subit was first trying to create the folder dev, but it couldn't because Devalready existed.

因此,当 Windows 尝试下拉时,dev/sub它首先尝试创建文件夹dev,但无法创建,因为Dev已经存在。

The solution was to delete the Devbranch locally and remotely with git branch -d Dev && git push origin :Dev. A git pullafter this ran fine.

解决方案是Dev使用git branch -d Dev && git push origin :Dev. Agit pull在这之后运行良好。

Another lesson going forward, branch names should always be lowercase to avoid this kind of gotcha's.

另一个教训是,分支名称应该总是小写,以避免这种问题。

回答by Juha Untinen

If all else fails, check that your repo system does not have limitations for branch names. In my case, you could only create branches that start with SD-<number>. Any other naming would give you just a generic:

如果所有其他方法都失败了,请检查您的回购系统是否对分支名称没有限制。就我而言,您只能创建以SD-<number>. 任何其他命名只会给你一个通用的:

remote: error: cannot lock ref 'refs/heads/mybranch': 'refs/heads/mybranch/environment-variables' exists; cannot create 'refs/heads/mybranch'
To git.example.com:project/repository.git
 ! [remote rejected] mybranch -> mybranch (failed to update ref)
error: failed to push some refs to '[email protected]:project/repository.git'

回答by Sanjay Zalke

I understand this is already answered, but it did not worked for me. I did not cared for local changes as it was already pushed up but having issues pulling back. In my case, we changed halfway between having "hotfix" as branch to folder system having parent folder as "hotfix".

我知道这已经得到了回答,但它对我没有用。我并不关心本地的变化,因为它已经被推高了,但在回滚时遇到了问题。就我而言,我们在将“修补程序”作为分支到将父文件夹作为“修补程序”的文件夹系统之间进行了中途更改。

-- hotfix ----hotfix/1234_bug ----hotfix/3456_bug

-- hotfix ----hotfix/1234_bug ----hotfix/3456_bug

So I was getting following error:

所以我收到以下错误:

Fetching from origin Error: cannot lock ref 'refs/remotes/origin/hotfix/1234_bug': 'refs/remotes/origin/hotfix' exists; cannot create 'refs/remotes/origin/hotfix'

After searching for similar errors finally I found solution on a discussion thread here.

最终搜索类似错误后,我在此处的讨论线程上找到了解决方案。

git remote prune origin

回答by Andrea Gherardi

git remote prune originfixed the issue for me

git remote prune origin为我解决了这个问题