Git Ninja

版本管理的发展史

Local Version Control Systems

To collaborate with developers on other systems,Centralized Version Control Systems (CVCSs) were developed.

Centralized Version Control Systems

其中就有大名鼎鼎的 SVN (Subversion),这种中央版本管理系统的特点:
have a single server that contains all the versioned files, and a number of clients that check out files from that central place.

这种标准被沿用多年,不过其缺点也很明显:
If that server goes down for an hour, then during that hour nobody can collaborate at all or save versioned changes to anything they’re working on. If the hard disk the central database is on becomes corrupted, and proper backups haven’t been kept, you lose absolutely everything.

当你的整个文件修改历史被保存在一个地方时,就有失去所有的风险

Distributed Version Control Systems

从上图很明显的看出,每个客户端都保有一个项目的所有版本修改历史
if any server dies, any of the client repositories can be copied back up to the server to restore it. Every clone is really a full backup of all the data.

Git 中的文件状态

要理解 git,必须理解 git 中文件的三种状态

Modified
Staged
Committed

Modified 对应文件所处区域 Working directory
Staged 对应文件所处区域 Staging area
Committed 对应文件所处区域 Git directory

The basic Git workflow goes something like this:

  • You modify files in your working directory.
  • You stage the files, adding snapshots of them to your staging area.
  • You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.

Git 配置

1
2
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

安装完 git 后,第一件事情就是设置全局 Identity:
The first thing you should do when you install Git is to set your user name and email address. This is important because every Git commit uses this information, and it’s immutably baked into the commits you start creating.

这是全局的,如果想针对某个项目设置

1
2
git config user.name "John Doe"
git config user.email johndoe@example.com

查看设置

1
git config --list

Git 常用命令整理

首先看一副图,基本上所有常用命令都可以用这张图上的某个过程描述

Two main approaches to get a git project

Initializing a Repository in an Existing Directory

1
2
3
git init
git add file<>
git commit -m "initial commit"

Cloning an Existing Repository

1
git clone https://github.com/xwillmadeit/webpack2.git [projectName](optional)

Add file to the staged area

1
2
git add .
git add <file>

Commit all staged files

1
2
git commit -m "initial commit"
git commit -a -m "skip git add"

Check status

1
2
git status
git status -s

Check the changes

1
2
3
git diff  (compare with unstaged status with staged or last commit status)
git diff --staged (compare with staged status and last commit status)
git difftool

Undoing things

  1. 当想要修改上次提交文件或提交信息时,可以使用该命令
1
git commit --amend

You end up with a single commit – the second commit replaces the results of the first.

  1. Unstage a file
1
git reset HEAD <file>
  1. Unmodifying a Modified File
1
git checkout -- <file>

this command will revert it back to what it looked like when you last committed.

Branch

list all branches

1
git branch

create a new branch

1
git branch test

switch to another branch

1
git checkout test

create and switch

1
git checkout -b iss53

show log

1
2
3
4
git log
git log <file>
git log -p <file>
git show <commit>

Stash changes before switching branch

1
2
3
git stash
git checkcout test
git stash pop/apply

到这里就整理这么多,都是根据 git book 官方原版书总结出来的,有机会再进阶一下