Git 使用介绍3. Git下的开发场景
• 1.连不上公司网络怎么办?
– 吭哧吭哧,
– 完成各种功能,
– feature代码照样提交
– 回到公司或者网络恢复,提交
• 2.产品已经上线了,需要开发一个新的功能,花费时间较长,又不能影响其
他人
– 新起一个分支
– 吭哧吭哧
– 完成各种功能
– 调试通过
– 提交合并到master主干版本
• 3.重构
– 小范围代码分支签出
– 重构完毕
– 合并到主干
4. 什么是Git?
• 最早由linus torwalds用来管理linux 内核开发
• SCM
• 开源(GNU GPL V2)
• 速度快
• 分布式
– 离线也能继续开发
• 非线性开发
7. 与SVN相比
Git Svn
分布式管理 集中式管理
速度快 速度慢
控制命令多 有成熟的管理工具
擅长分支管理 不擅长分支管理
处理文件冲突很好 处理文件冲突很差
8. Git安装
• Windows
– Msysgit
• http://msysgit.github.com/
– cygwin+git+openssh
– Tortoisegit
• Centos
– yum install git
• Ubuntu
– apt-get install git
• Mac
– Tower
13. 生成密钥对
• $ ssh-keygen
• Generating public/private rsa key pair.
• Enter file in which to save the key
(/c/Users/Medcl/.ssh/id_rsa):
Git服务器通过这些信息
来识别你的身份
• $ ls
• id_rsa id_rsa.pub known_hosts
14. 基本设置
你的信息,方便识别代码所有者
• git config --global user.name
“medcl”
• git config --global user.email
“m@medcl.net”
15. 创建一个Repository
• Repository(代码仓库)
在本地新建仓库,就可以开
• 1.本地新建一个代码仓库 始使用git的所有功能
– cd project
– git init .
– git add .
– git commit –m “initial commit”
• 2.知道远程repo地址,复制一份repo副本到本地
– git clone git://github.com/medcl/medcl.github.com.git
协同开发同一个项目的时候
17. 添加文件
• 自动添加目录及子目录下文件
– git add .
• 添加指定文件到git仓库中
– git add <文件名>
• 其它(查看帮助:git help add)
– git add git-*.sh
– git add Documentation/*.txt
21. .gitignore
• 告诉git那些文件不需要管
– 即使git add 也不会添加这些文件
• .gitignore文件内容例子:
/data
/work
/logs
/.idea
/target
/out 确保只提交给源代码到repo!
.DS_Store
*.iml
各种编译输出,中间文件,一定不要提交上去
24. 历史记录
• git show HEAD
• git help show
• gitk –all
• git log --graph
26. 撤销回退
• git checkout [HEAD] 档名
• git checkout master
• git reset HEAD 档名
– git reset SHA1:回退到指定版本
– git reset HEAD~1:回退到上一个版本
– 变回unstaged 或者 untracked;不变更内容
27. 分支
• 为什么要使用分支?
– 重构(refactor)
– 开发新功能
– 修复bug
• git鼓励多分支操作
• 每次修改都应该在分支上进行
• 只有调试通过了,才应该merge回主分支
29. 分支操作
• git branch
• master:默认的分支名称
• git checkout –b newidea :建立分支并且切换
• git branch
• git checkout newidea 签出分支(切换到分支下)
• git branch 查看当前分支
• git merge newidea 合并分支
• git branch –D newidea 删除分支
– 只能删除非工作分支
• git format-patch origin/master 可在当前目录下生成补丁
• git checkout master
• git aply xxx.patch
30. 分支操作
• touch newfile
• git commit –a –m “new feature:xxx”
• git checkout master
• git merge newidea
– 如果没有冲突,会自动合并
• git branch
• 合并分支:git merge <source branch>
• 目标分支为当前分支
32. 参考资源
• http://github.com
• http://progit.org
• http://git-scm.com
• https://git.wiki.kernel.org/index.php/Gitweb
• http://www.gitalist.com/install/
• http://gitstack.com
• http://sitaramc.github.com/gitolite/sts.html#ssh-ha