1.起步
1.1 设置username&email
| 1 | $ git config --global user.name "John Doe" | 
1.2 查看配置
| 1 | git config --list | 
1.3 初始化仓库、跟踪文件并提交
| 1 | $ git init | 
2.记录文件变化
2.1 查看文件状态
| 1 | $ git status -s | 
M代表modify,有两个可以出现的位置,
出现在右边的 M 表示该文件被修改了但是还没放入暂存区,
出现在左边的 M 表示该文件被修改了并放入了暂存区,
两个M表示该文件被修改并放入暂存区后又被修改了。
2.2 添加文件至暂存区
| 1 | git add file | 
git add是个多功能命令:可以用它跟踪新文件,
或者把已修改文件放到暂存区,还能把合并时冲突的文件标记为已解决状态。 
注意!如果你git add之后修改文件,这个文件会同时出现在暂存区和非暂存区,这时git commint只提交你最后一次运行 git add 命令时的那个版本,而不是你的最新版本。
2.3 文件对比
| 1 | git diff --cached | 
表示暂存后(git add后)的文件与原始版本的对比
| 1 | git diff | 
表示未暂存(git add前)的文件与原始版本的对比
也就是说,git add之前用git diff,之后用git diff –cached。
注意!如果暂存(git add)后又修改了文件,
则git diff比较未暂存的文件与暂存(git add)时的文件。
2.4 跳过使用暂存区域(即跳过git add)
| 1 | git commit -a -m 'added new benchmarks' | 
注意:只会提交已经跟踪过的文件,不会提交新添加的文件
2.5 移除文件(即脱离追踪):
| 1 | $ git rm PROJECTS.md | 
2.6 移动或重命名
| 1 | $ git mv README.md README | 
2.7 提交历史
| 1 | $ git log | 
2.8 取消暂存
| 1 | $ git reset HEAD file | 
2.9 还原文件
| 1 | $ git checkout -- file | 
2.10 文件状态变化周期图

3.远程仓库的使用
3.1 查看远程仓库
| 1 | git remote -v | 
orgin是远程仓库的别名,默认origin
3.2 详细查看远程仓库
| 1 | $ git remote show origin | 
它会列出远程仓库的 URL 与跟踪分支的信息,
告诉你git pull时哪两个分支会合并,git push时会推送到哪个分支
3.3 添加远程仓库
| 1 | git remote add <shortname> <url> | 
3.4 移除远程仓库
| 1 | $ git remote rm origin | 
3.5 推送至上游
| 1 | $ git push origin <local branch>:<remote branch> | 
3.6 检出远程分支(即创建本地分支)
| 1 | $ git checkout -b serverfix origin/serverfix | 
3.7 查看追踪分支(即本地分支对应的远程分支)
| 1 | $ git fetch --all | 
追踪分支:如果你在git pull或git push时没有显式指定远程分支,则默认追踪分支为对应的远程分支
3.8 修改当前分支的追踪分支
| 1 | git branch --set-upstream-to origin/serverfix | 
4.更新代码
git pull与git fetch的区别
| 1 | git pull origin master | 
| 1 | git fetch origin | 
| 1 | git fetch origin master:tmp | 
5.分支
5.1 新建分支
| 1 | git branch branchname | 
5.2 删除分支
| 1 | git branch -d branchname | 
5.3 查看所有分支&当前分支
| 1 | git branch -a |