Git 是一个免费开源的分布式版本控制工具,也是目前最流行的分布式版本控制系统,可以很好的减轻开发人员及开源项目对于管理分支代码的压力。本篇将贴出前期自己整理的笔记导图,也会在后面示例演练中说明每个操作的具体含义以及图解,方便大家及自我查阅, 有不正确的地方欢迎拍砖。


Git 分布式版本控制 实战

1. 基本概念及术语

  • 工作区: 表示当前的工作目录

  • 暂存区:存放到stage area(git add file/test.py)

  • 版本库:存在master代码库中(git commit )

Git 分布式版本控制 实战


M的含义:

第一列的M表示“缓存区“有改动

第二列的M表示“工作区”有改动


2. Git 安装及环境准备

[email protected]:~$ sudo install git  #安装git软件包
[email protected]:~$ mkdir git
[email protected]:~$ cd git ; mkdir project #创建两个目录方便后面示例说明
[email protected]:~/git/project$ ls -lh
-rw-rw-r-- 1 junluobj junluobj  5 Aug  2 07:18 README.txt
-rw-rw-r-- 1 junluobj junluobj 99 Aug  2 07:21 test.py


4. Git 全局配置

在项目或开发小组中都会有很多开发人员提交代码, 为了让代码日后查询以及知道是谁提交的,强烈建议在项目组中在自己的工作环境下配置自己的git全局配置,这样自己的代码提交了什么功能性的代码, 什么时候提交的,都可以从记录中查询, 也知道team成员提交的代码情况, 好了下见下面示例:

[email protected]:~/git/project$ git config --global user.name Rocky  # 配置用户名
[email protected]:~/git/project$ git config --global user.email [email protected] #配置用户邮箱
[email protected]:~/git/project$ git config --global color.ui true   #输出信息可颜色显示
[email protected]:~/git/project$ git config --list  #列出所有的配置
user.name=Rocky
[email protected]
color.ui=true

git 全局配置设置信息默认会保存在当前家目录的.gitconfig 文件中, 也可以直接的通过对此文件进行修改保存, 同时通过 git config --list 可以立即看到修改的信息。

[email protected]:~$ cat .gitconfig
[user]
        name = Rocky
        email = [email protected]
[color]
        ui = true

另外,若想删除user.name 等设置则可通过下面的指令来实现;也可手动修改.gitconfig 文件

[email protected]:~/git/project$ git config --unset --global user.name

5. 创建repository

创建repository 有两种方式, 一种是本地初始化,一种是从远程克隆,见示例(可从上导图查询)

#在本地创建一个空的repo,也将在project 目录中创建一个.git 目录
[email protected]:~/git/project$ git init   
Initialized empty Git repository in /home/junluobj/git/project/.git/
[email protected]:~/git/project$ ls -A .git/
branches  config  description  HEAD  hooks  info  objects  refs

#通过远程克隆一个repo
[email protected]:~/git$ git clone https://github.com/openstack-dev/devstack.git

6. 添加及提交文件

在project 有一个test.py 和 README.txt 文件,现在我对test.py 进行修改,然后通过git  status 查看repository 状态; 红色标记部分表示还未提交或未通过git命令进行登记, 同时注意此处的状态。

Git 分布式版本控制 实战

#添加文件,这里也可以通过git add --all 一次性添加文件
[email protected]:~/git/project$ git add test.py
[email protected]:~/git/project$ git add README.txt
[email protected]:~/git/project$ git status

Git 分布式版本控制 实战

#提交文件

[email protected]:~/git/project$ git commit -m "first commit code"
[email protected]:~/git/project$ git status
# On branch master
nothing to commit (working directory clean) # working directory 见上面的图解分析

7. 查看git状态

在查看git状态前,我先用个简单的示例说下 工作区: 表示当前的工作目录 暂存区:存放到stage area(git add file/test.py) 版本库:存在master代码库中(git commit )都是用来做什么的: 如下图


Git 分布式版本控制 实战

通常我们会在自己本地working Directory中修改或新增代码,每当我们修改完代码后, 通过git add File 将文件添加到staging area后就可以对文件进行提交了,通过git commit File  -m 就可将文件提交到git repository 中,也就是我们最终存放的代码的地方。经过图解,更加容易理解三个区层的关系

下面通过示例来进一部阐述git 状态的细节:

[email protected]:~/git/project$ git status # 查看git状态
# On branch master
nothing to commit (working directory clean)

我们可以看到当前的git 状态 working directroy clean, 也就表示 Staging area (缓存区)、Working Directory及git repository的内容都是一样的

