1、仓库

  1. # 在当前目录新建一个Git代码库
  2. $ git init
  3. # 新建一个目录,将其初始化为Git代码库
  4. $ git init [project-name]
  5. # 下载一个项目和它的整个代码历史
  6. $ git clone [url]

2、配置

  1. # 显示当前的Git配置
  2. $ git config --list
  3. # 编辑Git配置文件
  4. $ git config -e [--global]
  5. # 设置提交代码时的用户信息
  6. $ git config [--global] user.name "[name]"
  7. $ git config [--global] user.email "[email address]"

3、增加/删除文件

  1. # 添加指定文件到暂存区
  2. $ git add [file1] [file2] ...
  3. # 添加指定目录到暂存区,包括子目录
  4. $ git add [dir]
  5. # 添加当前目录的所有文件到暂存区
  6. $ git add .
  7. # 添加每个变化前,都会要求确认
  8. # 对于同一个文件的多处变化,可以实现分次提交
  9. $ git add -p
  10. # 删除工作区文件,并且将这次删除放入暂存区
  11. $ git rm [file1] [file2] ...
  12. # 停止追踪指定文件,但该文件会保留在工作区
  13. $ git rm --cached [file]
  14. # 改名文件,并且将这个改名放入暂存区
  15. $ git mv [file-original] [file-renamed]

4、提交代码

  1. # 提交暂存区到仓库区
  2. $ git commit -m [message]
  3. # 提交暂存区的指定文件到仓库区
  4. $ git commit [file1] [file2] ... -m [message]
  5. # 提交工作区自上次commit之后的变化,直接到仓库区
  6. $ git commit -a
  7. # 提交时显示所有diff信息
  8. $ git commit -v
  9. # 使用一次新的commit,替代上一次提交
  10. # 如果代码没有任何新变化,则用来改写上一次commit的提交信息
  11. $ git commit --amend -m [message]
  12. # 重做上一次commit,并包括指定文件的新变化
  13. $ git commit --amend [file1] [file2] ...