Git 零基础介绍

3,046 views
2,905 views

Published on

Git 背景、基本原理、基本命令和与 SVN 协同开发介绍

Published in: Technology, News & Politics
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,046
On SlideShare
0
From Embeds
0
Number of Embeds
734
Actions
Shares
0
Downloads
70
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

Git 零基础介绍

  1. 1. Git 网易有道 张宇辰Monday, February 27, 12
  2. 2. Agency • Git 历史 • 为什么用 Git • Git 基础知识 • 基本命令 • 暂存区与指针控制 (git reset/git checkout) • 与 SVN 协同 (git svn)Monday, February 27, 12
  3. 3. Git 历史 源社区又一神作Monday, February 27, 12
  4. 4. Linus Torvalds • Linux 项目的发起者 • 痛恨 VCS&SVN • 大神、脾气火爆、非常固执Monday, February 27, 12
  5. 5. 1991~2002 大神只用邮件收集 源社区的 patch,手工集成到自己电脑里 手工集成……手工……手工……伤不起啊! 源社区Monday, February 27, 12
  6. 6. 2002∼2005 大神终于选了 Bitlocker 做版本控制,一个商业软件 商业软件……商业……商业…… 源社区Monday, February 27, 12
  7. 7. 2005 年,Bitlocker 结束了对 源社区的授权。Monday, February 27, 12
  8. 8. 2005 年,Bitlocker 结束了对 源社区的授权。 我只是好奇... Andrew TridgellMonday, February 27, 12
  9. 9. 2005 年,Bitlocker 结束了对 源社区的授权。 我只是好奇... 一夜回到解放前 Andrew Tridgell 源社区Monday, February 27, 12
  10. 10. 2005 年,Bitlocker 结束了对 源社区的授权。 自己写一个! 我只是好奇... 一夜回到解放前 Andrew Tridgell 源社区 Linus TorvaldsMonday, February 27, 12
  11. 11. • 2005 年 4 月 3 日, 始 发 Git • 同年 4 月 6 日,项目发布 • 同年 4 月 7 日,Git 作为自身的项目控制工具,仅仅用了 5 天 • 同年 4 月 18 日,第一次多分支合并 • 同年 4 月 29 日,Linus 表示对 Git 的性能满意了 • 同年 6 月 16 日,Linux 2.6.12 发布, 始用 Git 维护,此时仅仅经过 74 天Monday, February 27, 12
  12. 12. Git 今日 Perl 3,500,000+ repositories QtMonday, February 27, 12
  13. 13. 为什么选择 Git SVN 为什么不爽Monday, February 27, 12
  14. 14. Bad case • 发过程中接到紧急需求 • 等待 review 的代码污染工作区 • 离线 发怎么办 • 写 Log 时不知道写什么Monday, February 27, 12
  15. 15. SVN remote checkout working copy modify repository commit developer’s computer 所有的修改只能发生在没有版本控制的本地 working copy 中Monday, February 27, 12
  16. 16. Git remote pull local checkout working copy repository push repository commit developer’s computerMonday, February 27, 12
  17. 17. Git remote pull local checkout working copy repository push repository commit developer’s computer 速度快 历史记录可控 可以离线工作Monday, February 27, 12
  18. 18. Git -- DVCS remote repository devA repository Distributed Version Control System devB repository ...Monday, February 27, 12
  19. 19. 除此之外 • 无处不在的自动分页 (less) • diff 支持逐字比较 (git diff --word-diff) • 版本库又小又快 • 支持多 网络传输协议 (http/ssh) 以及更强大的 git 协议,可以显示传输进度 • 丰富且简单的 config 配置Monday, February 27, 12
  20. 20. 安装 GitMonday, February 27, 12
  21. 21. Linux & Mac • Git: http://git-scm.comMonday, February 27, 12
  22. 22. Windows • Cygwin + git (推荐) • 命令更丰富 • msysGit + TortoiseGit • 图形界面 • 对中文目录名和文件名处理有问题Monday, February 27, 12
  23. 23. Git 基础知识 Git 就是个文件系统Monday, February 27, 12
  24. 24. Git 内部对象 null Commit 1 Commit 2 Tree 1 Tree 2 FileA FileA 2 FileB FileCMonday, February 27, 12
  25. 25. Git 内部对象 null Commit 1 Commit 2 Tree 1 Tree 2 FileA FileA 2 FileB FileC 一次提交中所有的文件Monday, February 27, 12
  26. 26. Git 内部对象 null Commit 1 Commit 2 Tree 1 Tree 2 FileA FileA 2 FileB FileC 一次提交 又一次提交Monday, February 27, 12
  27. 27. Git 内部对象 null Commit 1 Commit 2 Tree 1 Tree 2 同一个文件的历史版本 FileA FileA 2 FileB FileCMonday, February 27, 12
  28. 28. Git 内部对象 null Commit 1 Commit 2 HEAD 提交对象不可变且拥全局唯一 id 除第一个提交外,每个提交都保存上一个提交的引用 -- Git 里面就是个大链表Monday, February 27, 12
  29. 29. Git 基础命令Monday, February 27, 12
  30. 30. 不是检出 git clone remote_repo_url (checkout) 克隆一份远程仓库成为本地仓库 remote clone local checkout working copy repository repositoryMonday, February 27, 12
  31. 31. remote commit repository 239d5 master clone origin/master local commit repository 239d5 master HEAD checkout working copyMonday, February 27, 12
  32. 32. HEAD: 指向当前工作的 branch master: 默认的主干分支指针 origin/master: 远程仓库 origin 中 master 分支的指针 origin: 远程仓库名,在 git clone 时默认创建 commit origin/master 239d5 master HEADMonday, February 27, 12
  33. 33. echo “hello” > world git add world 将文件加入版本库 svn add commit origin/master 239d5 master HEADMonday, February 27, 12
  34. 34. git commit -m “init commit” 第一个提交 svn commit origin/master commit commit 239d5 7ca54 master HEADMonday, February 27, 12
  35. 35. git push origin master 将本地仓库推送到远程仓库上 svn commitMonday, February 27, 12
  36. 36. remote commit repository 239d5 master origin/master local commit repository 239d5 master HEAD commit working copyMonday, February 27, 12
  37. 37. remote commit repository 239d5 master origin/master local commit commit repository 239d5 7ca54 master HEAD commit working copyMonday, February 27, 12
  38. 38. remote commit commit repository 239d5 7ca54 master push origin/master local commit commit repository 239d5 7ca54 master HEAD commit working copyMonday, February 27, 12
  39. 39. remote commit commit repository 239d5 7ca54 master push origin/master local commit commit repository 239d5 7ca54 master HEAD commit working copyMonday, February 27, 12
  40. 40. Git 分支 git branch featureA git checkout featureA 在本地仓库创建一个分支 origin/master master commit 7ca54 featureA HEADMonday, February 27, 12
  41. 41. Git 分支 git branch -a 查看所有分支 master * foo remotes/origin/master origin/master master commit 7ca54 reatureA HEADMonday, February 27, 12
  42. 42. Git 分支 echo “r2” >> world; git add world; git commit -m ‘r2’ origin/master master commit 7ca54 reatureA HEADMonday, February 27, 12
  43. 43. Git 分支 echo “r2” >> world; git add world; git commit -m ‘r2’ origin/master master commit commit 7ca54 6539d featureA HEADMonday, February 27, 12
  44. 44. Git 分支切换 git checkout master origin/master master commit commit 7ca54 6539d featureA HEADMonday, February 27, 12
  45. 45. Git 分支切换 git checkout master origin/master master HEAD commit commit 7ca54 6539d featureAMonday, February 27, 12
  46. 46. Git 分支切换 git merge master 快进 (fast forward) 合并 origin/master master HEAD commit commit 7ca54 6539d featureAMonday, February 27, 12
  47. 47. Git 分支合并 git merge featureA 快进 (fast forward) 合并 origin/master master HEAD commit commit 7ca54 6539d featureAMonday, February 27, 12
  48. 48. Git 分支合并 git branch -d featureA origin/master master HEAD commit commit 7ca54 6539d featureAMonday, February 27, 12
  49. 49. Git 分支合并 git branch -d featureA origin/master master HEAD commit commit 7ca54 6539dMonday, February 27, 12
  50. 50. Git 冲突解决 remote commit commit repository 7ca54 ae78f master push origin/master local commit commit repository 7ca54 6539d master HEAD working copyMonday, February 27, 12
  51. 51. Git 冲突解决 git push 类似 svn 的 out of date 提示Monday, February 27, 12
  52. 52. remote commit commit repository 7ca54 ae78f master pull origin/master local commit commit repository 7ca54 6539d master HEAD working copyMonday, February 27, 12
  53. 53. remote commit commit repository 7ca54 ae78f master pull local commit commit repository 7ca54 6539d master HEAD commit ae78f working copy origin/masterMonday, February 27, 12
  54. 54. remote commit commit repository 7ca54 ae78f master pull local commit commit repository 7ca54 6539d master HEAD commit commit ae78f 36dc2 working copy origin/masterMonday, February 27, 12
  55. 55. remote commit commit repository 7ca54 ae78f master pull local commit commit repository 7ca54 6539d commit commit ae78f 36dc2 master HEAD working copy origin/masterMonday, February 27, 12
  56. 56. remote commit commit repository 7ca54 ae78f master push local commit commit repository 7ca54 6539d commit commit ae78f 36dc2 master HEAD working copy origin/masterMonday, February 27, 12
  57. 57. commit commit 7ca54 ae78f master remote repository commit commit 6539d 36dc2 push local commit commit repository 7ca54 6539d commit commit ae78f 36dc2 master HEAD working copy origin/masterMonday, February 27, 12
  58. 58. commit commit 7ca54 ae78f remote repository commit commit 6539d 36dc2 master push local commit commit repository 7ca54 6539d commit commit ae78f 36dc2 master HEAD working copy origin/masterMonday, February 27, 12
  59. 59. 暂存区与指针控制 git add git checkout git resetMonday, February 27, 12
  60. 60. Git 暂存区 工作区 版本库 a.js 暂存区 HEAD b.js master commit commit commit 7dae1 86dea 763daMonday, February 27, 12
  61. 61. Git 暂存区 git add a.js b.js 工作区 版本库 a.js 暂存区 HEAD b.js a.js master b.js commit commit commit 7dae1 86dea 763daMonday, February 27, 12
  62. 62. Git 暂存区 git commit 工作区 版本库 a.js 暂存区 HEAD b.js a.js master b.js commit commit commit 7dae1 86dea 763daMonday, February 27, 12
  63. 63. Git 暂存区 git commit 工作区 版本库 a.js 暂存区 HEAD b.js a.js master b.js commit commit 86dea 763daMonday, February 27, 12
  64. 64. Git 暂存区 git commit 工作区 版本库 a.js HEAD b.js master commit commit 暂存区 86dea 763da a.js b.jsMonday, February 27, 12
  65. 65. Git 暂存区 git commit 工作区 版本库 a.js HEAD b.js master commit commit 暂存区 86dea 763da a.js b.jsMonday, February 27, 12
  66. 66. Git 暂存区 git commit 工作区 版本库 a.js HEAD b.js master commit commit 暂存区 86dea 763da a.js b.jsMonday, February 27, 12
  67. 67. Git 暂存区 git commit 工作区 版本库 a.js HEAD b.js master commit commit 86dea 763daMonday, February 27, 12
  68. 68. Git 暂存区 工作区 版本库 a.js HEAD b.js master commit commit 86dea 763daMonday, February 27, 12
  69. 69. Git 暂存区 工作区 版本库 a.js HEAD b.js master commit commit commit 86dea 763da 12caaMonday, February 27, 12
  70. 70. Git 暂存区 工作区 版本库 a.js HEAD b.js master commit commit commit 86dea 763da 12caaMonday, February 27, 12
  71. 71. Git 暂存区 工作区 版本库 a.js HEAD b.js master commit commit commit 86dea 763da 12caaMonday, February 27, 12
  72. 72. Git 暂存区 工作区 版本库 a.js 暂存区 HEAD b.js a.js master b.js commit commit commit 86dea 763da 12caaMonday, February 27, 12
  73. 73. Git 分支指针控制 git reset --soft 763da 工作区 版本库 a.js 暂存区 HEAD b.js a.js master b.js commit commit commit 86dea 763da 12caaMonday, February 27, 12
  74. 74. Git 分支指针控制 git reset --soft 763da 工作区 版本库 a.js 暂存区 HEAD b.js a.js master b.js 1 commit commit commit 86dea 763da 12caaMonday, February 27, 12
  75. 75. Git 分支指针控制 git reset [--mixed] 763da 工作区 版本库 a.js 暂存区 HEAD b.js a.js master b.js 1 commit commit commit 86dea 763da 12caaMonday, February 27, 12
  76. 76. Git 分支指针控制 git reset [--mixed] 763da 工作区 版本库 a.js 暂存区 HEAD b.js master 2 1 commit commit commit 86dea 763da 12caaMonday, February 27, 12
  77. 77. Git 分支指针控制 git reset --hard 763da 工作区 版本库 a.js 暂存区 HEAD b.js master 2 1 commit commit commit 86dea 763da 12caaMonday, February 27, 12
  78. 78. Git 分支指针控制 git reset --hard 763da 工作区 版本库 3* 暂存区 HEAD master 2 1 commit commit commit 86dea 763da 12caa * 此处仅做演示 --hard 的效果,实际上 git 不会删除暂存区中不存在的文件,只会添加新文件和重置文件修改Monday, February 27, 12
  79. 79. Git 分支指针控制 git reset --soft: 1 --mixed: 1 & 2 版本库 --hard: 1 & 2 & 3 暂存区 HEAD master 3* 2 I’m lost* 1 工作区 commit commit commit a.js 86dea 763da 12caa b.js * 可以通过 git reflog 查看 HEAD 指针最近的移动历史Monday, February 27, 12
  80. 80. Git 分支指针控制 3 == git checkout . 版本库 暂存区 HEAD master 3 2 1 工作区 commit commit a.js 86dea 763da b.jsMonday, February 27, 12
  81. 81. Git HEAD 指针控制 git checkout 86dea 工作区 版本库 a.js 暂存区 HEAD b.js a.js master b.js commit commit commit 7dae1 86dea 763daMonday, February 27, 12
  82. 82. Git HEAD 指针控制 git checkout 86dea 工作区 版本库 a.js 暂存区 HEAD b.js a.js master b.js 1 commit commit commit 7dae1 86dea 763daMonday, February 27, 12
  83. 83. Git HEAD 指针控制 git checkout 86dea 工作区 版本库 a.js 暂存区 HEAD b.js master 2 1 commit commit commit 7dae1 86dea 763daMonday, February 27, 12
  84. 84. detached HEAD 状态 git checkout 86dea Note: checking out 86dea. You are in detached HEAD state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b new_branch_name HEAD is now at 78f871f... remove doc HEAD master commit commit commit 7dae1 86dea 763daMonday, February 27, 12
  85. 85. detached HEAD 状态 HEAD commit 45cdd master commit commit commit 7dae1 86dea 763daMonday, February 27, 12
  86. 86. detached HEAD 状态 git checkout master HEAD commit 45cdd master commit commit commit 7dae1 86dea 763daMonday, February 27, 12
  87. 87. detached HEAD 状态 I’m lost HEAD commit 45cdd master commit commit commit 7dae1 86dea 763daMonday, February 27, 12
  88. 88. 在 detached HEAD 状态上创建分支 git checkout -b branch HEAD branch commit 45cdd master commit commit commit 7dae1 86dea 763daMonday, February 27, 12
  89. 89. 在 detached HEAD 状态上创建分支 git checkout master branch HEAD commit 45cdd master commit commit commit 7dae1 86dea 763daMonday, February 27, 12
  90. 90. 修改历史记录 git rebaseMonday, February 27, 12
  91. 91. 通过 rebase 合并分支 git rebase master commit 832ac foo HEAD commit commit 7ca54 6539d masterMonday, February 27, 12
  92. 92. 通过 rebase 合并分支 git rebase master foo HEAD commit 832ac commit commit 7ca54 6539d masterMonday, February 27, 12
  93. 93. 通过 rebase 合并分支 git rebase master foo HEAD commit 832ac commit commit 7ca54 6539d masterMonday, February 27, 12
  94. 94. 通过 rebase 合并分支 git rebase master foo HEAD commit 832ac commit commit commit 7ca54 6539d 35ce5 masterMonday, February 27, 12
  95. 95. 通过 rebase 合并分支 git rebase master foo HEAD commit 832ac commit commit commit 7ca54 6539d 35ce5 masterMonday, February 27, 12
  96. 96. 通过 rebase 合并分支 git rebase master foo HEAD commit 832ac commit commit commit 7ca54 6539d 35ce5 masterMonday, February 27, 12
  97. 97. 通过 rebase 合并分支 git rebase master foo HEAD commit 832ac commit commit commit 7ca54 6539d 35ce5 masterMonday, February 27, 12
  98. 98. 通过 rebase 合并分支 git checkout master; git merge foo foo HEAD commit 832ac commit commit commit 7ca54 6539d 35ce5 masterMonday, February 27, 12
  99. 99. 通过 rebase 合并分支 git checkout master; git merge foo foo commit 832ac commit commit commit 7ca54 6539d 35ce5 master HEADMonday, February 27, 12
  100. 100. 通过 rebase 合并分支 Pros: 提交记录更简单 Cons: 操作略微 杂 HEAD master commit commit commit 7ca54 6539d 35ce5Monday, February 27, 12
  101. 101. Git + SVN 协同Monday, February 27, 12
  102. 102. git svn clone -r 310135:HEAD --trunk=trunk --branches=branches --branches=milestones https://..../YNote/ClientMonday, February 27, 12
  103. 103. git 命令 svn 命令 git pull svn update git push svn commitMonday, February 27, 12
  104. 104. git 命令 git svn 胶水命令 svn 命令 git pull git svn rebase svn update git push git svn dcommit svn commitMonday, February 27, 12
  105. 105. Git 技巧Monday, February 27, 12
  106. 106. Git revert commit commit 239d5 7ca54 master HEAD a.js +++Monday, February 27, 12
  107. 107. Git revert commit commit commit 239d5 7ca54 7865a master HEAD a.js +++ a.js ---Monday, February 27, 12
  108. 108. git commit --amend MOD: blahblah commit commit 239d5 7ca54 master HEADMonday, February 27, 12
  109. 109. git commit --amendMonday, February 27, 12
  110. 110. git commit --amend MOD: blahblah commit commit 239d5 7ca54 commit 7ca54 master HEAD MOD: ...Monday, February 27, 12
  111. 111. 通过 rebase 合并提交记录 git rebase -i origin/master # -i 进入交互式 rebase origin/master HEAD commit 7ca54 master commit commit 6539d 35ce5Monday, February 27, 12
  112. 112. 通过 rebase 合并提交记录 git rebase -i origin/master # -i 进入交互式 rebase origin/master HEAD commit commit 7ca54 d32ab master commit commit 6539d 35ce5Monday, February 27, 12
  113. 113. 通过 rebase 合并提交记录 git rebase -i origin/master # -i 进入交互式 rebase origin/master HEAD commit commit 7ca54 d32ab masterMonday, February 27, 12
  114. 114. 通过 rebase 丢弃提交记录 git rebase -i origin/master # -i 进入交互式 rebase origin/master HEAD commit 7ca54 master commit commit 6539d 35ce5Monday, February 27, 12
  115. 115. 通过 rebase 丢弃提交记录 git rebase -i origin/master # -i 进入交互式 rebase origin/master HEAD commit commit 7ca54 d32ab master commit commit 6539d 35ce5Monday, February 27, 12
  116. 116. Git rebase tip • 文件只要进了 git 的版本库,就是安全的 • rebase 本质上是基于已有提交构造新提交 • 永远不要 rebase 已经推送到公共仓库的提交Monday, February 27, 12
  117. 117. Git 学习资源Monday, February 27, 12
  118. 118. Pro git 免费的在线电子书 http://progit.orgMonday, February 27, 12
  119. 119. Git 权威指南Monday, February 27, 12
  120. 120. Q&A 张宇辰 zhangyc@rd.netease.com git 学习 POPO 群: 1195049Monday, February 27, 12

×