git提供了丰富的协议支持,包括:SSH.GIT.HTTP.HTTPS.FTP.FTPS.RSYNC及前面已经看到的本地协议等。
Git支持的协议 版本库间数据交换需要使用协议,协议分2种:
1)智能协议:在使用智能协议时,会在会话的2个版本库的各自一段打开相应的程序进行数据交换,在数据传输过程中会有清晰的进度显示,而且因为是按需传输所以传输量更小速度更快。SSH、GIT、本地协议、特殊配置的HTTP是智能协议。
2)哑协议:使用哑协议时,远程版本库不会允许辅助衬线,完全靠客户端去主动发现,需要访问refs获取引用列表,根据引用再去访问objects下的对象文件,如果对象文件被打包了还要访问packs以获取打包文件列表,在读取完整的打包文件获取对象。整个过程效率非常低,没有进度条。FTP、RSYNC、没有特殊配置的HTTP是哑协议。
本地版本库的最新提交及其历史提交git rev-list HEAD(从HEAD开始往上查询)命令
显示远程版本库引用对应的哈希值 gitremote;git ls-remote origin
当协同工作时候可能会出现这个
To ssh://xwx496887@10.148.192.216:29418/ST/mytest
! [rejected] HEAD -> master (non-fast-forward)
error: failed to push some refs to 'ssh://xwx496887@10.148.192.216:29418/ST/mytest'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
1.git push -f:强制推送,推送的提交会覆盖刷新版本库中的版本 不建议使用,可能会覆盖别人
示例:
jagent@ubuntu:~/xin$ git clone ssh://xwx496887@10.148.192.216:29418/ST/mytest user1
jagent@ubuntu:~/xin$ git clone ssh://xwx496887@10.148.192.216:29418/ST/mytest user2
jagent@ubuntu:~/xin/user1$ git config user.name user1
jagent@ubuntu:~/xin/user1$ git config user.email user1@122.com
jagent@ubuntu:~/xin/user2$ git config user.name user2
jagent@ubuntu:~/xin/user2$ git config user.email user2@122.com
jagent@ubuntu:~/xin/user1$ echo "this is user1" >> 1.txt
jagent@ubuntu:~/xin/user1$ git add -A;git commit -m "user1 commit"
[master 6c47fe5] user1 commit
1 file changed, 1 insertion(+)
jagent@ubuntu:~/xin/user1$ git push origin master
Counting objects: 7, done.
Delta compression using up to 48 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 267 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1)
remote: Processing changes: done
To ssh://xwx496887@10.148.192.216:29418/ST/mytest
b978762..6c47fe5 master -> master
jagent@ubuntu:~/xin/user2$ echo "this is a user2" >> 2.txt
jagent@ubuntu:~/xin/user2$ git add -A;git commit -m "user2 commit "
[master 76b6064] user2 commit
1 file changed, 1 insertion(+)
jagent@ubuntu:~/xin/user2$ git push origin master
To ssh://xwx496887@10.148.192.216:29418/ST/mytest
! [rejected] master -> master (fetch first)或者(non-fast-forward)
error: failed to push some refs to 'ssh://xwx496887@10.148.192.216:29418/ST/mytest'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
本地版本库的最新提交及其历史提交git rev-list HEAD(从HEAD开始往上查询)命令
显示远程版本库引用对应的哈希值 gitremote;git ls-remote origin
查看远程最新的HEAD和本地是否一致,如果不一致,代表此仓库有人合入
jagent@ubuntu:~/xin/user2$ git rev-list HEAD
76b60641a9f0552f3cbe1c1d258adf75edcd4f92
b9787623a6a2ff74d6827c29b500dbc9efa7dd07
139c2c7b53a818d79cffcdf43edacd85e18d2be1
ab8ca0a46d053a35f66e479ef818dd39de8a5bc4
jagent@ubuntu:~/xin/user2$ git ls-remote origin
6c47fe53a6684d96cd11b3798cf93aaed6097f30 HEAD
6c47fe53a6684d96cd11b3798cf93aaed6097f30 refs/heads/master
a643f74e8152a98c5ac33eb4520c226bbb583972 refs/meta/config
jagent@ubuntu:~/xin/user2$ git pull
remote: Counting objects: 5, done
remote: Finding sources: 100% (3/3)
remote: Total 3 (delta 1), reused 3 (delta 1)
Unpacking objects: 100% (3/3), done.
From ssh://10.148.192.216:29418/ST/mytest
b978762..6c47fe5 master -> origin/master
Merge made by the 'recursive' strategy.
1.txt | 1 +
1 file changed, 1 insertion(+)
jagent@ubuntu:~/xin/user2$ git rev-list HEAD
fdf43483f66580d57a4c5af710936d686efe2144
76b60641a9f0552f3cbe1c1d258adf75edcd4f92
6c47fe53a6684d96cd11b3798cf93aaed6097f30
b9787623a6a2ff74d6827c29b500dbc9efa7dd07
139c2c7b53a818d79cffcdf43edacd85e18d2be1
ab8ca0a46d053a35f66e479ef818dd39de8a5bc4
jagent@ubuntu:~/xin/user2$ git ls-remote origin
6c47fe53a6684d96cd11b3798cf93aaed6097f30 HEAD
6c47fe53a6684d96cd11b3798cf93aaed6097f30 refs/heads/master
a643f74e8152a98c5ac33eb4520c226bbb583972 refs/meta/config
PS:如果本地commit了git rev-list HEAD^ 和 git ls-remote origin
1.git push -f:强制推送,推送的提交会覆盖刷新版本库中的版本 不建议使用,可能会覆盖别人
2.先git pull拉取最新的代码,合并之后,再push,可能会出现冲突