目录

Git 起步

Git 是一种在全球范围都广受欢迎的版本控制系统。在开发过程中,为了跟踪代码,文档,项目等信息中的变化,版本控制变得前所未有的重要。但跟踪变化远远不能满足现代软件开发行业的协同需求,基于 Git 的 Workflow 满足了合作编程的需求,让开发从此变得更加高效和有趣。相比集中式版本控制系统如 SVN ,分布式版本控制系统 Git 拥有更强大的分支管理与合并能力,支持离线开发,并良好地保留了提交过程,让您和您的团队在开发过程中如虎添翼。

概述

设计初衷

  • 为速度而生
  • 简单的设计
  • 对非线性开发模式的强力支持(允许上千个并行开发的分支)
  • 完全分布式
  • 有能力高效管理类似 Linux 内核一样的超大规模的项目(速度和数量)

一些特性

  • 优越的存储能力
  • 直接记录快照,而非差异比较
  • 离线操作,几乎所有操作都是本地执行
  • 时刻保持数据完整性
  • 多数操作仅添加数据
  • 开源

git 中文件的三种状态:工作目录,暂存区域和本地仓库:

https://inotes.oss-cn-beijing.aliyuncs.com/git/201812/git-three-status.png

工作流程

  • 在工作目录中修改某些文件
  • 对修改后的文件进行快照,然后保存到暂存区域
  • 提交更新,将保存在暂存区域的文件快照永久转储到 git 目录中

关于版本控制

集中化的版本控制系统

CVCS - Centralized Version Control Systems

https://inotes.oss-cn-beijing.aliyuncs.com/git/201812/centralized-vcs.png

分布式版本控制系统

DVCS - Distributed Version Control System

https://inotes.oss-cn-beijing.aliyuncs.com/git/201812/distributed-vcs.png

安装

Linux 安装

安装依赖

1
2
3
4
5
// CentOS
$ sudo yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel

// Ubuntu
$ sudo apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev

手动编译安装

去这里下载 git

1
2
3
4
5
// 解压后进行编译安装
$ tar -zxf v2.16.1.tar.gz
$ cd v2.16.1
$ make prefix=/usr/local all
$ sudo make prefix=/usr/local install

包管理器安装

1
2
3
4
5
// CentOS
$ yum install git-core

// Ubuntu
$ apt-get install git

Mac 安装

1
2
// 使用 brew 安装 git
$ brew install git

Windows 安装

We bring the awesome Git SCM to Windows

配置

三种配置

  • 全局配置:当前用户的所有仓库有效,存储配置的文件为 ~/.gitconfig
1
2
3
4
// 查看全局配置
$ git config --global -l
$ git config --list --global
  • 局部配置:当前用户的某个仓库有效,存储配置的文件为 .git/config
1
2
3
4
// 查看局部配置
$ git config --local -l
$ git config --list --local
  • 系统配置:系统所有用户有效,存储配置的文件为 /etc/gitconfig
1
2
3
4
// 查看系统配置
$ git config --system -l
$ git config --list --system

用户信息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// 配置全局的用户名称
$ git config --global user.name "Your Name"

// 配置全局的邮箱地址
$ git config --global user.email "Your Email"

// 取消全局配置的用户名称
$ git config --global --unset user.name

// 取消全局配置的邮箱地址
$ git config --global --unset user.email

// 配置某个仓库的用户名称
$ git config --local user.name "Your Name"

// 配置某个仓库的邮箱地址
$ git config --local user.email "Your Email"

文本编辑器

Git Commit Problem: "error: There was a problem with the editor 'vi'"

1
2
// 配置 vim 为 git 默认的编辑器,为满足在 git 中输入一些额外消息的时候,自动调用一个外部文本编辑器的需要
$ git config --global core.editor $(which vim)

差异分析工具

1
2
// 配置解决合并冲突时使用哪种差异分析工具
$ git config --global merge.tool vimdiff

换行符配置

操作系统对回车 CR 和换行 LF 定义是有差异的。

AutoCRLF

1
2
3
4
5
6
7
8
// 提交时转换为 LF,检出时转换为 CRLF
$ git config --global core.autocrlf true

// 提交时转换为 LF,检出时不转换
$ git config --global core.autocrlf input

// 提交检出均不转换
$ git config --global core.autocrlf false

SafeCRLF

1
2
3
4
5
6
7
8
// 拒绝提交包含混合换行符的文件
$ git config --global core.safecrlf true

// 允许提交包含混合换行符的文件
$ git config --global core.safecrlf false

// 提交包含混合换行符的文件时给出警告
$ git config --global core.safecrlf warn

修正空白符

git 预先设置了一些选项来探测和修正空白问题

  • 默认打开的 2 个选项是 trailing-spacespace-before-tabtrailing-space 会查找每行结尾的空格,space-before-tab 会查找每行开头的制表符前的空格
  • 默认关闭的 2 个选项是 indent-with-non-tabcr-at-eolindent-with-non-tab 会查找 8 个以上空格(非制表符)开头的行,cr-at-eolgit 知道行尾回车符是合法的
1
2
// 全局配置修正空白符
$ git config --global core.whitespace trailing-space,space-before-tab

配置快捷操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// 为 git 命令创建一个快捷方式
$ git config --global alias.<alias-name> <git-command>

// 检出快捷方式设置
$ git config --global alias.co checkout

// 提交快捷方式设置
$ git config --global alias.ci commit

// 状态快捷方式设置
$ git config --global alias.st status

// 分支快捷方式设置
$ git config --global alias.br branch

配置在 Windows 下 ASCII 字符的文件名正确显示

git 乱码解决方案汇总

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// 一个显示乱码的实例
$ git status
On branch master
Your branch is up to date with 'gitee/master'.

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)

   deleted:    "xxx-\345\210\235\350\257\206.xx" // 中文文件名显示不正常

Untracked files:
  (use "git add <file>..." to include in what will be committed)

// 全局配置以解决 Windows Git Bash、Linux 下的中文转码问题
$ git config --global core.quotepath false

配置对大小写敏感

1
2
// 全局配置对大小写敏感,保持 Mac/Win/Linux 一致性
$ git config --global core.ignorecase false

配置菜色显示

1
2
// 全局配置菜色显示
$ git config --global color.ui auto

以 https 方式进行连接时,记住密码

为当前仓库设置记住密码,设置后,只要在推送一次,以后就不需要用户名和密码了

1
2
3
4
5
6
7
8
// 设置保存密码到硬盘,如果是 macOS,选项可以改为 osxkeychain
$ git config credential.helper store

// 设置保存密码的超时时间
$ git config credential.helper 'cache --timeout=36000'

// 查看密码文件(以明文的方式保存的)
$ cat ~/.git-credentials

查看配置信息

1
2
3
$ git config --list
$ git config -l

解决目录卡顿问题

主要解决在 zsh 下,在 git 版本控制的目录输入命令执行缓慢的问题。

1
2
// 不读取文件变化信息(针对当前用户当前项目)
$ git config --add oh-my-zsh.hide-dirty 1

https://inotes.oss-cn-beijing.aliyuncs.com/git/201812/git-zsh-dirty.png

1
2
// 不读取任何 git 信息(针对当前用户所有项目)
$ git config --global --add oh-my-zsh.hide-status 1

https://inotes.oss-cn-beijing.aliyuncs.com/git/201812/git-zsh-status.png

帮助命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
$ git help <verb>
$ git <verb> --help
$ man git

// 查看帮助命令
$ git --help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Reapply commits on top of another base tip
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.