推送到远程私有 Git 仓库提示 checked out

推送到远程私有 Git 仓库提示 checked out

在远程主机上建立了私有 Git 仓库。

在本地执行 git push 向远程私有 Git 仓库推送代码时,Git 提示 ! [remote rejected] master -> master (branch is currently checked out),无法推送。

(根据分支名称不同,也可能是 ! [remote rejected] main -> main (branch is currently checked out))。

这个问题如何解决?

问了 ChatGPT,它回答:

当你在非裸仓库(即带有工作区的仓库)直接 git push 到当前分支时,可能会导致工作区文件与提交不一致。Git 通过 receive.denyCurrentBranch 来防止或处理这种情况。

receive.denyCurrentBranch 设为 updateInstead,就允许来自外部的 git push,并自动更新工作区。

具体的解决方案如下:

1
2
3
4
5
6
7
8
# 删除并重建仓库
rm -rf repo
mkdir repo
# 进入目录并初始化
cd repo
git init
# 设置 receive.denyCurrentBranch
git config --local receive.denyCurrentBranch "updateInstead"

搞定!


图片版权

题图:https://www.deviantart.com/black-pixel/art/Git-Wallpaper-Clean-357130508

头图:Image by Pexels from Pixabay


求扫码打赏
“我这么可爱,请给我钱 o(*^ω^*)o”

推送到远程私有 Git 仓库提示 checked out
https://blog.kukmoon.com/a3e006830e2b/
作者
Kukmoon谷月
发布于
2025年2月1日
许可协议