git實戰12 – 無法將本地資料push到遠端


當在本地端下達

git push

git push origin master:master

得到以下錯誤訊息

Counting objects: 73, done.
Compressing objects: 100% (48/48), done.
Writing objects: 100% (48/48), 10.95 KiB, done.
Total 48 (delta 42), 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 git@192.168.1.100:/home/git/project/lytp/.git
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'git@192.168.1.100:/home/git/project/lytp/.git'

查看網路上討論的解法,在遠端目錄裡的 .git/config 加入

[receive]
    denyCurrentBranch = false

再執行一次

git push
git@192.168.1.100's password:
Counting objects: 73, done.
Compressing objects: 100% (48/48), done.
Writing objects: 100% (48/48), 10.95 KiB, done.
Total 48 (delta 42), reused 0 (delta 0)
To git@192.168.1.100:/home/git/project/lytp/.git
   c8254af..4e16be9  master -> master

這時可以看到可以正常 push 到遠端了

單字解釋:
receive 接受
deny Current Branch 否任當前分支
所以加入的參數是表示:不否任當前的分支

不過到遠端檢查卻發現檔案沒有上傳上去
只好使用另一個方法,在遠端建立一個純倉庫
先使用 root 身份刪除遠端的目錄
再使用 git 身份建立空的倉庫

git init --bare
Initialized empty Git repository in /home/git/project/lytp/

( 初始化空的Git資料庫在 /home/git/project/lytp/ )