Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Git 常用命令

commit

# 提交暂存区内容
git commit -m "message"

# 跳过暂存,直接提交所有已跟踪文件的改动
git commit -am "message"

# 追加到上一次提交(不新建 commit)
git commit --amend -m "new message"

#  amend 保持原提交信息
git commit --amend --no-edit

push

# 推送当前分支到远程
git push

# 推送并设置上游分支(首次推送)
git push -u origin main

# 强制推送(慎用)
git push --force

# 更安全的强制推送,不会覆盖他人提交
git push --force-with-lease

# 删除远程分支
git push origin --delete branch-name

pull / fetch

# 拉取并合并(fetch + merge)
git pull

# 拉取并变基(fetch + rebase,保持历史线性)
git pull --rebase

# 仅拉取远程数据,不合并
git fetch

# 拉取所有远程仓库
git fetch --all

# 拉取后清理远程已删除的分支
git fetch --prune

# 查看远程分支与本地分支的差异(fetch 后)
git diff origin/main

pull --rebase 比默认的 pull(merge)更适合个人开发分支,历史更干净;但已推送的公共分支应避免 rebase。

branch

# 列出本地分支
git branch

# 列出远程分支
git branch -r

# 列出所有分支
git branch -a

# 创建分支
git branch new-branch

# 删除已合并的分支
git branch -d branch-name

# 强制删除(即使未合并)
git branch -D branch-name

# 查看已合并到当前分支的分支
git branch --merged

# 查看尚未合并的分支(删除前排查用)
git branch --no-merged

# 重命名分支
git branch -m old-name new-name

checkout

# 切换到已有分支
git checkout branch-name

# 创建并切换到新分支
git checkout -b new-branch

# 基于远程分支创建本地分支
git checkout -b local-name origin/remote-name

# 恢复某个文件到最近提交的状态
git checkout -- file

# 切换到指定 commit(detached HEAD)
git checkout <commit-hash>

较新版本的 Git 中 git switchgit restore 分别替代了 checkout 的分支切换和文件恢复功能,语义更清晰。

log

# 简洁单行展示
git log --oneline

# 带分支图
git log --oneline --graph --all

# 查看某文件的提交历史
git log -- file

# 查看某文件的每次改动摘要
git log -p -- file

# 按关键字搜索提交信息
git log --grep="keyword"

# 搜索代码变更内容(某段代码的引入/删除历史)
git log -S "code snippet"

# 按日期范围
git log --since="2024-01-01" --until="2024-12-31"

# 按作者
git log --author="name"

# 最近 n 条
git log -5

diff

# 查看未暂存的改动
git diff

# 查看已暂存的改动
git diff --staged

# 与上一次提交比较
git diff HEAD~1

# 比较两个分支的差异
git diff branchA..branchB

# 比较两个分支的差异(三点语法:branchB 自分叉点以来的改动)
git diff branchA...branchB

# 只列出有差异的文件名
git diff --name-only branchA..branchB

# 查看某个文件的差异
git diff branchA..branchB -- file

stash

# 暂存当前未提交的改动
git stash

# 暂存时附带说明
git stash push -m "description"

# 查看暂存列表
git stash list

# 恢复最近一次暂存(保留 stash 记录)
git stash apply

# 恢复最近一次暂存(删除 stash 记录)
git stash pop

# 恢复指定暂存
git stash apply stash@{1}

# 删除指定暂存
git stash drop stash@{1}

# 清空所有暂存
git stash clear

merge

# 将指定分支合并到当前分支
git merge branch-name

# 合并时禁止快进,始终生成 merge commit
git merge --no-ff branch-name

# 压缩合并,将所有 commit 合并为一个
git merge --squash branch-name
git commit -m "merge branch-name"

# 放弃合并(冲突时)
git merge --abort

rebase

# 将当前分支变基到目标分支
git rebase main

# 交互式变基,合并/编辑/丢弃 commit
git rebase -i HEAD~3

# 冲突时:解决冲突后继续
git rebase --continue

# 跳过当前 commit
git rebase --skip

# 放弃变基
git rebase --abort

reset

# 回退到指定 commit,保留工作区和暂存区改动(最安全)
git reset --soft <commit-hash>

# 回退并清空暂存区,保留工作区改动(默认模式)
git reset --mixed <commit-hash>

# 回退并丢弃所有改动(危险)
git reset --hard <commit-hash>

# 将某个文件从暂存区移除(不影响工作区)
git reset -- file
git restore --staged file    # 新写法
模式HEAD暂存区工作区场景
--soft回退保留保留合并多个 commit 后重提交
--mixed回退清空保留重新整理本次要提交的内容
--hard回退清空清空彻底放弃改动(可用 reflog 恢复)

revert

# 撤销指定 commit(创建新的反向 commit,安全)
git revert <commit-hash>

# 撤销时不自动提交
git revert --no-commit <commit-hash>

# 撤销多个连续 commit
git revert <oldest>..<newest>

cherry-pick

# 将指定 commit 应用到当前分支
git cherry-pick <commit-hash>

# 批量 cherry-pick(不含 A)
git cherry-pick A..B

# 冲突时:解决后继续
git cherry-pick --continue

# 跳过当前 commit
git cherry-pick --skip

# 放弃 cherry-pick
git cherry-pick --abort

tag

# 创建轻量标签
git tag v1.0.0

# 创建附注标签(推荐,含作者和日期)
git tag -a v1.0.0 -m "release v1.0.0"

# 查看所有标签
git tag

# 查看标签详情
git show v1.0.0

# 推送单个标签
git push origin v1.0.0

# 推送所有标签
git push origin --tags

# 删除本地标签
git tag -d v1.0.0

# 删除远程标签
git push origin --delete v1.0.0

轻量标签只是一个指向 commit 的引用;附注标签是一个独立对象,包含打标签者、日期、注释,适合正式发布。

remote

# 查看远程仓库列表
git remote -v

# 添加远程仓库
git remote add upstream https://github.com/original/repo.git

# 修改远程仓库地址
git remote set-url origin git@github.com:user/repo.git

# 删除远程仓库
git remote remove upstream

# 查看远程仓库详情
git remote show origin

# 清理本地已不存在于远程的分支引用
git remote prune origin

config

# 设置全局用户名和邮箱
git config --global user.name "name"
git config --global user.email "email@example.com"

# 为当前仓库单独设置
git config user.name "name"

# Windows 换行符处理(检出 LF 转 CRLF,提交 CRLF 转 LF)
git config --global core.autocrlf true

# 常用别名
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --all"

# 查看所有配置
git config --list

reflog

# 查看所有操作历史(含 HEAD 移动记录)
git reflog

# 查看指定分支的操作历史
git reflog show branch-name

# 恢复到 reflog 中的某次操作(误 reset 后救命)
git reset --hard HEAD@{2}

reflog 只记录本地操作,不随 push 传输。reset --hard 误操作后只要没超过过期时间(默认 90 天)都能找回。

blame

# 查看每行代码的修改者和 commit
git blame file

# 指定行范围
git blame -L 10,20 file

# 忽略空白改动
git blame -w file

# 显示完整 commit hash
git blame -l file

clean

# 预览将被删除的未跟踪文件
git clean -n

# 删除未跟踪的文件
git clean -f

# 同时删除未跟踪的目录
git clean -fd

# 同时删除 .gitignore 中指定的文件
git clean -fx

-n 预览再执行,以免误删。

git-lfs

# 安装 LFS(首次使用)
git lfs install

# 追踪某类大文件
git lfs track "*.psd"
git lfs track "*.zip"

# 查看已追踪的文件类型
git lfs track

# 列出当前仓库中的 LFS 文件
git lfs ls-files

# 拉取 LFS 文件
git lfs pull

# 迁移已有文件到 LFS
git lfs migrate import --include="*.psd" --everything

.gitattributes 会被 git lfs track 自动更新,记得提交该文件。

移除某个文件的所有 commit 记录

git filter-branch -f --index-filter 'git rm -rf --cached --ignore-unmatch file-to-delete' HEAD
git push origin --force --all