在QtCreator上使用版本控制系统Git(三)

<style type="text/css"> <!-- @page {margin:2cm} td p {margin-bottom:0cm} p {margin-bottom:0.21cm} --> </style>

QtCreator

使用版本控制系统Git(三)

git的强大之处在于能很好的处理多人的协同工作。下面我介绍一下怎样在QtCreator下怎样使用git进行多人的协同工作。我也是最近学会的,有不对的地方请尽管提出来,我会改正的。

开发环境是:Ubuntu11.10QtCreator2.41Qt4.8

下面是典型的两人协同工作情景:蒋彩阳和蒋彩星是两个开发者,他们的计算机IP分别是172.16.200.47172.16.200.43。现在他们准备协同建立一个小项目。首先要配置好环境。如果还没有配置相关环境的话,可以看这里

蒋彩阳

172.16.200.47

蒋彩星

172.16.200.43

现在作为本地仓库的172.16.200.43需要将作为服务器代码仓库的172.16.200.47中的代码复制到本地来。我们这样做:

创建一个新的项目,从代码仓库中提取。如图:

在QtCreator上使用版本控制系统Git(三)

选择“Git仓库的克隆”,进入下一个界面:

在QtCreator上使用版本控制系统Git(三)

在“克隆URL”一栏中填写正确的URL。因为蒋彩阳的计算机(172.16.200.47)的test项目保存在home/jiangcaiyang/test中,这里用~代表当前用户的目录。随后设置工作拷贝的目录,这里选择172.16.200.43计算机上的home/jiangcaiyang/Programs目录。并且命名检出目录是test。默认下检出目录test是随着URL的填写自动适应的。点击”下一步“;

在QtCreator上使用版本控制系统Git(三)

如果一切正常,将会显示下面的界面。常见的错误有远程目录不是代码仓库、目标目录已经存在但不是代码仓库、远程拒绝回应(一般是没有安装openssh-server)、连接问题等等。

点击“完成”,创建项目test的目标设置。创建完毕后如下图所示:

在QtCreator上使用版本控制系统Git(三)

这是上一次完成的test程序,下面蒋彩星同学要在此添加一段代码,作为项目提交的依据:

在QtCreator上使用版本控制系统Git(三)

修改完了,确认无误后提交一下,下面是提交的界面:

在QtCreator上使用版本控制系统Git(三)

填写完了信息后点击提交,查看一下我们提交的信息吧。用“工具➡Git➡Log”查看一下我们的提交。

在QtCreator上使用版本控制系统Git(三)

嗯,一切都很好。下面蒋彩星要将其上传至代码库。我们可以点击“工具➡Git➡Push”提交至远程的代码库。首先还是要经过OpenSSH的认证,但是认证后出现了这样的错误:

22:16正在/home/jiangcaiyang/Programs/test中执行:/usr/bin/git push

remote:error: refusing to update checked out branch: refs/heads/master

remote:error: By default, updating the current branch in a non-barerepository

remote:error: is denied, because it will make the index and work treeinconsistent

remote:error: with what you pushed, and will require 'git reset --hard' tomatch

remote:error: the work tree to HEAD.

remote:error:

remote:error: You can set 'receive.denyCurrentBranch' configuration variableto

remote:error: 'ignore' or 'warn' in the remote repository to allow pushinginto

remote:error: its current branch; however, this is not recommended unlessyou

remote:error: arranged to update its work tree to match what you pushed insome

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'.

Tossh://172.16.200.47/~/test

![remote rejected] master -> master (branch is currently checkedout)

error:failed to push some refs to 'ssh://172.16.200.47/~/test'

原因是默认git不允许执行push操作(见这里),需要更改我们test代码仓库的设置。执行下面的指令:

~$cd test/.git

~$gedit config

将后面添加这一句:

[receive]

denycurrentbranch= ignore

保存,随后再试一遍。结果显示成功。

22:25正在/home/jiangcaiyang/Programs/test中执行:/usr/bin/git push

Tossh://172.16.200.47/~/test

c6e125c..557ff45 master -> master

命令'/usr/bin/git'正确结束.

但是蒋彩阳的计算机(172.16.200.47)这里仍然未显示已经更改的数据。是怎么回事呢?我们打开gitk看看:

在QtCreator上使用版本控制系统Git(三)

原来版本1.3在这里啊。我们可以通过重新设置HEAD指针来回到Ver.1.3版本。这里选择Hard来还原,这样以来就可以在蒋彩阳的电脑上显示蒋彩星修改的内容了。

在QtCreator上使用版本控制系统Git(三)