Git 版本控制器使用指南-II(远程代码控制)

来源:互联网 发布:pxe网络装机 编辑:程序博客网 时间:2024/06/05 19:24

这篇主要讲的是FreeBSD上部署的Git中,远程的代码仓储的使用,下面是Git远程的测试,服务器和客户机还是同一台虚拟机,使用另外创建的git用户测试!

[git@zfsworkstation ~/gitwork]$ git config --global user.name "Git.TAO"[git@zfsworkstation ~/gitwork]$ git config --global user.email "taoscu@gmail.com"[git@zfsworkstation ~/gitwork]$ git config --listcore.editor=emacsuser.name=Git.TAOuser.email=taoscu@gmail.com

1.git clone 可以远程克隆一个库,并且系统默认其为origin库

[git@zfsworkstation ~/gitwork]$ ls -al /home/taozj/source_code/git/repo/helloworld/.git/       .gitignore  Makefile    README.txt  hello.cpp   hello.exe[git@zfsworkstation ~/gitwork]$ ls /home/taozj/source_code/git/repo/helloworld/Makefile        README.txt      hello.cpp       hello.exe[git@zfsworkstation ~/gitwork]$ git clone ssh://git@zfsworkstation.peachzone.org/home/taozj/source_code/git/repo/helloworldCloning into helloworld...Password:remote: Counting objects: 28, done.remote: Compressing objects: 100% (22/22), done.remote: Total 28 (delta 7), reused 0 (delta 0)Receiving objects: 100% (28/28), done.Resolving deltas: 100% (7/7), done.[git@zfsworkstation ~/gitwork]$ ls helloworld/Makefile        README.txt      hello.cpp[git@zfsworkstation ~/gitwork]$ ls -a helloworld/.               ..              .git            .gitignore      Makefile        README.txt      hello.cpp

这里将服务器的代码同步下来了,同步的时候需要输入密码。
这里需要注意的是,我们的hello.exe被写入.gitignore忽略跟踪了,所以git同步下来的代码中没有这个文件,还有就是这个clone是使用ssh远程克隆的,所以权限可以的话,就可以提交代码到远程仓库上去,因为ssh是一个可以读写的协议 ;而如果使用http或者git协议,直接 git clone git://— ,就是一个只读的拷贝,无法提交代码的

2.显示远程仓库的信息

[git@zfsworkstation ~/gitwork]$ cd helloworld/[git@zfsworkstation ~/gitwork/helloworld]$ git remote -vorigin  ssh://git@zfsworkstation.peachzone.org/home/taozj/source_code/git/repo/helloworld (fetch)origin  ssh://git@zfsworkstation.peachzone.org/home/taozj/source_code/git/repo/helloworld (push)[git@zfsworkstation ~/gitwork/helloworld]$ git remote show originPassword:* remote origin  Fetch URL: ssh://git@zfsworkstation.peachzone.org/home/taozj/source_code/git/repo/helloworld  Push  URL: ssh://git@zfsworkstation.peachzone.org/home/taozj/source_code/git/repo/helloworld  HEAD branch: master  Remote branch:    master tracked  Local branch configured for 'git pull':    master merges with remote master  Local ref configured for 'git push':    master pushes to master (up to date)

3.跟新同步到远程仓库分支
我们在远程仓库进行了更改,添加了test.txt文件,然后这里 git fetch origin 可以查看到,远程的库已经发生了改变

[git@zfsworkstation ~/gitwork/helloworld]$ git fetch originPassword:remote: Counting objects: 4, done.remote: Compressing objects: 100% (2/2), done.remote: Total 3 (delta 1), reused 0 (delta 0)Unpacking objects: 100% (3/3), done.From ssh://zfsworkstation.peachzone.org/home/taozj/source_code/git/repo/helloworld   09dccf0..c55af4b  master     -> origin/master[git@zfsworkstation ~/gitwork/helloworld]$ git remote show originPassword:* remote origin  Fetch URL: ssh://git@zfsworkstation.peachzone.org/home/taozj/source_code/git/repo/helloworld  Push  URL: ssh://git@zfsworkstation.peachzone.org/home/taozj/source_code/git/repo/helloworld  HEAD branch: master  Remote branch:    master tracked  Local branch configured for 'git pull':    master merges with remote master  Local ref configured for 'git push':    master pushes to master (local out of date)

