Git 常用命令速查

1. 设置

1.1. 查看

$ git config --list # 所有设置
$ git config --list --global # 全局设置
$ git config --list --local # 当前仓库设置

1.2. 最小设置

# 不进行此设置时,只能执行 git add 操作
# 一般与待 push 的远程仓库保持一致
# 针对 --local 的设置,优先级高于 --global s
$ git config --global user.name "xxx" # 用户名
$ git config --global user.email "xxx@email.com" # 用户绑定的邮箱

1.3. 代理加速

# 设置代理
$ git config --global http.proxy socks5://127.0.0.1:1234
$ git config --global https.proxy socks5://127.0.0.1:1234
$ git config --global http.sslVerify false

# 取消代理
$ git config --global --unset http.proxy
$ git config --global --unset https.proxy
$ git config --global http.sslVerify true

1.4. 命令别名

# 示例
$ git config --global alias.st status
$ git st # 相当于 git status

# 直接修改配置文件,添加多个
$ vim ~/.gitconfig
$ source ~/.gitconfig
# ~/.gitconfig
[alias]
st = status
co = checkout
ci = commit
br = branch
unstage = "reset HEAD"
last = "log -n1"
lg = "log -n10 --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

2. 常用命令

2.1. 克隆及更新(clone、pull、fetch)

$ git clone xxx.git # 从远程仓库克隆一个项目到本地
$ git clone xxx.git "指定目录" # 克隆的同时,修改目录名
$ git clone -b <new_branch_name> xxx.git # 克隆同时,创建一个指定分支

$ git pull # 拉取远程仓库所有分支更新并合并到本地分支。
$ git pull origin master # 将远程 master 分支合并到当前本地分支
$ git pull origin master:master # 将远程 master 分支合并到本地 master 分支,冒号后面表示本地分支

$ git fetch --all # 拉取所有远端的最新代码
$ git fetch origin master # 拉取远程最新 master 分支代码

2.2. 提交(add、commit、push)

$ git add <.|file|dir> # 提交「工作区」的改动到「暂存区」

$ git commit -m ["本次提交的备注信息"] # 提交「暂存区」的改动到「本地仓库」
$ git commit file1[file2 file3 ...] -m [message] # 只提交指定文件

$ git push origin master # 将「本地仓库」的变动全部推送到「远程仓库」 master 分支
$ git push --tags # 推送所有标签

2.3. 分支(branch、checkout、cherry-pick)

$ git branch # 查看所有本地分支,同 branch -l
$ git branch -r # 查看所有远程的分支
$ git branch -a # 查看所有远程分支和本地分支

$ git checkout -b <branch_name> # 新建并且切换到新的分支
$ git checkout -b <branch_name> <remote_branch|remote_tag> # 基于远程分支或 tag 创建并切换到新分支
$ git checkout -t origin/dev # 创建与远程分支相同的本地分支
$ git branch <branch_name> # 新建一个分支,但仍停留在原分支不切换

$ git branch -D <branch_name> # 删除本地分支
$ git push origin -d <branchname> # 删除远程分支

$ git checkout master # 切换到 master 分支

$ git cherry-pick <branch_name> # 把指定分支的提交记录全部捡出到当前分支
$ git cherry-pick <commit_id> # 捡出指定提交记录到当前分支
$ git cherry-pick <commit_id1>..<commit_idx> # 捡出从 commit_id1 到 commit_idx 的提交记录到当前分支
$ git cherry-pick --abort # 取消捡出,回退到捡出操作之前(出现异常,不能顺利捡出时)

2.4. 标签(tag)

$ git tag # 列出所有 tag
$ git tag <tag_name> # 在当前 commit 新建一个 tag
$ git tag <tag_name> <commit_id> # 基于指定 commit 新建一个 tag
$ git tag -d <tag_name> # 删除本地 tag
$ git push origin <tag_name> # 推送 tag 到远程
$ git show <tag_name> # 查看 tag 的文件变化
$ git checkout -b <branch_name> <tag_name> # 新建一个分支,指向某个 tag

2.5. 提交记录(log)

$ git status # 查看当前工作区状态

$ git log # 查看「当前分支」的所有提交日志
$ git log --all # 查看「所有分支」的所有提交日志
$ git log --oneline # 单行简单格式显示日志信息
$ git log --graph # 以树形展示提交记录
$ git log -n5 # 查看最新的 5 条提交记录
$ git log <branch_name> # 指定分支
$ git log <file1> # 指定文件
$ git log -p <file1> # 根据提交记录,查看某个文件的具体变化
$ git blame <file1> # 从头到尾遍历文件,显示每行文件的修改记录

$ git log --oneline feature1 ^dev # 在基于 dev 创建的分支 feature1 下,查看做了哪些提交

2.6. 撤销与回退(reset)

$ git reset HEAD <file1>... # 恢复「暂存区」中指定文件到 HEAD 的版本
$ git checkout <file1>... # 恢复「工作区」中指定文件到「暂存区」的版本
$ git checkout . # 丢弃「工作区」中的所有变动

$ git reset --soft <commit_id> # 把版本库上的提交回退到「暂存区」,修改记录保留
$ git reset --mixed <commit_id> # 把版本库上的提交回退到「工作区」,修改记录保留
$ git reset --hard <commit_id> # 把版本库上的提交彻底回退到指定 commit,修改记录全部 revert

2.7. 查询远程仓库(remote)

$ git remote # 查看关联的远程仓库名称
$ git remote -v # 查看关联的远程仓库地址
$ git remote add <url> # 添加一个远程仓库
$ git remote show <remote_name> # 显示某个远程仓库的详细信息

2.8. 合并及冲突处理(merge)

$ git merge master  # 在当前分支上合并 master 分支过来
$ git merge --no-ff origin/dev # 在当前分支上合并远程分支dev
$ git merge --abort # 终止本次 merge,并回到 merge 前的状态

2.9. 文件差异比较(diff)

$ git diff [-- file1 file2 ...] # 比较工作区与暂存区差异
$ git diff HEAD # 比较工作区与 HEAD ( 当前工作分支)的差异
$ git diff --cached # 比较暂存区和 HEAD 的差异
$ git diff <branch_name> # 比较与另一个分支的差异
$ git diff commit_id # 比较与某一次提交差异

2.10. 重命名文件(mv)

$ git mv README.md README_2.md

2.11. 删除文件(rm)

$ git rm file1 ...

2.12. 使用暂存(stash)加塞其它紧急任务

$ git stash # 暂存当前工作区的所有变动,使工作区重新变成干净状态,以便开始其它需要紧急处理的工作
$ git stash list # 显示保存的工作进度列表
$ git stash apply [stash num] # 恢复最后一次保存的工作进度,在工作进度列表不删除
$ git stash pop [stash num] # 恢复最后一次保存的工作进度,并且在工作进度列表删除
$ git stash show [stash num] # 显示做了哪些改动
$ git stash drop [stash num] # 删除一条保存的工作进度
$ git stash clear # 删除所有暂存的改动

3. 更多学习资源推荐