Git 推送错误 '[remote rejected] master -> master (branch is currently checked out)'

标签 git git-push

昨天,我发布了一个关于如何克隆 Git 的问题从我的一台机器到另一台机器的存储库, How can I 'git clone' from another machine? .

我现在能够成功地将 Git 存储库从源 (192.168.1.2) 克隆到目标 (192.168.1.1)。

但是当我编辑一个文件时,一个 git commit -a -m "test" 和一个 git push,我在我的目的地得到了这个错误( 192.168.1.1):

git push                                                
hap@192.168.1.2's password: 
Counting objects: 21, done.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (11/11), 1010 bytes, done.
Total 11 (delta 9), reused 0 (delta 0)
error: refusing to update checked out branch: refs/heads/master
error: By default, updating the current branch in a non-bare repository
error: is denied, because it will make the index and work tree inconsistent
error: with what you pushed, and will require 'git reset --hard' to match
error: the work tree to HEAD.
error: 
error: You can set 'receive.denyCurrentBranch' configuration variable to
error: 'ignore' or 'warn' in the remote repository to allow pushing into
error: its current branch; however, this is not recommended unless you
error: arranged to update its work tree to match what you pushed in some
error: other way.
error: 
error: To squelch this message and still keep the default behaviour, set
error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To git+ssh://hap@192.168.1.2/media/LINUXDATA/working
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git+ssh://hap@192.168.1.2/media/LINUXDATA/working'

我正在使用两个不同版本的 Git(远程计算机上的 1.7 和本地计算机上的 1.5)。这是一个可能的原因吗?

最佳答案

错误消息 error: refusing to update checked out branch: refs/heads/master 由远程存储库发出,这意味着您正在尝试将代码推送到远程非裸存储库当前在工作目录中 check out 了不同的代码。避免此问题的最佳方法是仅推送到裸存储库 - 裸存储库永远不会发生此问题。

您可以简单地将远程存储库转换为裸存储库(裸存储库中没有工作副本 - 该文件夹仅包含实际存储库数据)。

在您的远程存储库文件夹中执行以下命令:

git config --bool core.bare true

然后删除该文件夹中除.git 之外的所有文件。然后你就可以执行 git push 到远程存储库而不会出现任何错误。

关于Git 推送错误 '[remote rejected] master -> master (branch is currently checked out)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44274958/

相关文章:

ios - git 差异 : How to ignore starting space of an empty line?

git - git 似乎无法使用 OSX SSH key

git - 了解 Mercurial、Bazaar 等的内部结构?

Git:管理推送事件的文件

git - 无法推送到远程 Git 存储库 - 一切都是最新的

git - `git push` 是否用作 `git push origin newfeature` 的简写?

git - 如何通过ubuntu更新github中的文件

Git - 以编程方式阻止提交或推送的能力

Git 错误 : cant execute git log && git diff [No such file or directory]

git - 如何删除链接存储库中的 git 远程标签?