git push时出现错误refusing to update checked out branch: refs/heads/master
錯誤描述
使用tortoisegit push的時候如下錯誤
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添加如下代碼:
或者按照提示內容,設置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分支,他就想當然的敲了
于是乎出現
因為遠端倉庫的用戶正在master的分支上操作,而你又要把更新提交到這個master分支上,當然就出錯了。
但如果是往遠端倉庫中空閑的分支上提交還是可以的,比如
還是可以成功的
解決辦法就是使用”git init –bare”方法創建一個所謂的裸倉庫,之所以叫裸倉庫是因為這個倉庫只保存git歷史提交的版本信息,而不允許用戶在上面進行各種git操作,如果你硬要操作的話,只會得到下面的錯誤(”This operation must be run in a work tree”)
這個就是最好把遠端倉庫初始化成bare倉庫的原因。
微信號:kelicom QQ交流群:215861553
來源:https://www.zhaokeli.com/article/8023.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的git push时出现错误refusing to update checked out branch: refs/heads/master的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git 服务器+git linux客户端
- 下一篇: 含硫三氯甲烷基杀虫剂有哪些