Appearance
Git 速记表
配置
用户信息
git config --global user.name "Your Name"git config --global user.email "email@example.com"git config --list # 查看所有配置git config user.name # 查看特定配置git config --system # 系统级(所有用户)git config --global # 用户级(当前用户)git config --local # 仓库级(当前仓库,默认)配置别名
git config --global alias.s statusgit config --global alias.co checkoutgit config --global alias.b branchgit config --global alias.cm commit 初始化
创建仓库
git init # 初始化本地仓库git init project # 创建并初始化项目目录git clone url # 克隆远程仓库git clone url dir # 克隆到指定目录克隆选项
git clone --depth 1 url # 浅克隆(仅最新提交)git clone --branch dev url # 克隆指定分支git clone --single-branch url # 仅克隆一个分支基本操作
查看状态
git status # 查看状态git status -s # 简短输出git status -b # 显示分支信息git diff # 查看未暂存的修改git diff --staged # 查看已暂存的修改添加文件
git add file.txt # 添加单个文件git add . # 添加所有文件git add *.js # 添加所有js文件git add -p # 交互式添加(部分添加)提交
git commit -m "message" # 提交git commit -am "message" # 添加并提交(已跟踪文件)git commit --amend # 修改最近一次提交git commit --amend --no-edit # 修改提交但不改消息撤销操作
git restore file.txt # 撤销工作区修改git restore --staged file.txt # 取消暂存git reset HEAD~1 # 撤销最近一次提交(保留修改)git reset --hard HEAD~1 # 撤销提交并丢弃修改git revert commit-hash # 创建新提交来撤销删除文件
git rm file.txt # 删除文件并暂存git rm --cached file.txt # 仅从仓库删除(保留本地)git mv old.txt new.txt # 重命名文件分支管理
分支操作
git branch # 查看本地分支git branch -a # 查看所有分支git branch name # 创建分支git branch -d name # 删除分支git branch -D name # 强制删除分支git branch -m old new # 重命名分支切换分支
git checkout branch # 切换分支git checkout -b branch # 创建并切换分支git switch branch # 切换分支(新)git switch -c branch # 创建并切换(新)合并分支
git merge branch # 合并分支git merge --no-ff branch # 禁用快速合并git merge --squash branch # 压缩合并git merge --abort # 取消合并变基
git rebase branch # 变基到指定分支git rebase -i HEAD~3 # 交互式变基最近3次提交git rebase --continue # 继续变基git rebase --abort # 取消变基分支比较
git diff branch1 branch2 # 比较分支git diff branch1..branch2 # 同上git diff branch1...branch2 # 比较分支分叉点后的差异git log branch1..branch2 # 查看差异提交远程操作
远程仓库
git remote # 查看远程仓库git remote -v # 查看远程仓库详细信息git remote add origin url # 添加远程仓库git remote remove origin # 删除远程仓库git remote rename old new # 重命名远程仓库获取更新
git fetch # 获取远程更新git fetch origin # 获取指定远程仓库git fetch --all # 获取所有远程仓库git fetch --prune # 获取并清理已删除的远程分支拉取
git pull # 拉取并合并git pull --rebase # 拉取并变基git pull origin main # 从指定远程分支拉取git pull --ff-only # 仅快速合并推送
git push # 推送到远程git push origin main # 推送到指定分支git push -u origin main # 推送并设置上游git push --all # 推送所有分支git push --tags # 推送所有标签git push --force # 强制推送(慎用)跟踪分支
git branch -vv # 查看跟踪分支git branch -u origin/main # 设置上游分支git push -u origin branch # 推送并设置上游git checkout --track origin/branch # 创建跟踪分支查看历史
日志查看
git log # 查看提交历史git log --oneline # 单行显示git log --graph # 图形化显示git log --all # 显示所有分支git log -n 5 # 显示最近5条git log --stat # 显示文件变更统计格式化日志
git log --oneline --graph --all --decorategit log --pretty=format:"%h - %an, %ar : %s"git log --pretty=onelinegit log --pretty=shortgit log --pretty=full筛选日志
git log --author="name" # 按作者git log --since="2 weeks ago" # 按时间git log --until="2024-01-01" # 截止时间git log --grep="keyword" # 按提交消息git log -- file.txt # 按文件git log -S "function" # 按内容变化查看提交
git show commit-hash # 查看提交详情git show HEAD # 查看最新提交git show HEAD~2 # 查看倒数第3次提交git show branch:file # 查看分支上的文件查看差异
git diff # 工作区 vs 暂存区git diff HEAD # 工作区 vs 最新提交git diff --cached # 暂存区 vs 最新提交git diff commit1 commit2 # 比较两次提交git diff branch1 branch2 # 比较分支查看文件历史
git log -- file.txt # 文件提交历史git log -p file.txt # 显示文件变更内容git blame file.txt # 查看每行最后修改git log --follow file.txt # 跟踪重命名标签管理
创建标签
git tag v1.0.0 # 创建轻量标签git tag -a v1.0.0 -m "Release 1.0" # 创建附注标签git tag v1.0.0 commit-hash # 为指定提交打标签查看标签
git tag # 列出所有标签git tag -l "v1.*" # 列出匹配的标签git show v1.0.0 # 查看标签详情推送标签
git push origin v1.0.0 # 推送指定标签git push origin --tags # 推送所有标签git push origin :refs/tags/v1.0.0 # 删除远程标签删除标签
git tag -d v1.0.0 # 删除本地标签git push origin --delete v1.0.0 # 删除远程标签暂存
stash 操作
git stash # 暂存当前修改git stash save "message" # 暂存并添加说明git stash -u # 包含未跟踪文件git stash list # 查看暂存列表git stash show # 查看最新暂存恢复暂存
git stash pop # 恢复最新暂存并删除git stash apply # 恢复最新暂存(保留)git stash apply stash@{2} # 恢复指定暂存git stash drop # 删除最新暂存git stash clear # 清空所有暂存创建分支
git stash branch name # 从暂存创建分支高级操作
cherry-pick
挑选提交
git cherry-pick commit-hash # 应用指定提交git cherry-pick commit1 commit2 # 应用多个提交git cherry-pick --continue # 继续git cherry-pick --abort # 取消reflog
引用日志
git reflog # 查看引用历史git reflog show branch # 查看分支历史git reset --hard HEAD@{2} # 恢复到之前的状态bisect
二分查找问题
git bisect start # 开始二分查找git bisect bad # 标记当前为坏的git bisect good commit # 标记好的提交git bisect reset # 结束查找clean
清理未跟踪文件
git clean -n # 预览要删除的文件git clean -f # 删除未跟踪文件git clean -fd # 删除文件和目录git clean -fX # 删除被忽略的文件git clean -fx # 删除所有未跟踪文件worktree
工作树
git worktree add ../path branch # 添加工作树git worktree list # 列出工作树git worktree remove path # 删除工作树git worktree prune # 清理工作树子模块
添加子模块
git submodule add url path # 添加子模块git submodule init # 初始化子模块git submodule update # 更新子模块git submodule update --init --recursive # 递归初始化管理子模块
git submodule status # 查看子模块状态git submodule foreach git pull # 更新所有子模块git clone --recurse-submodules url # 克隆包含子模块删除子模块
git submodule deinit path # 注销子模块git rm path # 删除子模块rm -rf .git/modules/path # 清理.git目录.gitignore
忽略规则
# 注释*.log # 忽略所有.log文件!important.log # 不忽略/temp # 仅忽略根目录的temptemp/ # 忽略所有temp目录**/logs # 忽略所有logs目录*.txt # 忽略所有txt文件常见模式
# Node.jsnode_modules/npm-debug.log # Python__pycache__/*.py[cod].env # IDE.vscode/.idea/*.swp全局忽略
git config --global core.excludesfile ~/.gitignore_global清除已跟踪文件
git rm -r --cached . # 清除缓存git add . # 重新添加git commit -m "Update .gitignore"冲突解决
查看冲突
git status # 查看冲突文件git diff # 查看冲突内容git log --merge # 查看冲突的提交解决冲突
# 手动编辑文件,删除冲突标记<<<<<<< HEAD当前分支的内容=======合并分支的内容>>>>>>> branch# 标记为已解决git add conflicted-filegit commit冲突工具
git mergetool # 使用合并工具git checkout --ours file # 使用当前分支版本git checkout --theirs file # 使用合并分支版本取消合并
git merge --abort # 取消合并git rebase --abort # 取消变基最佳实践
提交信息
# 格式<type>(<scope>): <subject> <body> <footer> # 类型feat: 新功能fix: 修复bugdocs: 文档style: 格式refactor: 重构test: 测试chore: 构建/工具分支命名
main/master # 主分支develop # 开发分支feature/feature-name # 功能分支bugfix/bug-name # 修复分支hotfix/hotfix-name # 热修复分支release/v1.0.0 # 发布分支工作流
# 功能开发git checkout -b feature/new-feature develop# 开发...git add .git commit -m "feat: add new feature"git checkout developgit merge --no-ff feature/new-featuregit branch -d feature/new-featuregit push origin develop保持整洁
# 定期同步主分支git checkout maingit pullgit checkout feature-branchgit rebase main # 压缩提交git rebase -i HEAD~3 # 清理分支git branch -d old-branchgit push origin --delete old-branch常用命令组合
快速提交
git add . && git commit -m "message" && git push撤销推送
git reset --hard HEAD~1git push --force同步fork
git remote add upstream urlgit fetch upstreamgit checkout maingit merge upstream/maingit push origin main查找提交
git log --all --grep="keyword"git log --all --oneline | grep "keyword"统计信息
git shortlog -sn # 贡献者统计git log --author="name" --oneline | wc -l # 提交数git diff --stat # 变更统计备份分支
git branch backup-branchgit tag backup-tagGit Hooks
常用钩子
pre-commit # 提交前commit-msg # 提交消息pre-push # 推送前post-checkout # 检出后post-merge # 合并后创建钩子
# .git/hooks/pre-commit#!/bin/shnpm testif [ $? -ne 0 ]; then echo "Tests failed" exit 1fi chmod +x .git/hooks/pre-commithusky
现代 Git Hooks
npm install husky -Dnpx husky initecho "npm test" > .husky/pre-commit技巧与诀窍
临时忽略文件
git update-index --assume-unchanged filegit update-index --no-assume-unchanged filegit ls-files -v | grep "^h" # 查看被忽略的文件修改作者
git commit --amend --author="Name <email>"git rebase -i HEAD~3 # 交互式修改多个查看远程分支
git branch -r # 远程分支git branch -a # 所有分支git remote show origin # 远程仓库详情优化仓库
git gc # 垃圾回收git prune # 清理无用对象git fsck # 检查完整性git count-objects -vH # 查看仓库大小找回删除的提交
git reflog # 查找提交hashgit checkout -b recover-branch commit-hashgit cherry-pick commit-hash批量修改历史
# 修改邮箱git filter-branch --env-filter 'OLD_EMAIL="old@email.com"NEW_EMAIL="new@email.com"if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]; then export GIT_COMMITTER_EMAIL="$NEW_EMAIL" export GIT_AUTHOR_EMAIL="$NEW_EMAIL"fi' --tag-name-filter cat -- --branches --tags