同时,为了更好区别其状态的细节,在working directory 下增加新的文件及修改代码,再次查看状态:

Git 分布式版本控制 实战

test.py  第1列 为空, 第二列有个flag 标记为M

testfile  第1列为A, 第二列为空,  这些是什么含义呢?

通常每个文件都有两个flag 位;test.py 第一个标志位为空,是表示staging area 没有发生变化;第二M 表示working directory 发生了变化; 而testfile的第一个flag A 表示stage area添加了新的变化,而后面空是代表working directory 没有发生变化,所以为空白;

Git 分布式版本控制 实战

再次修改test.py后,再次查看状态,发现test.py 后面多了个flag位M; 则表示working directory 发生了变化,第一个是表示Staging area 发生变化,再次把新修改的文件添加到staging area 中,提交代码。

Git 分布式版本控制 实战

再次git status -s 查看git 状态, 没有任何显示表示三个区层的内容都是一致的,而-s 选项是简短显示git状态信息。

8.查看文件差别

从下图可以看到当前的test.py 与git repository 文件的差异

Git 分布式版本控制 实战

9. 撤销操作:

当我们在操作时, 不小心提交不正确的代码, 又想撤销其操作的话,以下有三种方式来撤销

第一种:git reset test.py (从git repository 中取出来覆盖其staging area 中的文件)

第二种:git checkout test.py (从staging area 中来覆盖当前working directory 中文件)

第三种:git checkout HEAD test.py(从git repository 中来覆盖当前working directoy 中的文件)


10. 创建及删除分支 

Git 分布式版本控制 实战

Git 分布式版本控制 实战

从上图中可以看到:

[email protected]:~/git/project$ git branch # 列出当前所有的branch
[email protected]:~/git/project$ git branch test-branch # 创建test-branch
[email protected]:~/git/project$ git checkout test-branch #切换到新建的branch
M       test.py
Switched to branch 'test-branch'
#切换到master 分支, 删除新建的分支
[email protected]:~/git/project$ git checkout master
M       test.py
Switched to branch 'master'
[email protected]:~/git/project$ git branch -d test-branch
Deleted branch test-branch (was 20b0d2e).


11. 查看git 提交的记录:

Git 分布式版本控制 实战

Git 还有很多git 常用命令,如git merg ,rm 、mv 等等 这里就不一一去截图画图来显示了,直接查看它的帮助信息即可,里面大部分的命令在工作都会使用到。


12. Git 常用命令

#常用命令
[email protected]:~$ git --help

   add        Add file contents to the index
   bisect     Find by binary search the change that introduced a bug
   branch     List, create, or delete branches
   checkout   Checkout a branch or paths to the working tree
   clone      Clone a repository into a new directory
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   fetch      Download objects and refs from another repository
   grep       Print lines matching a pattern
   init       Create an empty git repository or reinitialize an existing one
   log        Show commit logs
   merge      Join two or more development histories together
   mv         Move or rename a file, a directory, or a symlink
   pull       Fetch from and merge with another repository or a local branch
   push       Update remote refs along with associated objects
   rebase     Forward-port local commits to the updated upstream head
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index
   show       Show various types of objects
   status     Show the working tree status
   tag        Create, list, delete or verify a tag object signed with GPG


13. Git-项目组git工作流程:


#1.从远程服务器(上游)克隆代码库:
$ git clone -b project-code ssh://[email protected]/IP/git/ofm.git project-code

#2.进入到当前的工作目录,并根据项目的组件或功能或bug 来创建本地的brach
$ cd project-code
$ git checkout -b story-123 #创建并切换到新建的branch

#3.确保你是在当前新建的branch中,在其下面修改代码
$ git status or git status -s
$ vim some-code / vim files

#4.添加代码
$ git add some-code/files

#5. 提交代码
$ git commit -m "coments xxxxx"

#6.如果你的代码需要他人帮你review, 可创建一个path 发送给他
$ git format-patch -1
0001-second-commit.patch

#7.在将你的代码推向到你的项目组的代码库中,你需要确保代码跟同事之间没有冲突
$ git checkout project-code
$ git pull
$ git rebase project-code story-123
# 若有冲突,解决其冲突的部分,并在此添加冲突的文件
$ git add conf/conflict.file
$ git rebase --continue

#8.切换到项目的分支并自建的分支内容
$ git checkout project-code
$ git merg story-123

#9. 将代码推向到项目组代码库中
$ git push

#10.最后删除自建的分支
$ git branch -d story-123


14. Git 参考资料:

    http://www.git-scm.com/book
    http://gitref.org/
    http://en.wikipedia.org/wiki/Git_%28software%29
    https://github.com/