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

Git 入门实战

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for icyleaf icyleaf
September 08, 2012

Git 入门实战

随着 Git 越来越受欢迎,目前开始大部队开始尝试使用 Git,但是 Git 的强大不仅仅在于机制和使用上,更在于命令的灵活性,它和其他版本控制有何不同,使用它又能帮助开发者解决哪些问题,这个就是本话题分享的抛砖引玉的开篇。

Avatar for icyleaf

icyleaf

September 08, 2012
Tweet

More Decks by icyleaf

Other Decks in Programming

Transcript

  1. 个人 SCM 使用经历 n 2008 文件打包备份 (个人解决方案) n 2008 1-2

    个月 CVS (同事解决方案) n 2008 - 2009 SVN (源于 Wordpress ) n 2009 - 至今 Git (源于 Kohana PHP 3.0 )
  2. Linus Torvalds n 05.4.3 Git 项目启动 n 05.4.6 项目第一版发布 n

    05.4.7 Git 作为自身的版本控制工具 n 05.4.18 发布第一个多分支合并 n 05.4.29 Git 性能已经达到预期 n 05.6.16 Git 正式维护 Linux 内核代码
  3. git 与 svn n git 是分布式, svn 是集中式 n git

    速度快, svn 慢的一塌糊涂 n git 把内容按元数据方式存储, svn 是按文件 n git branch 灵活且强大, svn 仅仅是文件复制管理 n git 有无尽的后悔药, svn 的恢复显得有些苍白
  4. 初次配置 git config --global user.name “icyleaf” git config --global user.email

    [email protected]” git config --global core.filemode false git config --global core.autocrlf true Windows
  5. 克隆仓库 # 本地⽂文件路径 git clone /opt/git/project.git # ⽂文件协议 git clone

    file://opt/git/project.git # HTTP 协议 git clone https://github.com/progit/progit.git # SSH 协议 git clone ssh://[email protected]:progit/progit.git git clone [email protected]:progit/progit.git
  6. 添加 & 提交 touch README git add README git commit

    -m “add README” touch LICENSE git status echo “Hello, World” > README git commit -am “updated README” git status
  7. 查看历史 git log # 查看最近 3 次提交的详细修改内容 git log -p

    -3 # 查看 icyleaf ⽤用户最近⼀一个星期提交信息 git log --author icyleaf --since=‘one week ago’ # ⽤用简单图形查看分⽀支提交的情况 git log --graph --oneline
  8. 恢复 echo “Hellp, icyleaf” > README git checkout -- README

    echo “Hello, icyleaf” > README git add README git reset -- README echo “Hello, Mr. icyleaf” > README git commit -am ‘Modified README’ git commit --amend
  9. 分支 # 列出当前所有本地分⽀支 git branch # 新建⼀一个名为 develop 的分⽀支并切换到它 git

    branch develop git checkout master # 下⾯面⼀一⾏行命令等同于上⾯面两⾏行(快速⾼高效,推荐!) git checkout -b develop # 改名分⽀支 git branch -m develop 2.0/develop # 删除分⽀支 git branch -d 2.0/develop
  10. 本地远程仓库 # 查看本地远程仓库 git removte -v # 添加本地远程仓库 git remote

    add upstream http://github.com/icyleaf/repo.git # 改名本地远程仓库 git remote rename upstream icyleaf # 删除本地远程仓库 git remote rm icyleaf
  11. 推送至服务器 # 推送本地 master 分⽀支到远程 origin 上⾯面 git push -u

    origin master # 删除远程 origin 上⾯面提交的临时分⽀支:issue3 git push origin :issue3 git push [远程名] [本地分⽀支]:[远程分⽀支] -u 的作用是让 git 知道默认 push 的 remote 和 branch ,方便 默认 pull 。
  12. 跟踪分支(拉取代码) # 更新远程仓库的最新代码索引 git fetch origin git merge origin/master #

    直接更新病合并最新的分⽀支代码 git pull orign master 默认可以使⽤用 git pull 代替。
  13. 合并代码 git checkout -b hotfix # 开始修复代码 git checkout master

    # 合并 hotfix 的代码 git merge --no-ff hotfix # 如果没有冲突⽂文件会以 Fast forward 的⽅方式顺利合并(--no-ff 的 作⽤用是保证合并是有多分⽀支合并的记录,默认是 --ff,直接把 commit 合并到⼀一条主线上) # 如果发⽣生冲突,使⽤用 git status 查看冲突的⽂文件(类似 SVN 状况) # 解决后,使⽤用 git add 标记已完成,并 git commit 提交冲突⽂文件
  14. 衍合(变基)分支 # 假如想修改最近三个的 commit git rebase -i HEAD~3 # 回滚到某个特性的

    commit pick f7f3f6d changed my name a bit edit 310154e updated README formatting and added blame pick a5f4a0d added cat-file # Rebase 710f0f8..a5f4a0d onto 710f0f8 # # Commands: # p, pick = use commit # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # # If you remove a line here THAT COMMIT WILL BE LOST. # However, if you remove everything, the rebase will be aborted. # 对于 edit 的 commit 进⾏行任意操作 # 修改完毕(git commit)之后没问题了继续衍合当前 commit git rebase --continue # 如果当前 edit 不需要编辑了可以跳过 git rebase --skip
  15. 标签 # 查看当前所有标签 git tag # 标记当前分⽀支为 v1.0 版本作为归档 git

    tag -a v1.0 # 添加带备注的标签 git tag -a v1.2.1225 -m ‘圣诞节特别版本’ # 把历史特定 commit 标记标签 git tag -a v1.1 1d2x33 # 查找 v1.0 版本下有多少标记的⼩小版本 git tag -l ‘v1.*’ # 分享标签 git push origin v1.0
  16. services n Github (git) (public free) n Bitbucket (hg/git) (public

    & private all free) n Google Code (svn/hg/git) (public only)
  17. commit & push git add README git commit -m “first

    commit” echo “hello world” > README git commit -am “updated README” echo “missing content” >> README git commit -am “completed README” git push origin master 本地多次提交完成功能, 最后统一提交到服务器。
  18. 子模块 # 假设有⼀一个 blog 的项⺫⽬目,所有外部模块都放在 vendor 下⾯面。 # ⾸首先添加⼀一个 twig

    模板引擎的⼦子模块 git submodule add https://github.com/fabpot/Twig.git vendor/twig # 添加了那么多⼦子模块,我如何全部更新呢? git submodule foreach git pull # clone 别⼈人带⼦子模块的,我怎么获取他们的代码呢? git submodule init && git submodule update git submodule update --init # 我怎么知道⼀一个项⺫⽬目都有哪些⼦子模块? cat .gitmodules
  19. 最后的后悔药 git reset --hard 3d2x9 # 发现回滚错了,⽽而之前的 HEAD 已经没有了!怎么办! git

    reflog b8981f0 HEAD@{0}: reset: moving to b8981f0 3e15a82 HEAD@{1}: commit: Modified README 07e0183 HEAD@{2}: commit (initial): init git reset --hard 3e15a82 30 天内有效,否则 git 会 做垃圾处理掉
  20. git-svn # Clone 指定⺫⽬目录下,相依起始版本号的代码 git svn clone https://intra.leju.com/svn/mobile -s -r524:HEAD

    # 其他操作(如,add,commit,log 等)全部使⽤用 git 本⾝身的命令 # 提交代码到 svn 服务器 git svn dcommit # 从 svn 服务器获取最新代码 git svn rebase
  21. 继续利用分支特性 创建两个 remote(production)/test 和两个 branch(master/ develop) git remote add production

    http://pro.xxx.com/pro.git git remote add test http://test.xxx.com/pro.git 根据分⽀支 push 到不同的 remote git push test develop git fetch production git merge production/master master (develop)$ git merge master 若有冲突合并之后在测试⽆无误,没问题了合并到 master 推送到产品机器 (master)$ git merge devlop git push production master
  22. Q&A