Git Start

Git是一个开源的分布式版本控制系统。用于敏捷高效地处理任何或小或大的项目,除了Git还有CVS,SVN等版本管理系统。

安装Git

想学习或者使用git,第一步需要安装它。

1
2
3
4
5
6
7
8
#Mac环境下:使用homebrew安装git
$ brew install git
#Linux(centos、red hat):
$ yum install git
#Linux(ubuntu、debian)
$ apt-get install git
#查看安装版本
$ git --version

简单的配置Git

开始使用Git,首先需要配置你的用户名和邮箱。

1
2
3
4
5
6
#配置用户名
$ git config --global user.name 'your name'
#配置邮箱
$ git config --global user.email 'example@domain.com'
#查看git的配置信息
$ git config --list

清除git config配置

1
2
$ git config --unset --global user.name
$ git config --unset --global user.email

git config的作用域

1
2
3
4
5
6
#只对当前仓库有效
$ git config --local
#当前用户所有仓库有效
$ git config --global
#系统所有用户有效
$ git config --system

git config优先级:local > global > system

初始化(创建)git仓库
1
2
3
4
#进入项目目录
$ cd your_project
#仓库初始化
$ git init

创建完仓库后,项目目录下会有个.git的隐藏文件夹,进去看下吧。

1
2
3
4
5
6
$ cd .git
#查看config文件,你会发现你配置的user信息
$ cat config
#查看HEAD文件,指向ref的分支
$ cat HEAD
ref: refs/heads/master

使用Git进行提交工作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#在工作区新增编辑一个文件
$ vim test_file
#查看git当前状态,有一个文件未添加到暂存区
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)

test_file

nothing added to commit but untracked files present (use "git add" to track)
#将test_file加入暂存区
$ git add test_file
#将test_file提交到版本库,-m是提交描述
$ git commit -m 'add test file'
#再次查看状态会发现工作区是干净的状态
$ git status
On branch master
nothing to commit, working tree clean
#查看日志
$ git log

这就是git一个简单的提交工作流程。