git push时出现错误refusing to update checked out branch: refs/heads/master

来源:赵克立博客 分类: 其它 标签:Git发布时间:2016-11-21 13:57:03最后更新:2016-12-08 12:21:42浏览:9844
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2016-12-08 12:21:42
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章

错误描述

使用tortoisegit push的时候如下错误

blob.png

Writing objects: 100% (3/3), 258 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 0 (delta 0)

remote: error: refusing to update checked out branch: refs/heads/master

remote: error: By default, updating the current branch in a non-bare repository

remote: error: is denied, because it will make the index and work tree inconsistent

remote: error: with what you pushed, and will require 'git reset --hard' to match

remote: error: the work tree to HEAD.

remote: error:

remote: error: You can set 'receive.denyCurrentBranch' configuration variable to

remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into

remote: error: its current branch; however, this is not recommended unless you

remote: error: arranged to update its work tree to match what you pushed in some

remote: error: other way.

remote: error:

remote: error: To squelch this message and still keep the default behaviour, set

remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.

To ssh://gitadmin@122.114.88.133:22/ICW/home/gitadmin/testgit

! [remote rejected] master -> master (branch is currently checked out)

error: failed to push some refs to 'ssh://gitadmin@122.114.88.133:22/ICW/home/gitadmin/testgit'



git did not exit cleanly (exit code 1) (9033 ms @ 2016/11/21 13:48:04)


上面原因是因为远程仓库创建的时候没有使用—bare选项

第一种方法:

解决这个冲突需要将远程仓库当前分支切换到其他分支(如谁也不会使用的分支),

第二种方法:

这是由于git默认拒绝了push操作,需要进行设置,修改.git/config添加如下代码:

[receive]
denyCurrentBranch = ignore

或者按照提示内容,设置denyCurrentBranch = ignore

在服务器上运行

git config receive.denyCurrentBranch ignore



上面设置后你可以push上去但是在服务器上目录里看不到push进来的文件

错误解析:

在初始化远程仓库时最好使用 git--bare init   而不要使用:gitinit

如果使用了gitinit初始化,则远程仓库的目录下,也包含worktree,当本地仓库向远程仓库push时, 

如果远程仓库正在push的分支上(如果当时不在push的分支,就没有问题), 那么push后的结果不会反应在worktree上,  也即在远程仓库的目录下对应的文件还是之前的内容,必须得使用git reset--hard才能看到push后的内容.

下面记录下创建仓库时的区别


git init  和 git init –bare 的区别


使用命令"git init --bare"(bare汉语意思是:裸,裸的)初始化的版本库(暂且称为bare repository)只会生成一类文件:用于记录版本库历史记录的.git目录下面的文件;而不会包含实际项目源文件的拷贝;所以该版本库不能称为工作目录(working tree);

如果你进入版本目录,就会发现只有.git目录下的文件,而没有其它文件;就是说,这个版本库里面的文件都是.git目录下面的文件,把原本在.git目录里面的文件放在版本库的根目录下面;

换句话说,不使用--bare选项时,就会生成.git目录以及其下的版本历史记录文件,这些版本历史记录文件就存放在.git目录下;

而使用--bare选项时,不再生成.git目录,而是只生成.git目录下面的版本历史记录文件,这些版本历史记录文件也不再存放在.git目录下面,而是直接存放在版本库的根目录下面


用"git init"初始化的版本库用户也可以在该目录下执行所有git方面的操作。但别的用户在将更新push上来的时候容易出现冲突。


比如有用户在该目录(就称为远端仓库)下执行git操作,且有两个分支(master 和 b1),当前在master分支下。另一个用户想把自己在本地仓库(就称为本地仓库)的master分支的更新提交到远端仓库的master分支,他就想当然的敲了

git push origin master:master

于是乎出现

因为远端仓库的用户正在master的分支上操作,而你又要把更新提交到这个master分支上,当然就出错了。

但如果是往远端仓库中空闲的分支上提交还是可以的,比如

git push origin master:b1

还是可以成功的

解决办法就是使用”git init –bare”方法创建一个所谓的裸仓库,之所以叫裸仓库是因为这个仓库只保存git历史提交的版本信息,而不允许用户在上面进行各种git操作,如果你硬要操作的话,只会得到下面的错误(”This operation must be run in a work tree”)

这个就是最好把远端仓库初始化成bare仓库的原因。



微信号:kelicom QQ群:215861553 紧急求助须知
Win32/PHP/JS/Android/Python