原来的修改并没有被更新下来,使用merge将远程的修改和本地的原始版本进行合并,就实现了代码的实质更新了 git merge (远程仓储名)/(分支名称)

[git@zfsworkstation ~/gitwork/helloworld]$ git merge origin/masterUpdating 09dccf0..c55af4bFast-forward 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.txt[git@zfsworkstation ~/gitwork/helloworld]$ lsMakefile        README.txt      hello.cpp       test.txt

实际上pull就是 fetch 和 merge的组合,所以上面可以简化成 git pull (远程仓储名) (分支名称)

[git@zfsworkstation ~/gitwork/helloworld]$ git pull origin masterPassword:From ssh://zfsworkstation.peachzone.org/home/taozj/source_code/git/repo/helloworld * branch            master     -> FETCH_HEADAlready up-to-date.

4.提交本地修改到远程存储仓库
而如果想把本地的更改更新到远程的服务器上面,可以使用 git push (远程仓储名) (分支名称)

[git@zfsworkstation ~/gitwork/helloworld]$ touch test2.txt[git@zfsworkstation ~/gitwork/helloworld]$ git push origin master[git@zfsworkstation ~/gitwork/helloworld]$ git add test2.txt[git@zfsworkstation ~/gitwork/helloworld]$ git commit -m "for test2.txt push test"[master 177fc0e] for test2.txt push test 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 test2.txt[git@zfsworkstation ~/gitwork/helloworld]$ git push origin masterPassword:Counting objects: 3, done.Delta compression using up to 4 threads.Compressing objects: 100% (2/2), done.Writing objects: 100% (2/2), 245 bytes, done.Total 2 (delta 1), reused 0 (delta 0)error: insufficient permission for adding an object to repository database ./objectsfatal: failed to write objecterror: unpack failed: unpack-objects abnormal exitTo ssh://git@zfsworkstation.peachzone.org/home/taozj/source_code/git/repo/helloworld ! [remote rejected] master -> master (n/a (unpacker error))error: failed to push some refs to 'ssh://git@zfsworkstation.peachzone.org/home/taozj/source_code/git/repo/helloworld'

上面的错误是因为文件系统的权限问题,我们 给git用户以wheel组的身份 ,同时代码树的组写权限也打开

[taozj@zfsworkstation ~/source_code/git]$ sudo pw usermod -n git -g wheel[taozj@zfsworkstation ~]$ chmod -R g+w source_code

然后就可以push代码更新了

[git@zfsworkstation ~/gitwork/helloworld]$ git push origin masterPassword:Counting objects: 3, done.Delta compression using up to 4 threads.Compressing objects: 100% (2/2), done.Writing objects: 100% (2/2), 216 bytes, done.Total 2 (delta 1), reused 0 (delta 0)To ssh://git@zfsworkstation.peachzone.org/home/taozj/source_code/git/repo/helloworld   ac1817f..2796f6c  master -> master

为了测试代码提交的结果,最后我们新建一个目录,重新从服务器上面下载代码,就可以看到新的结果体现出来了

[git@zfsworkstation ~/gitwork/testdir2]$ git clone ssh://git@zfsworkstation.peachzone.org/home/taozj/source_code/git/repo/helloworldCloning into helloworld...Password:remote: Counting objects: 37, done.remote: Compressing objects: 100% (29/29), done.remote: Total 37 (delta 11), reused 0 (delta 0)Receiving objects: 100% (37/37), done.Resolving deltas: 100% (11/11), done.[git@zfsworkstation ~/gitwork/testdir2]$ lshelloworld[git@zfsworkstation ~/gitwork/testdir2]$ cd helloworld/[git@zfsworkstation ~/gitwork/testdir2/helloworld]$ lsMakefile        README.txt      hello.cpp       test.txt        test2.txt
0 0