即使每天都使用Git,也无法记住很多命令,所以记录下来方便查阅。
一般来说,只要记住下图中的 6 个命令就能满足日常使用。但是,但是熟练使用,恐怕要记住 60 到 100 个命令。

Git相关术语
使用Git不可避免会遇到下面几个术语,想要更好地理解Git,就要先熟悉它们。
- 工作区(Working Directory)
- 暂存区(Stage/Index)
- 本地历史仓库(Repository)
- 远程仓库(Remote)

工作区
我们写代码的地方就是工作区,就是在电脑里能看到的目录,我们当前的工作空间。
暂存区
暂存区(stage)就是每次 git add 时,文件的修改存放的地方。
git commit 时就是一次性把暂存区所有修改提交到分支。
本地仓库
我们可以把暂存区的内容提交到我们的本地仓库,又名版本库(Repository),可将其理解成一个目录,该目录下的所有文件都会被 git 管理起来,每个文件的修改、删除、git 都能跟踪,以便随时追踪历史,和还原。
.git
隐藏目录就是 git 的版本库,里面存了很多东西,最重要的就是 stage(index) 暂存区,还有第一个分支 master,以及指向 master 的 HEAD 指针。

远程仓库
远程仓库其实就是找一台电脑充当服务器的角色,每天 24 小时开机,其他每个人都从这个远程仓库克隆一份到自己的电脑上,并且各自把各自的提交推送到服务器仓库里,也从服务器仓库中拉取别人的提交。
比如,GitHub、Gitlab 等都属于远程仓库。
下面,举一个形象化的例子来帮助大家理解上面几个概念:
比如我们在逛着某宝:
1、看到了心仪的物品,我们可以把商品添加到购物车(暂存区),我们可能会频繁的添加商品(add)或者移除商品(checkout),在这个过程中我们可以随便嗨,反正还没给钱;
2、接着我们挑的七七八八了,接着我们就要提交我们的订单了,点击提交订单(commit),接着会生成一个商品的订单列表(快照),我们还可以在提交的时候添加点备注信息,比如要什么颜色(commit -m “颜色”),好的,此时订单提交了,但是我们还没支付(Push),我们可以在自己的账户未支付订单列表(本地仓库)中找到我们的这个订单订单(快照),也可以看到自己以前的一些订单记录;
3、再接着我们选择这个还没付款的订单,进行支付(Push),付款完成后,商家(远程仓库)就会收到这个订单,然后发货…
其他重要概念
HEAD
HEAD 就是当前活跃分支的游标,你现在在哪儿,HEAD 就指向哪儿。
HEAD 是一个指针,总是指向当前分支。仓库版本的回退和追踪都是通过操作 HEAD 指针来完成。
不过 HEAD 并非只能指向分支的最顶端(时间节点距今最近的那个),实际上它可以指向任何一个节点,它就是 Git 内部用来追踪当前位置的东东。
标签
有了 commit id 为什么还要tag?因为 commit id 不好找,tag 是有意义的名字,它与 commit 绑在一起。
其他要点
1、每一次 git commit,都会生成一个 commit id 记录该次提交,Git 都会将它们串成一条时间线,这条时间线就是一个分支。
2、因为创建、合并、删除分支都很快,所以 git 鼓励你使用分支完成某个任务,合并后再删除分支。过程比直接在 master 分支工作更安全,且效果一样。
3、分支策略:master 分支应该是非常稳定的,仅用来发布新版本,平时不能在上面干活,干活都在 dev 分支,dev 是不稳定的,到 1.0 发布时,再将 dev 合并到 master 上,由 master 发布新版本。
Git常用命令
1. 仓库相关
新建一个仓库
1 2 3 4 5
| $ git init
$ git init [project-name]
|
克隆一个仓库
1 2 3 4 5
| $ git clone /path/to/repository
$ git clone <项目地址>
|
查找一个仓库
与远程仓库连接
1 2 3 4 5
| $ git remote add origin <项目地址>
$ git remote -v
|
与远程仓库断开连接
1
| $ git remote remove origin
|
2. 配置
Git 的配置文件是 .gitconfig
,可以放在用户的主目录(全局配置)下或项目目录下(项目配置)。
显示当前的 Git 配置
编辑 Git 配置
1
| $ git config -e [--global]
|
设置用来提交代码的用户信息
1 2
| $ git config [--global] user.name "[name]" $ git config [--global] user.email "[email address]"
|
3. 文件操作
添加文件
1 2 3 4 5 6 7 8 9 10 11 12
| $ git add [file1] [file2] ...
$ git add [dir]
$ git add .
$ git add -p
|
删除文件
1 2 3 4 5
| $ git rm [file1] [file2] ...
$ git rm --cached [file]
|
改名文件
1 2
| $ git mv [file-original] [file-renamed]
|
文件状态
4. 代码提交
1 2 3 4 5 6 7 8 9 10 11
| $ git commit -m "message"
$ git commit [file1] [file2] ... -m [message]
$ git commit -a
$ git commit -v
|
1 2 3 4 5 6
|
$ git commit --amend -m [message]
$ git commit --amend [file1] [file2] ...
|
5. 分支相关
列出分支
1 2 3 4 5 6 7 8
| $ git branch
$ git branch -r
$ git branch -a
|
创建新分支
1 2 3 4 5 6 7 8 9 10 11
| $ git branch [branch-name]
$ git checkout -b [branch]
$ git branch [branch] [commit]
$ git branch --track [branch] [remote-branch]
|
切换分支
1 2 3 4 5
| $ git checkout [branch-name]
$ git checkout -
|
删除分支
1 2 3 4 5 6
| $ git branch -d [branch-name]
$ git push origin --delete [branch-name] $ git branch -dr [remote/branch]
|
合并分支
1 2 3 4 5
| $ git merge [branch]
$ git cherry-pick [commit]
|
其他
1 2
| $ git branch --set-upstream [branch] [remote-branch]
|
6. 标签操作
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
| $ git tag
$ git tag [tag]
$ git tag [tag] [commit]
$ git tag -d [tag]
$ git push origin :refs/tags/[tagName]
$ git show [tag]
$ git push [remote] [tag]
$ git push [remote] --tags
$ git checkout -b [branch] [tag]
|
7. 查看信息
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 51 52 53 54 55 56 57 58 59 60
| $ git status
$ git log
$ git log --stat
$ git log -S [keyword]
$ git log [tag] HEAD --pretty=format:%s
$ git log [tag] HEAD --grep feature
$ git log --follow [file] $ git whatchanged [file]
$ git log -p [file]
$ git log -5 --pretty --oneline
$ git shortlog -sn
$ git blame [file]
$ git diff
$ git diff --cached [file]
$ git diff HEAD
$ git diff [first-branch]...[second-branch]
$ git diff --shortstat "@{0 day ago}"
$ git show [commit]
$ git show --name-only [commit]
$ git show [commit]:[filename]
$ git reflog
|
8. 远程同步
下载远程仓库的所有变动
显示所有远程仓库
显示某个远程仓库的信息
1 2
| $ git remote show [remote]
|
增加远程仓库
1 2
| $ git remote add [shortname] [url]
|
pull
1 2
| $ git pull [remote] [branch]
|
push
1 2 3 4 5 6 7 8
| $ git push [remote] [branch]
$ git push [remote] --force
$ git push [remote] --all
|
在$ git push origin master
中:
origin指的就是远程仓库,也就是本地的代码库托管在Github上的版本
如果字面上理解不了,回顾远程仓库的创建命令
1 2
| $ git remote add origin https://github.com/xxx/test.git
|
master只是这个repository中默认创建的第一个branch,可以把 master 换成你想要推送的任何分支。
9. 撤销操作
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
| $ git checkout [file]
$ git checkout [commit] [file]
$ git checkout .
$ git reset [file]
$ git reset --hard
$ git reset [commit]
$ git reset --hard [commit]
$ git reset --keep [commit]
$ git revert [commit]
$ git stash $ git stash pop
|
10. 其他
Github 实例教程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| $ git init
$ git add README.md
$ git commit -m "first commit"
$ git remote add origin https://github.com/xxx/test.git
$ git push -u origin master
|
- 如果本地仓库已经绑定别的远程仓库,我们可以用以下命令将其删除相应的仓库信息
1 2
| $ git remote remove origin
|
- 如果我们remote repo (即远端仓库已经存在了),那么我们只需要执行以下命令就OK了
1
| $ git clone https://github.com/xxx/test.git "you path"
|
比如我们想储存在 G://test 目录下,那么我们可以输入一下命令
1
| $ git clone https://github.com/xxx/test.git D://test
|
Github 配置SSH连接
1. 检查是否已经有SSH Key。
2. 生成一个新的SSH
1
| $ ssh-keygen -t rsa -C "email@github.com"
|
之后直接回车,不用填写东西。之后会让你输入密码(可以不输入密码,直接为空,这样更新代码不用每次输入 id_rsa 密码了)。然后就生成一个目录.ssh ,里面有两个文件:id_rsa , id_rsa.pub(id_rsa中保存的是私钥,id_rsa.pub中保存的是公钥)
3. 添加ssh key到GitHub/GitLab
在GitHub/GitLab上找到关于SSH keys->add key把id_rsa.pub公钥的内容复制进去就可以了。
参考文档
The Most Basic Git Command List: https://www.tutorialdocs.com/article/git-basic-command-list.html
git概念及工作流程详解: https://www.cnblogs.com/tsingke/p/7350490.html
廖雪峰的git教程: https://www.liaoxuefeng.com/wiki/896043488029600