clone
root@ve:/home/nanyt# git clone ssh://root@192.168.13.2/home/gittestCloning into 'gittest'...root@192.168.13.2's password: remote: Counting objects: 6, done.remote: Compressing objects: 100% (2/2), done.remote: Total 6 (delta 0), reused 0 (delta 0)Receiving objects: 100% (6/6), done.Checking connectivity... done.
与远端仓库保持一致
root@ve:/home/nanyt/gittest# ls -altotal 16drwxr-xr-x 3 root root 4096 Oct 9 19:51 .drwxr-xr-x 6 root root 4096 Oct 9 19:51 ..drwxr-xr-x 8 root root 4096 Oct 9 19:51 .git-rw-r--r-- 1 root root 22 Oct 9 19:51 READMEroot@ve:/home/nanyt/gittest# cat README init filemodify fileroot@ve:/home/nanyt/gittest# git logcommit 5fa67a97a2b1a813036950155ef56cfdf0037de9Author: Leon NanDate: Mon Oct 10 03:45:24 2016 +0800 modify filecommit 0f3e0e50082bab03bd36188c1f41b11133454e91Author: Leon Nan Date: Mon Oct 10 03:45:01 2016 +0800 init file
在本地修改文件前需要修改本地的用户名和邮箱
root@ve:/home/nanyt/gittest# git config user.name "Leon Nan Remote"root@ve:/home/nanyt/gittest# git config user.email leon0820@126.com
修改文件
root@ve:/home/nanyt/gittest# echo "modify file remote" >> READMEroot@ve:/home/nanyt/gittest# git statusOn branch masterYour branch is up-to-date with 'origin/master'.Changes not staged for commit: (use "git add..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: READMEno changes added to commit (use "git add" and/or "git commit -a")
增加文件并交付
root@ve:/home/nanyt/gittest# git add -Aroot@ve:/home/nanyt/gittest# git commit -m "modify file remote"[master 82783ae] modify file remote 1 file changed, 1 insertion(+)
push到远端
root@ve:/home/nanyt/gittest# git push origin master root@192.168.13.2's password: Counting objects: 5, done.Writing objects: 100% (3/3), 269 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)remote: error: refusing to update checked out branch: refs/heads/masterremote: error: By default, updating the current branch in a non-bare repositoryremote: error: is denied, because it will make the index and work tree inconsistentremote: error: with what you pushed, and will require 'git reset --hard' to matchremote: error: the work tree to HEAD.remote: error: remote: error: You can set 'receive.denyCurrentBranch' configuration variable toremote: error: 'ignore' or 'warn' in the remote repository to allow pushing intoremote: error: its current branch; however, this is not recommended unless youremote: error: arranged to update its work tree to match what you pushed in someremote: error: other way.remote: error: remote: error: To squelch this message and still keep the default behaviour, setremote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.To ssh://root@192.168.13.2/home/gittest ! [remote rejected] master -> master (branch is currently checked out)error: failed to push some refs to 'ssh://root@192.168.13.2/home/gittest'
返回失败,原因是远端仓库不是bare的,也就是说远程repo有工作区,且已经被checkout某个branch,这种情况下git不允许你push,因为如果你要push的代码跟远程代码有conflict,在你这边是没有办法resolve的。
解决方案1:修改远端git接受设置后再次push
[root@localhost gittest]# git config receive.denyCurrentBranch ignore
root@ve:/home/nanyt/gittest# git push origin master root@192.168.13.2's password: Counting objects: 5, done.Writing objects: 100% (3/3), 269 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To ssh://root@192.168.13.2/home/gittest 5fa67a9..82783ae master -> masterroot@ve:/home/nanyt/gittest#
解决方案2:将远端仓库变成bare的
[root@localhost gittest]# git init --bareInitialized empty Git repository in /home/gittest/
root@ve:/home/nanyt/gittest# git push origin master root@192.168.13.2's password: Counting objects: 5, done.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 281 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)remote: warning: updating the current branchTo ssh://root@192.168.13.2/home/gittest 82783ae..402c001 master -> master
参考文档: