SlideShare a Scribd company logo
Git 入门(客户端版)

   技术部 - kim
作者及起源




2005 年,开发 BitKeeper 的商业公司同 Linux 内核开源社区的合
作关系结束,他们收回了免费使用 BitKeeper 的权力。这就迫使
Linux 开源社区(特别是 Linux 的缔造者 Linus Torvalds )不得不
吸取教训,只有开发一套属于自己的版本控制系统才不至于重蹈
覆辙。
传统 CVCS

集中化的版本控制系统( Centralized Version Control Systems,简称 CVCS )

例如:CVS, Subversion

优点:管理容易

缺点:宕机无法更新,磁盘坏了丢失数据
DVCS

分布式版本控制系统( Distributed Version Control System,简称 DVCS )

例如:Git, Mercurial

优点:文件快照+原始代码仓库的完整镜像,每一次的提取操作,实际上都是一
次对代码仓库的完整备份

缺点:入门及过渡稍有难度(这也算?)
CVCS   DVCS
SVN vs Git
Git 本地仓库状态
Git 安装

源代码(翻墙) :http://git-scm.com/download (Git-软件配置管理)

用 yum 安装: $ yum install git-core

用 apt-get 安装: $ apt-get instal git-core

Windows下: http://code.google.com/p/msysgit
          http://code.google.com/p/tortoisegit/
TortoiseGit
Git 配置

Linux 配置文件: /etc/gitconfig

Windows 配置文件: C:Documents and Settings$USER.gitconfig

初始化配置:
$ git config --global user.name “kim"
$ git config --global user.email xqpmjh@gmail.com

演示:
$ git config --list
Git 配置
本地创建仓库
    $ git init




 观察一下 .git 目录
本地创建仓库
Git 默认是禁止 push 操作的,所以需要编辑一下 local 的 .gitconfig 文件
本地创建仓库
加上 dengCurrentBranch = ignore
克隆仓库
$ git clone --progress -v "D:wwwrep1" "D:wwwgit1”
Git 文件的4种状态
添加文件
$ git add abc.txt
忽略文件
指令稍微复杂,建议使用客户端
查看更新
 $ git diff
提交更新
        $ git commit –m “hello world”




默认分支为 master ,多人协作开发可以为每个人开一个分支。
   每一次运行提交操作,都是对你项目作一次快照。
删除文件
     $ git rm abc.txt




同时删除版本库以及文件夹里面的文件
文件改名
$ git mv abc.txt cba.txt
查看历史
  $ git log --pretty




不用客户端的话根本没法看
取消暂存
    $ git reset HEAD abc.txt




即撤消了 add 操作,蓝色十字变回蓝色问号
撤销修改
      $ git checkout -- 111.txt


撤销前                               撤销后
推送
    $ git push origin master




push 的是之前 commit 到本地暂存的快照
拉取
    $ git pull origin master




pull 的是仓库中没有同步到本地的快照版本
打标签
      $ git tag -a v1.0 -m 'my version 1.0'




       PS:比如发布版本的时候就打上一个吧
如果说 branch 是一对一的关系,那么 tag 则是一对多的关系
查看标签
$ git show v1.0




show log 一目了然
推送标签
     $ git push origin --tags




 push 的时候选中 Include Tags
单推一个 tag 用 $ git push origin v1.0
标签的用途

1. 标记重要事件或版本

2. 方便搜索和查看

3. 分支检出(checkout)与分支合并(merge)
文件标注
$ git blame abc.txt




犯罪历史一目了然
Git 分支



分支,杀手级应用
版本存储方式




每次提交就是提交一个 commit 对象
Snapshot 之间关系




一个 snapshot 包括当前 commit 对象,及其 parents
Master 分支
 分支:指向 commit 对象的指针




每次提交,默认分支指针都会自动向前移动
创建分支
   $ git branch testing




创建一个新的分支指针 testing
创建分支
$ git branch -v
切换分支

              或者




PS:Git 里面 checkout 的意思跟 SVN 有所不同
HEAD 指针
      $ git checkout testing
      $ git status




切换分支其实就是把 HEAD 指针指向别的分支指针
       指针的指针的意思
分支修改
  $ git checkout testing
  $ vim branch_changes.log
  $ git commit -a -m 'made a change'




每次提交后 HEAD 随着分支一起向前移动
分支修改
$ git checkout master
$ vim branch_changes_2.log
$ git commit -a -m 'made other changes'




   这种情况也可以称之为“分叉”
分支对比

$ git checkout master   $ git checkout testing
分支的本质


Git 中的分支,实际上仅是一个包含所指对象“校验和”(40
        个字符长度 SHA-1 字串)的文件




          分支切换非常廉价
合并分支
$ git checkout master
$ git merge testing
删除分支
$ git branch -d testing




PS:已合并的分支可以看情况保留一段时间,而不是第一时间删除
使用分支应注意

1. 切忌在多个分支上频繁切换

2. 最好保持独立分支,大于等于2个分支时,尽快 merge

3. 使用分支的最好场景还是发布新版本的时候

4. Git 玩分支太廉价,容易走火入魔
分支流水线
本地分支
origin/master 的本质是:Git 自动为你命名的指向原始仓库的指针
修改本地分支
本地修改只会移动 master 指针,而不会移动 origin/master 指针,直到你执行
              fetch ,更新 origin 的状态为止
远程分支

远程分支(remote branch)是对远程仓库状态的索引




比如,我们先来创建一个名为 myframework 的仓库
分享分支
$ git clone --progress -v "D:wwwmyframework" "D:wwwmfr”
$ cd D:wwwmfr
$ git branch sharing
$ git push --progress "origin" sharing




    PS:前提当然是你有 myframework 仓库的写权限
添加远程仓库
$ cd D:/www/rep1
$ git remote add myframework D:wwwmyframework
同步远程仓库
$ git fetch myframework
跟踪分支
$ git pull "myframework“ sharing
归并
如果想把代码合并到你的项目,可以使用 merge
   $ git merge myframework/sharing




  也可以在本地新建一个分支来指向远程分支
$ git checkout –b localmfr myframework/sharing
删除远程分支
      $ git push myframework :sharing




这句语句可以这么理解:把本地分支置空,再同步到远程仓库
分支衍合(rebase)

    不建议使用
子模块


子模块允许你将一个 Git 仓库当作另外一个 Git 仓库的子目录。

这允许你克隆另外一个仓库到你的项目中,并且保持你的提交相对独立。
添加子模块
$ git submodule add -- "D:/www/myframework" ""
更新子模块
$ vim myframework/api.php
更新子模块
    $ cd D:/www/git1/myframework
    $ git branch -v




    $ cd D:/www/myframework
    $ git branch -v




再给 branch_of_rep1 添加一个新文件,如 api2.php
更新子模块
$ git submodule update
$ cd D:/www/git1/myframework
$ git pull origin "origin" branch_of_rep1




子模块的修改跟主项目是相对独立的
其它特性

1. Git 挂钩

2. 和 SVN 一起使用?

3. 储藏(git stash 命令)

4. 子树合并(git tree 命令)

5. Git 支持4种协议:file, ssh, http(s), git

6. Git 导入 SVN(git svn 命令)
常见问题
1. 慎用 clean up
常见问题
2. Push 前先进行 Pull
常见问题
3. 保持换行符不变

$ git config --global core.autocrlf false
常见问题
4. 不小心删除了文件怎么办
常见问题
5. 和 TortoiseSVN 一起共用,图标会冲突
常见问题
6. Git 的两大好处

1) 不用每次都同步到服务器,比如今天的活儿就今天
   commit ,项目完了再一次过 push

2) 只有一个 .git 文件夹,清爽
常见问题
7. 如何配置 putty

tortoiseGit -> setting -> Network -> ssh client ->
$GitDirTortoiseGitbinTortoisePlink.exe
常见问题
8. 配置了 putty 作为 ssh 客户端之后?

运行 GitDirTortoiseGitbinpageant.exe ,Add Key 加入
  你的 ssh privete key file 文件,并输入密码
常见问题
9. 如何学习使用 Git

1) 《Pro Git》

2) 反复操作
谢谢!

More Related Content

PDF
Git 簡介(古時候的簡報備份)
PDF
Git 入门实战
PPTX
Git 入門與實作
PDF
Git and Github basic with SourceTree
PDF
版本控制 使用Git & git hub
PPTX
Git & Sourcetree 介紹
PDF
寫給大家的 Git 教學
PDF
Git與source tree 基礎教學
Git 簡介(古時候的簡報備份)
Git 入门实战
Git 入門與實作
Git and Github basic with SourceTree
版本控制 使用Git & git hub
Git & Sourcetree 介紹
寫給大家的 Git 教學
Git與source tree 基礎教學

What's hot (20)

PPTX
工程師必備第一工具 - Git
PDF
Introduction to git
PDF
初心者 Git 上手攻略
PPTX
Git基礎介紹
PPTX
簡介 GitHub 平台
PDF
Xcode 的 git 版本管理
PPTX
Visual Studio 2015 與 Git 開發實戰
PPTX
Git内部培训文档
PDF
連哈秋都懂的Git教學
PPTX
Git前世今生
PPTX
Git & git hub v1.2
PDF
Submodule && subtree
PPTX
Mercurial簡介與教學
PDF
Git tutorial
PPTX
git merge 與 rebase 的觀念與實務應用
PDF
A successful git branching model 導讀
PPTX
Git 使用介绍
PDF
Git+使用教程
PDF
Git Tutorial 教學
PDF
Linux Container Introduction
工程師必備第一工具 - Git
Introduction to git
初心者 Git 上手攻略
Git基礎介紹
簡介 GitHub 平台
Xcode 的 git 版本管理
Visual Studio 2015 與 Git 開發實戰
Git内部培训文档
連哈秋都懂的Git教學
Git前世今生
Git & git hub v1.2
Submodule && subtree
Mercurial簡介與教學
Git tutorial
git merge 與 rebase 的觀念與實務應用
A successful git branching model 導讀
Git 使用介绍
Git+使用教程
Git Tutorial 教學
Linux Container Introduction
Ad

