Git Common Commands - tenji/ks GitHub Wiki

Git常用命令

Keep Updating...

专业术语

  • Workspace:工作区
  • Index/Stage:暂存区
  • Repository:仓库区(本地仓库)
  • Remote:远程仓库

新建仓库

  • 在当前目录新建一个Git代码库
$ git init
  • 下载一个项目和它的整个代码历史
$ git clone [url]

添加/删除文件

  • 添加指定文件到暂存区
$ git add [file1] [file2] ...
  • 添加指定目录到暂存区,包括子目录
$ git add [dir]
  • 添加当前目录的所有文件到暂存区
$ git add .
  • 从暂存区移除指定文件
$ git reset HEAD [file1] [file2] ...
  • 从缓存区移除指定目录,包括子目录
$ git reset HEAD [dir]
  • 丢弃工作区修改的指定文件
$ git checkout -- [file] ...
  • 丢弃工作区修改的所有文件
$ git checkout -- .
  • 告诉你哪些文件会被删除. 记住他不会真正的删除文件, 只是一个提醒
$ git clean -n
  • 删除当前目录下所有 untrack 的文件
git clean -f
  • 删除指定路径下的 untrack 的文件
git clean -f [path]
  • 删除当前目录下 untrack 的文件和文件夹
git clean -df

代码提交

  • 提交暂存区到仓库区
$ git commit -m [message]
  • 提交暂存区的指定文件到仓库区
$ git commit [file1] [file2] ... -m [message]

状态及日志

  • 显示有变更的文件
$ git status
  • 显示当前分支的版本历史
$ git log
  • 查看最近一次提交所有更改过的文件
$ git log -n 1 --stat
  • 查看最近一次提交所有更改的细节
$ git log -n 1 -p
  • 显示暂存区和工作区的差异
$ git diff
  • 显示暂存区和工作区单个文件的差异
$ git diff [file]
  • 获取文件更改的个数、增加行数、删除行数
$ git diff --stat
  • 表格形式获取增加行数和减少行数
$ git diff --numstat
  • 显示暂存区中文件的修改
$ git diff -staged
  • 查看某个 commit 的文件修改记录
$ git show [commit]
  • 查看某个 commit 修改的文件列表
$ git show [commit] --stat

分支

  • 列出本地所有分支
$ git branch
  • 列出远程所有分支
$ git branch -r
  • 切换到指定分支,并更新工作区
$ git checkout [branch-name]
  • 新建一个分支,并停留在当前分支
$ git branch [branch-name]
  • 新建一个分支,并切换到该分支
$ git checkout -b [branch-name]
  • 新建一个分支,与指定的远程分支建立追踪关系
$ git branch --track [branch-name] [remote-branch]
  • 新建一个分支,与指定的commit点建立追踪关系
$ git branch -b [branch-name] [SHA1]
  • 删除本地分支
$ git branch -d [branch-name]
  • 强制本地删除分支
$ git branch -D [branch-name]
  • 删除远程分支
$ git push origin --delete [branch-name]
  • 新增远程仓库
$ git remote add [repo-name] [repo-url]
  • 修改远程仓库地址
$ git remote add [repo-name] [repo-url]
  • 删除指定的远程仓库
$ git remote rm [repo-name]

标签

  • 列出所有tag
$ git tag
  • 删除本地tag
$ git tag -d [tag]
  • 删除远程tag
$ git push origin :refs/tags/[tagName]
  • 查看tag信息
$ git show [tag]
  • 提交指定tag
$ git push [remote] [tag]
  • 新建一个分支,指向某个tag
$ git checkout -b [branch] [tag]

远程同步

  • 下载远程仓库的所有变动
$ git fetch [remote]
  • 显示所有远程仓库
$ git remote -v
  • 下载远程仓库的所有变动
$ git fetch [remote]
  • 取回远程仓库的变化,并与本地分支合并
$ git pull [remote] [branch]
  • 取回远程仓库的变化,并与本地分支rebase
$ git pull --rebase [remote] [branch]
  • 上传本地指定分支到远程仓库
$ git push [remote] [branch]
  • 强行推送当前分支到远程仓库,即使有冲突
$ git push [remote] --force
  • 拉取更新远程分支列表
$ git remote update [remote] --prune
  • 从其它分支拉取指定的 commit 到当前分支
$ git cherry-pick [commitHash]
  • 从其它分支拉取指定的多个 commit 到当前分支
$ git cherry-pick [commitHashA] [commitHashB]

撤销

  • 重置暂存区与工作区,与上一次commit保持一致
$ git reset --hard
  • 重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
$ git reset [commit]
  • 重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
$ git reset --hard [commit]
  • 保存当前的工作进度。会分别对暂存区和工作区的状态进行保存
$ git stash
  • 显示进度列表。此命令显然暗示了git stash 可以多次保存工作进度,并用在恢复时候进行选择
$ git stash list
  • 显示保存的工作进度内容
$ git stash show [--index] [<stash>]
  • 恢复指定工作进度,但是不将工作进度从工作进度列表中删除
$ git stash apply [--index] [<stash>]
  • 恢复最新保存的工作进度,并将恢复的工作进度从存储的工作进度列表中清除
$ git stash pop [--index] [<stash>]
  • 删除保存的指定工作进度
$ git stash drop [--index] [<stash>]
  • 删除所有存储的进度
$ git stash clear
  • 查看 stash 记录的时间
$ git stash list --date=relative
$ git stash list --date=short
$ git stash list --date=local

代理设置

  • 设置当前分支HTTP代理
$ git config http.proxy http://127.0.0.1:3128
  • 设置全局HTTP代理
$ git config --global http.proxy http://127.0.0.1:3128
  • 设置当前分支HTTPS代理
$ git config https.proxy https://127.0.0.1:3128
  • 设置HTTPS代理
$ git config --global https.proxy https://127.0.0.1:3128
  • 忽略SSL证书错误
$ git config --global https.proxy https://127.0.0.1:3128

其它

  • 设置当前分支代码提交模板
$ git config [template-file-path]
  • 设置全局代码提交模板
$ git config --config [template-file-path]
  • 查看全局配置
$ git config --global --list
  • 删除指定的全局配置
$ git config --global --unset user.name
  • 检查 patch/diff 是否能正常打入
$ git apply --check [path/to/xxx.patch]
$ git apply --check [path/to/xxx.diff]
  • 打入 patch/diff
$ git apply [path/to/xxx.patch]
$ git apply [path/to/xxx.diff]

参考链接

⚠️ **GitHub.com Fallback** ⚠️