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> #删除本地分支
相关文章推荐
- Handler相关
- 前端面试--大众点评
- JavaScript中日期时间操作
- maven详解六、maven的使用问答
- iOS 判断字符串中含有某个字符串
- jQuery源码学习(四)
- 互斥型信号量
- 2016.6.20
- HTML标准属性
- 羽毛球比赛中羽毛球擦网和卡网(挂网)怎么判罚?
- 多线程编程(一)多线程基础
- mybatis_user_guide(6) Java API
- maven详解五、maven mvn 命令行的操作和使用
- Ojective-C入门请读
- 基础算法学习-求组合数
- iOS_逆向_使用class dump导出头文件
- 字节流调试笔记
- 兼容IE,Firefox,CSS3 opacity透明度-FireFox 3.5+不支持-moz-opacity属性
- 兼容IE,Firefox,CSS3 opacity透明度-FireFox 3.5+不支持-moz-opacity属性
- 兼容IE,Firefox,CSS3 opacity透明度-FireFox 3.5+不支持-moz-opacity属性