耕码记

Git 命令速查手册

2026/08/18
3
0

Git 命令速查手册

Git 常用命令集合,按功能分类整理


目录


基础操作

展示帮助信息

git help -g

回到远程仓库的状态

抛弃本地所有的修改,回到远程仓库的状态:

git fetch --all && git reset --hard origin/master

重设第一个 commit

把所有的改动都重新放回工作区,并清空所有的 commit:

git update-ref -d HEAD

展示工作区和暂存区的不同

git diff

展示任意两个 commit 之间的文件变动:

git diff <commit-id> <commit-id>

展示暂存区和最近版本的不同

git diff --cached

展示暂存区、工作区和最近版本的不同

git diff HEAD

快速切换到上一个分支

git checkout -

修改上一个 commit 的描述

如果暂存区有改动,同时也会将暂存区的改动提交到上一个 commit:

git commit --amend

修改作者名

git commit --amend --author='Author Name <[email protected]>'

给 git 命令起别名

git config --global alias.<handle> <command>

# 示例:将 git status 简化为 git st
git config --global alias.st status

忽略某个文件的改动

关闭 track 指定文件的改动:

git update-index --assume-unchanged path/to/file

恢复 track 指定文件的改动:

git update-index --no-assume-unchanged path/to/file

忽略文件的权限变化

git config core.fileMode false

分支管理

展示本地分支关联远程仓库的情况

git branch -vv

关联远程分支

关联之后可以直接使用 git push,不需要指定远程仓库:

git branch -u origin/mybranch

或者在 push 时加上 -u 参数:

git push origin/mybranch -u

列出所有远程分支

git branch -r

列出本地和远程分支

git branch -a

查看远程分支和本地分支的对应关系

git remote show origin

创建并切换到本地分支

git checkout -b <branch-name>

从远程分支中创建并切换到本地分支

git checkout -b <branch-name> origin/<branch-name>

删除本地分支

git branch -d <local-branchname>

删除远程分支

git push origin --delete <remote-branchname>

或者:

git push origin :<remote-branchname>

删除已经合并到 master 的分支

git branch --merged master | grep -v '^\*\|  master' | xargs -n 1 git branch -d

重命名本地分支

git branch -m <new-branch-name>

远程删除了分支本地也想删除

git remote prune origin

以最后提交的顺序列出所有 Git 分支

最新的放在最上面:

git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/

新建并切换到新分支上,同时这个分支没有任何 commit

相当于保存修改,但是重写 commit 历史:

git checkout --orphan <branch-name>

clone 下来指定的单一分支

git clone -b <branch-name> --single-branch https://github.com/user/repo.git

clone 最新一次提交

减少 clone 时间:

git clone --depth=1 https://github.com/user/repo.git

把 A 分支的某一个 commit,放到 B 分支上

git checkout <branch-name> && git cherry-pick <commit-id>

标签管理

查看标签

git tag

展示当前分支的最近的 tag:

git describe --tags --abbrev=0

查看标签详细信息

git tag -ln

本地创建标签

默认 tag 是打在最近的一次 commit 上:

git tag <version-number>

指定 commit 打 tag:

git tag -a <version-number> -m "v1.0 发布(描述)" <commit-id>

推送标签到远程仓库

推送单个标签:

git push origin <local-version-number>

一次性推送所有标签:

git push origin --tags

删除本地标签

git tag -d <tag-name>

删除远程标签

需要先删除本地标签,再执行:

git push origin :refs/tags/<tag-name>

切回到某个标签

git checkout -b branch_name tag_name

撤销与回退

放弃工作区的修改

放弃某个文件的修改:

git checkout <file-name>

放弃所有修改:

git checkout .

恢复删除的文件

# 得到 deleting_commit
git rev-list -n 1 HEAD -- <file_path>

# 回到删除文件 deleting_commit 之前的状态
git checkout <deleting_commit>^ -- <file_path>

以新增一个 commit 的方式还原某一个 commit 的修改

git revert <commit-id>

回到某个 commit 的状态,并删除后面的 commit

和 revert 的区别:reset 命令会抹去某个 commit id 之后的所有 commit

# 默认就是 -mixed 参数
git reset <commit-id>

# 回退至上个版本,重置 HEAD 到另外一个 commit,重置暂存区,工作区不会被更改
git reset –mixed HEAD^

# 回退至三个版本之前,只回退 commit 信息,暂存区和工作区保持一致
git reset –soft HEAD~3