Viewers also liked (20)

PPT
Murphy Portfolio 8 18 09
PPT
Edm E Drive Orlando Conf Mar2009 V13
PPT
Melink Energy Savings Solutions
PPT
Sites Of Ancient Rome
PDF
PPS
Double Vision
PPTX
Limited Warranty
PPTX
Testing OSGi-based Applications with DA-Testing
PDF
Q3 2009
 
PPTX
Successorize!
PPT
2009 Sw Media Kit
XLS
Copy Of Pricelist
PPT
WWI Comes To An End
PPT
Perceptions Of Hispanic Offenders Toward Reentry Programs
DOC
Clothes
PPTX
Lessons In Leadership
PPT
Eelex 2009
PDF
Eco Efficiency & Corporate Reputation Chapter Guerin Springer 2009
PPT
MongoDB Basics and Tutorial
PPS
Farmeri - Alijansa na baxu
Murphy Portfolio 8 18 09
Edm E Drive Orlando Conf Mar2009 V13
Melink Energy Savings Solutions
Sites Of Ancient Rome
Double Vision
Limited Warranty
Testing OSGi-based Applications with DA-Testing
Q3 2009
 
Successorize!
2009 Sw Media Kit
Copy Of Pricelist
WWI Comes To An End
Perceptions Of Hispanic Offenders Toward Reentry Programs
Clothes
Lessons In Leadership
Eelex 2009
Eco Efficiency & Corporate Reputation Chapter Guerin Springer 2009
MongoDB Basics and Tutorial
Farmeri - Alijansa na baxu
Ad

Similar to Git Essence Tutorial (20)

PDF
Git in a nutshell
ODP
Git 程式碼版本控制軟體介紹
ODP
Git 教學
PPT
Git share
PDF
PDF
Git 版本控制系統 -- 從微觀到宏觀
PPT
Git 超簡單學習懶人包(軟體程式版本控管系統)
PDF
First meetingwithgit
PPTX
Git使用入门
PDF
Git Tutorial
PPTX
Git and git hub
PDF
Git入门与实践
PPT
Git flow
PPTX
Git 实战
PPT
Learn git
PPTX
Git原理与实战 201607
PPT
Github简介及实用入门
PPTX
Git introduction
PPTX
20170510 git 懶人包
PPTX
Git & git flow
Git in a nutshell
Git 程式碼版本控制軟體介紹
Git 教學
Git share
Git 版本控制系統 -- 從微觀到宏觀
Git 超簡單學習懶人包(軟體程式版本控管系統)
First meetingwithgit
Git使用入门
Git Tutorial
Git and git hub
Git入门与实践
Git flow
Git 实战
Learn git
Git原理与实战 201607
Github简介及实用入门
Git introduction
20170510 git 懶人包
Git & git flow

More from Ho Kim (14)

PDF
解决Lvs上行丢包的过程和收获
PDF
40 Powerful Shortcuts of Xcode 6.x
PPTX
Project Management Using Redmine
PPTX
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
PPTX
Web Caching Architecture and Design
PPT
Lua 30+ Programming Skills and 20+ Optimization Tips
PPTX
人人-56 账号拆分项目总结
PPTX
OpenResty/Lua Practical Experience
PPTX
JavaScript 80+ Programming and Optimization Skills
PPT
Character Encoding and Database Transcoding Project
PPT
Video Upload Architecture of 56.com
PPT
PHP Optimization for Millions Visits Level
PPTX
Comment System of 56.com
PPT
PHP Coding Standard and 50+ Programming Skills
解决Lvs上行丢包的过程和收获
40 Powerful Shortcuts of Xcode 6.x
Project Management Using Redmine
OpenResty/Lua 70+ Advanced Programming Skills and Optimization tips
Web Caching Architecture and Design
Lua 30+ Programming Skills and 20+ Optimization Tips
人人-56 账号拆分项目总结
OpenResty/Lua Practical Experience
JavaScript 80+ Programming and Optimization Skills
Character Encoding and Database Transcoding Project
Video Upload Architecture of 56.com
PHP Optimization for Millions Visits Level
Comment System of 56.com
PHP Coding Standard and 50+ Programming Skills

Git Essence Tutorial