您的位置:首页 > 其它

Git版本管理使用教程

2016-06-20 20:57 393 查看

安装和配置Git客户端

Linux(Debian或Ubuntu)下安装Git用以下命令:
sudoapt-get install git
Windows下安装Git需从以下网址下载msysgit:
https://git-for-windows.github.io
运行安装文件,按默认选项安装即可。
安装完成后需运行以下命令配置用户名和邮箱:
gitconfig --global user.name "Your Name"
gitconfig --global user.email “email@example.com

关联远程仓库

远程仓库就是Git的一个中心库,所有开发人员通过这个中心库交换本地库的文件。所有开发人员在本地库的提交都要推送到这个中心库上,并且所有开发人员在修改本地库的文件前都要从中心库拉取最新版本的文件。
通过以下命令将本地库和远程库关联:
gitremote add origin git@server-name:path/repo-name.git
server-name,path和repo-name请询问系统管理员。
在Linux中打开Shell(Windows下打开GitBash),执行以下命令创建SSHKey:
ssh-keygen-t rsa -C “email@example.com
执行以上命令后,可以在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,将id_rsa.pub文件发给系统管理员,由系统管理员将你的公钥添加到远程仓库中。
执行以下命令将远程仓库克隆到本地:
gitclone
git@server-name:path/repo-name.git

开发分支

开发人员的开发工作必须在开发分支(develop)上进行,如果本地还没有开发分支,需要先从远程仓库拉取到本地:
gitcheckout -b develop origin/develop
如果本地已有develop分支,则要先切换到develop分支,再从远程仓库拉取最新的代码:
gitcheckout develop
gitpull
如果gitpull失败,原因可能是没有指定本地develop分支与远程origin/develop分支的链接,根据提示,设置develop和origin/develop的链接:
gitbranch --set-upstream develop origin/develop
开发完成后,将修改提交到本地develop分支,并推送到远程仓库:
gitadd *
gitcommit –m “Feature 010 develop completed”
gitpush origin develop

合并分支

在develop分支上开发和测试完成后,切换到主分支,将develop分支合并到主分支:
gitcheckout master
gitmerge develop

解决冲突

如果在合并分支或从远程仓库拉取时出现以下错误提示:
Auto-mergingexercise.txt
CONFLICT(content): Merge conflict in exercise.txt
Automaticmerge failed; fix conflicts and then commit the result.
说明修改内容和别人的修改冲突了,“exercise.txt”是冲突的文件名,打开“exercise.txt”,看到文件内容变成下面这样:
Thisis a git exercise.
<<<<<<<HEAD
Thisis a git exercise for merge.
=======
Thisis a git exercise for branch.
>>>>>>>develop
“<<<<<<<HEAD”下面的部分是当前分支所做的修改,“>>>>>>>develop”上面的部分是develop分支所做的修改,将两部分合并,解决冲突后,重新提交修改及合并(或推送到远程仓库)。

其他常用命令

gitstatus #查看版本库当前状态
gitdiff <file name> #查看具体的修改内容,<file name >是修改的文件名,可省略
gitlog #查看提交日志,按Q退出
gitreset --hard HEAD~n #向前回退n个提交
gitreflog #查看命令历史记录
gitrm <file name> #从版本库中删除某个文件
gitremote #查看远程仓库配置
gitbranch #查看分支信息
gitbranch –b <branch name> #创建本地分支并切换到该分支
gitbranch –d <branch name> #删除本地分支
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: