版本控制:Git 常用命令

My GitHub Home Page

参考:https://www.git-scm.com/book/zh/v2

设置用户信息

# 系统
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

# 项目目录下
$ git config user.name "John Doe"
$ git config user.email johndoe@example.com

检查配置

# 检查所有配置
$ git config --list

# 检查指定配置
$ git config user.name

仓库

# 初始化本地仓库
$ git init

# 克隆远程仓库
$ git clone https://github.com/libgit2/libgit2
$ git clone https://github.com/libgit2/libgit2 mylibgit

修改

# 添加
$ git add *.c
$ git add LICENSE

# 移除
$ git rm PROJECTS.md

# 移动
$ git mv file_from file_to

状态

# 查看状态
$ git status
$ git status -s
$ git status --short

# 查看差异
$ git diff
$ git diff --cached
$ git diff --staged     # Git 1.6.1

提交

# 提交
$ git commit
$ git commit -m 'Initial project'
$ git commit -a -m 'Initial project'     # 跳过 git add

历史

$ git log
$ git log -p -2              # 显示最近两次提交
$ git log --stat             # 提交的简略的统计
$ git log --pretty=oneline
$ git log --pretty=format:"%h - %an, %ar : %s"
$ git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \
   --before="2008-11-01" --no-merges -- t/