Git基本工作流程-Git详解(2)

Git 4种对象

  • blob (文本文件,二进制文件,链接文件)
  • tree (目录)
  • commit (历史提交)
  • tag (指向一个固定的历史提交)
graph LR b1[id:c376d
blob] b2[id:6a3cb
blob] b3[id:4a62c
blob] tree[id:825c7
tree:size
blob:c376d
blob:6a3cb
blob:4a62c] commit[id:7562d
commit:size
tree:825c7
author:jkxy
first commit] tag[id:12ch6
tag:size
object:7562d
type:commit
tag:V0.0.1
tagger:jkxy
tag for first commit ] tag --> commit commit --> tree tree --> b1 tree --> b2 tree --> b3

Git 仓库的获取

  • git init
  • git clone

Git 仓库分2种

  • bare: (裸仓库) 不带工作区.git目录,常用在服务器上方便协作
  • non-bare:带 .git 目录
1
2
$ git init #初始化当前目录为仓库
$ git init non_bare_repo #会新建目录并初始化

创建一个裸仓库

1
$ git init --bare git_bare_repo #无.git目录

对一个已有的文件目录,纳入git管理

1
2
3
$ mkdir git_init_repo # 假设已经存在的目录
$ cd git_init_repo
$ git init

使用git clone 获取仓库

1
$ git clone git_bare_repo git _clone_repo

Git 工作区、暂存区、历史区

graph TD wd[working directory
工作区] sa[staging direct
暂存区] hr[history repository
历史区] wd --> |stage files| sa sa --> |commit| hr hr --> |check out the project| wd

相关命令

1
2
3
4
5
6
$ git add
$ git commit
$ git status
$ git rm
$ git mv
6 git gitignore

例子

1
2
3
4
5
6
7
$ git init git_basics
$ cd git_basics
$ touch a
$ touch b
$ git add a b
$ git commit -m "Initial commit"
$ vim a #修改a ,加入文字

1
2
3
4
5
6
7
8
9
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: a
no changes added to commit (use "git add" and/or "git commit -a")

git commit -a 参数 ,这里等价于 $ git add a

1
2
3
4
5
$ git commit -a -m 'modify a'
On branch master
Changes not staged for commit:
modified: a
no changes added to commit

1
$ git rm a # 会删除工作区和暂存区的文件,但并commit

因为没有提交,可以用命令恢复

1
2
$ git reset HEAD a
$ git checkout a

只删除暂存区的文件,不删工作区

1
$ git rm --cached a

重命名

1
2
3
4
5
6
$ git mv c d
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: c -> d

git ingnore

.gitignore 配置文件用于配置不需要加入版本管理的文件

配置语法:

  • 以斜杠“/”开头表示目录;
  • 以星号“*”通配多个字符;
  • 以问号“?”通配单个字符
  • 以方括号“[]”包含单个字符的匹配列表;
  • 以叹号“!”表示不忽略(跟踪)匹配到的文件或目录;

此外,git 对于 .ignore 配置文件是按行从上到下进行规则匹配的,意味着如果前面的规则匹配的范围更大,则后面的规则将不会生效;

示例说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[wangshibo@gerrit-server hqsb_android]$ cat .gitignore
#Built application files
*.apk
*.ap_
*.[oa] #根目录下以o或a结尾排除
*.pyc
!foo.pyc #foo.pyc不忽略
**/res #任意目录下的res
# Files for the Dalvik VM
*.dex
# Java class files
*.class

Git 暂存区

维护一个虚拟的数据结构,代表了将要被提交的下一次数据的整体