2021/03/16

[git] add remote origin with ssh

 我先講一下我的實驗:

我有個 repository 它的源是國外的,這個我們不管,位置在
~/q8vista/q8server

然後我在別台機器的 /tmp/xx 下做實驗:
cd /tmp/xx/ &&
git clone wade@192.168.20.110:/home/wade/q8vista/q8server
因為不想破壞源庫,所以...
cd /tmp/xx/ && mkdir q8vista && cd q8vista
此時 /tmp/xx/q8server 它的源是192.168.20.110:/home/wade/q8vista/q8server , 
然後再做一次
git clone ssh://wade@192.168.20.110:/tmp/xx/q8server
上面的命令方式跟底下一樣的意思:
git clone wade@192.168.20.110:/tmp/xx/q8server
接下來 cd /tmp/xx/q8vista/q8server, 它的源是 192.168.20.110:/tmp/xx/q8server
cd /tmp/xx/q8vista/q8server 並做點變動之後
git commit -a -m 'test for ssh push' && git push
此時會發生錯誤:

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: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set the 'receive.denyCurrentBranch' configuration variable
remote: to 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.

git 的好處是,它的訊息通常會告訴你接下來該怎麼做
我就下底下的命令:
git config --global receive.denyCurrentBranch ignore
或在 ~/.gitconfig 可以找到底下這2行:
[receive]
        denyCurrentBranch = ignore

再進行一次 git push 就通過了
要注意的是,遠端的 git tree 被更新了沒錯,
但是如果在遠端執行 git status 會發現遠端該目錄下的 local files 與 git repository 並不一致,一般的做法是 git reset --hard HEAD

0 意見: