Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Git版本控制

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for 卢克 卢克
June 01, 2013

 Git版本控制

讲述Git的常用命令及存储原理

Avatar for 卢克

卢克

June 01, 2013
Tweet

Other Decks in Technology

Transcript

  1. Git安装 1.Linux 通过包管理工具进行安装 $ yum install git-core $ apt-get install

    git 2.Mac 安装Xcode的命令行工具 通过第三方的包管理工具进行安装 通过安装包进行安装 $ sudo port install git-core +svn +doc +bash_completion +gitweb $ brew install git 3.Windows 安装文件  记得设置环境变量
  2. Git相关资源 学习 客户端 SourceTree(Mac&Windows  免费) Tower(Mac  收费) 服务器端 gitosis gitolite

    gitlab基于gitolite并用Rails开发的一个开源系统 http://github.com http://gitlab.alibaba-inc.com/ http://gitready.com/
  3. Git基础命令 git init [--bare] [directory] git init [--bare] [directory] bare的库一般用于远程库,文件夹名称一般以.git结尾,比如node.git

    Luke on luke.local in ~/Projects/git-repo-demo $ git init Initialized empty Git repository in /Users/Luke/Projects/git- repo-demo/.git/ Luke on luke.local in ~/Projects/git-repo-demo(~|master) $ ls -al .git total 24 drwxr-xr-x 10 Luke staff 340 Apr 12 22:12 . drwxr-xr-x 3 Luke staff 102 Apr 12 22:12 .. -rw-r--r-- 1 Luke staff 23 Apr 12 22:12 HEAD drwxr-xr-x 2 Luke staff 68 Apr 12 22:12 branches -rw-r--r-- 1 Luke staff 138 Apr 12 22:12 config -rw-r--r-- 1 Luke staff 73 Apr 12 22:12 description drwxr-xr-x 10 Luke staff 340 Apr 12 22:12 hooks drwxr-xr-x 3 Luke staff 102 Apr 12 22:12 info drwxr-xr-x 4 Luke staff 136 Apr 12 22:12 objects drwxr-xr-x 4 Luke staff 136 Apr 12 22:12 refs
  4. Git基础命令 git clone <repo> [directory] 克隆git库,⽀支持 ssh:// file:// git:// 以及http(s)://等协议

    git clone [email protected]:joyent/node.git git config [--global] user.name <name> git配置项设置命令,其实你也可以直接修改相关配置⽂文件 当前⽤用户全局配置⽂文件 ~/.gitconfig 当前库的配置⽂文件 .git/config git log [--author="<pattern>"] [--grep="<pattern>"] [--graph] [-- decorate] [--oneline] [c1..c2] 查看git库提交历史记录,注意 c1..c2 为(c1,c2] $ git log --graph --decorate --oneline * 881ef7c url: ~ is not actually an unwise char * 17a379e url: Escape all unwise characters * 061151c uv: Upgrade to v0.11.1 * 259839f Merge branch 'v0.10' |\ | * 8ee4300 (origin/v0.10) build: Typo in tools/msvs/msi/product.wxs | * b0de1e4 stream: Fix unshift() race conditions | * 440bc06 Now working on v0.10.5 | * bf8ed11 Merge branch 'v0.10.4-release' into v0.10 | |\ | | * 9712aa9 (tag: v0.10.4, origin/v0.10.4-release) 2013.04.11, Version 0.10.4 (Stable) | * | 22c7d13 lint | * | 50be397 blog: Fix title for v0.8.23 release | |/ | * 1ccae9c npm: Upgrade to 1.2.18
  5. Git基础命令 git add [filename|-A|-i|-p] 将修改加⼊入暂存区,以便下次提交 git commit -m “commit消息” 将暂存区的修改进⾏行提交

    文件状态图 未修改 已修改 已暂存 已提交 修改 git  checkout  filename git  add  filename git  reset  filename git  commit git  com m it  -a 跳 过 暂 存 区 直 接 提 交 git  reset  --hard 撤 销 所 有 变 化 , 包 括 暂 存 的 和 未 暂 存 的
  6. Git基础命令 git show <object> 可以用来显示某次提交的详情 git show 31d0d5a #直接使⽤用commit的sha1 git

    show v1.01 #tag name git show master #分⽀支名 git show HEAD #当前分⽀支最后⼀一次提交 git show HEAD^ #当前分⽀支的前⼀一次提交 等同于HEAD~1 git show HEAD^^ #当前分⽀支的上上次提交 等同于HEAD~2 git show HEAD~3 #当前分⽀支的倒数第三次提交 等同于HEAD^^^ git show HEAD@{10} #by reflog PS:版本标识详解 https://www.kernel.org/pub/software/scm/git/docs/gitrevisions.html
  7. Git撤销命令 git checkout [commit] 检出之前的任一版本到工作拷贝 工作拷贝 checkout master git revert

    <commit> 撤销之前的某次提交 git reset <commit> 重置历史都某次commit X master revert&commit git clean 清除所有未跟踪的文件 master 修改未暂存 reset master
  8. Git分支操作 git branch 列出本地库中所有的分支 git branch <branch> 创建分支 git branch

    -m <branch> 修改当前分支名 git branch -D|-d <branch> 删除分支,其中-d是安全删除 B C A master D E F feature1
  9. Git分支操作 B C A master D E F feature1 G

    git merge [--no-ff] <branch> 将branch分支合并到当前分支
  10. Git分支操作 什么是fast  forwarding? B C A master D E F

    feature1 master fast  forwarding non  fast  forwarding B C A master D E F feature1 G master $git merge feature1 fast  forwarding情况不走ff $git merge --no-ff feature1 B C A master D E F feature1 G master
  11. Git改写历史 git rebase -i <after-this-commit> 以交互的方式重新组织<after-this-commit>之后的所有提交 git rebase -i master~2

    1 pick 38149bb http: escape unsafe characters in request path¬ 2 pick 31d0d5a test: add extra checks¬ 3 ¬ 4 # Rebase 881ef7c..31d0d5a onto 881ef7c¬ 5 #¬ 6 # Commands:¬ 7 # p, pick = use commit¬ 8 # r, reword = use commit, but edit the commit message¬ 9 # e, edit = use commit, but stop for amending¬ 10 # s, squash = use commit, but meld into previous commit¬ 11 # f, fixup = like "squash", but discard this commit's log message¬ 12 # x, exec = run command (the rest of the line) using shell¬ 13 #¬ 14 # These lines can be re-ordered; they are executed from top to bottom.¬ 15 #¬ 16 # If you remove a line here THAT COMMIT WILL BE LOST.¬ 17 # However, if you remove everything, the rebase will be aborted.¬ 18 #¬ 19 # Note that empty commits are commented out¬ ~ "~/Projects/node/.git/rebase-merge/git-rebase-todo" 19L, 700C B C A master D master~2 HEAD
  12. Git远程库操作 分支同步及操作 git fetch origin master 获取远程库分支,获取后远程库的分支后在本地都有一个对应的引用,其 和本地分支引用本质上是一样的 可以通过git  branch

     -r命令查看本地库中对应的远程分支 git pull [--rebase] origin master 本质上就是先fetch,然后merge或rebase git push origin master 将master分支同步给origin git push origin :feature 删除远程分支 git push origin --tags push所有的本地tags git push origin --force 强制覆盖远程库的分支
  13. Git远程库操作 引用映射规则  Refspec [+]<src>:<dst> src  源分支引用 dst  目标分支引用 如果存在+,那么同步出现non  fast

     forward的时候 进行强制更新 当添加⼀一个远程库的时候 .git/config中⾃自动增加⼀一个fetch的映射规则 [remote "origin"]¬ url = [email protected]:joyent/node.git¬ fetch = +refs/heads/*:refs/remotes/origin/* 当你执⾏行 git fetch origin 的时候默认会按照这个映射规则进⾏行fetch 当然你也可以在执⾏行命令的时候显式的指定 规则
  14. Git其他命令 git reflog reflog,就是reference  log的意思,是git中用来记录引用历 史的机制,比如某个分支上次或者上上次所指向的commit, 当某个分支更新时,对应的reflog便会记录一条记录 git tag 打tag

    git cherry-pick 单独挑一个或者几个commit进行合并 git bisect 如果当前分支无法编译或者存在问题,将当前分支设置成 bad,然后再设置上次ok的分支或提交为good,然后进行二 分查找“问题提交” git submodule init/update submodule初始化以及更新
  15. Git  Workflow B C A D H devel master G

    I J feature v1.0 E rc F 可以考虑加入merge  request和review的流程
  16. Git对象 引用的分类 1.分支  .git/refs/heads/ 2.HEAD标记  .git/HEAD 3.Tags      .git/refs/tags/

    4.Remotes即远程分支  .git/refs/remotes/ 当然分支,标签或者远程分支都可能不在以上目录内而出现在  .git/packed-refs文件中
  17. Git对象 object header object content SHA1哈希 5ad28e22767f979da2c198dc6c1003b25964e3da zlib压缩 compressed object

    .git/objects/5a/d28e22767f979da2c198dc6c1003b25964e3da 对象路径 object header为: type+’ ‘+内容⻓长度+’\0’ 松散对象    Loose object
  18. Git对象 #!/usr/bin/python # -*- coding: utf-8 -*- import zlib,os,sys,hashlib reload(sys)

    sys.setdefaultencoding('utf8') fp = open(os.path.expanduser("~/Projects/git-repo-demo/.git/ objects/61/407dd7081e36deb99149d03ca8ea849353555e")) str = fp.read() fp.close() str = zlib.decompress(str) print str gitsha1 = hashlib.sha1() gitsha1.update(str) print gitsha1.hexdigest() Git提供了查看对象文件的工具  git cat-file -p <object> 当然你也可以使用代码来验证,下面是使用Python代码来验证Git  Object的例子
  19. Git对象 commit tree blob tag 父提交 子目录(+目录名) tag  ref reference

    1 1 1 0..* 1 0..* 1 0..* 1 1 文件(+文件名  +模式) Git对象关系图 分支或者轻量级标签
  20. Git对象 一个Git库经过三次提交后的对象图 commit e9158a3 tree 61407dd blob 5ad28e2 README.md commit

    01b61c3 tree 822812 parent tree c260b5b subdir READ M E.m d blob 0fa1294 demo.txt commit eb490b0 parent tree 9aa45fe subdir blob 7688d56 README.md master HEAD
  21. Git对象的存储 悬空对象dangling  object 查看 git fsck --unreachable --no-reflogs #忽略relog的引⽤用 git

    fsck --unreachable #查看真正的悬空对象 清理那些unreachable的commit git  reflog  expire  -­‐-­‐expire=now  -­‐-­‐all git  gc  -­‐-­‐prune=now git reset --hard HEAD^^ master master unreachable commit 这些对象不会被其他任何分支直接或者间接引用到,但是有可能被 reflog引用到,当他们从reflog中也清除掉后就变成悬空对象了,可以 通过一定的命令将悬空对象删除 B C A master E feature1 E' feature1 unreachable commit git rebase master