# 彻底回退到指定 commit-id 的状态,暂存区和工作区也会变为指定版本的内容
git reset –hard <commit-id>

把暂存区的指定 file 放到工作区中

git reset <file-name>

强制推送

git push -f <remote-name> <branch-name>

暂存操作

存储当前的修改,但不用提交 commit

git stash

保存当前状态,包括 untracked 的文件

git stash -u

展示所有 stashes

git stash list

回到某个 stash 的状态

git stash apply <stash@{n}>

回到最后一个 stash 的状态,并删除这个 stash

git stash pop

删除所有的 stash

git stash clear

从 stash 中拿出某个文件的修改

git checkout <stash@{n}> -- <file-path>

远程仓库

列出所有远程仓库

git remote

增加远程仓库

git remote add origin <remote-url>

修改远程仓库的 url

git remote set-url origin <URL>

从远程仓库根据 ID,拉下某一状态,到本地分支

git fetch origin pull/<id>/head:<branch-name>

查看与搜索

查看 commit 历史

git log

展示简化的 commit 历史

git log --pretty=oneline --graph --decorate --all

在 commit log 中查找相关内容

git log --all --grep='<given-text>'

在 commit log 中显示 GPG 签名

git log --show-signature

commit 历史中显示 Branch1 有的,但是 Branch2 没有 commit

git log Branch1 ^Branch2

查看某段代码是谁写的

git blame <file-name>

显示本地更新过 HEAD 的 git 命令记录

每次更新了 HEAD 的 git 命令都会被记录下来(不限分支),可以 reset 到任何一次更新了 HEAD 的操作之后:

git reflog

查看两个星期内的改动

git whatchanged --since='2 weeks ago'

详细展示一行中的修改

git diff --word-diff

展示任意分支某一文件的内容

git show <branch-name>:<file-name>

高级技巧

强制删除 untracked 的文件

可以用来删除新建的文件。注意:删除的文件无法找回,不会影响 tracked 文件的改动:

git clean <file-name> -f

强制删除 untracked 的目录

git clean <directory-name> -df

清除 .gitignore 文件中记录的文件

git clean -X -f

展示所有 tracked 的文件

git ls-files -t

展示所有 untracked 的文件

git ls-files --others

展示所有忽略的文件

git ls-files --others -i --exclude-standard

展示忽略的文件

git status --ignored

展示所有 alias 和 configs

# 当前目录
git config --local --list

# 全局
git config --global --list

删除全局设置

git config --global --unset <entry-name>

把某一个分支导出成一个文件

git bundle create <file> <branch-name>

从包中导入分支

git clone repo.bundle <repo-dir> -b <branch-name>

执行 rebase 之前自动 stash

git rebase --autostash

代理配置

git 配置 http 和 socks 代理

git config --global https.proxy 'http://127.0.0.1:8001'
git config --global http.proxy 'http://127.0.0.1:8001'
git config --global socks.proxy "127.0.0.1:1080"

git 配置 ssh 代理

# 编辑 ~/.ssh/config
Host gitlab.com
  ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p

Host github.com
  ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p

Commit 规范

提交信息结构

  • 标题行:必填,描述主要修改类型和内容
  • 主题内容:描述为什么修改、做了什么样的修改、开发思路等
  • 页脚注释:放 Breaking Changes 或 Closed Issues

常用类型 (type)

类型说明
feat新特性
fix修改问题
refactor代码重构
docs文档修改
style代码格式修改(非 CSS)
test测试用例修改
chore其他修改(构建流程、依赖管理等)

其他字段说明

字段说明
scopecommit 影响的范围,如:route, component, utils, build
subjectcommit 的概述
bodycommit 具体修改内容,可以分为多行
footer一些备注,通常是 BREAKING CHANGE 或修复的 bug 的链接

使用 Commitizen 代替 git commit

全局安装:

npm install -g commitizen cz-conventional-changelog

echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc

安装后使用 git cz 代替 git commit


核心概念说明

概念说明
工作区改动(增删文件和内容)
暂存区git add 后的改动存放位置
本地仓库git commit 后的版本存放位置
远程仓库git push 后的存放位置(GitHub 等)
commit-idgit log 最上面那行 commit xxxxxx 后的字符串

注意事项

  1. ⚠️ 一定要先测试命令的效果后,再用于工作环境
  2. git clean 删除的文件无法找回
  3. git reset --hard 会丢失 commit 历史,请谨慎使用