掌握Git命令行,提升开发效率 🚀
Git是现代软件开发中不可或缺的版本控制工具。本文整理了最常用的Git命令,按功能分类,便于快速查找和学习。
1. 仓库初始化与基础配置 ⚙️
仓库初始化
| 命令 |
功能说明 |
常用参数 |
使用场景 |
git init |
初始化新的Git仓库 |
--bare 创建裸仓库
--template=<template_directory> 指定模板 |
新项目开始时 |
git clone <url> |
克隆远程仓库 |
-b <branch> 指定分支
--depth 1 浅克隆
--recursive 递归克隆子模块 |
获取现有项目 |
配置管理
| 命令 |
功能说明 |
常用参数 |
使用场景 |
git config |
配置Git设置 |
--global 全局配置
--local 本地配置
--system 系统配置 |
环境初始化 |
git config --list |
查看所有配置 |
--show-origin 显示配置来源 |
检查配置状态 |
git config --unset |
删除配置项 |
--global 删除全局配置 |
清理错误配置 |
常用配置命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
git config --global core.editor "code --wait" git config --global core.editor "vim"
git config --global init.defaultBranch main
git config --global core.autocrlf input git config --global core.autocrlf true
git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit
git config --list git config user.name
|
2. 文件状态与提交管理 📝
文件状态查看
| 命令 |
功能说明 |
常用参数 |
使用场景 |
git status |
查看工作区状态 |
-s 简洁输出
--porcelain 脚本友好格式
-b 显示分支信息 |
检查文件状态 |
git ls-files |
列出索引中的文件 |
--cached 暂存区文件
--deleted 已删除文件
--modified 已修改文件 |
查看跟踪文件 |
git diff |
查看差异 |
--cached 暂存区差异
--staged 同–cached
--name-only 仅显示文件名 |
比较文件变化 |
文件操作
| 命令 |
功能说明 |
常用参数 |
使用场景 |
git add |
添加文件到暂存区 |
. 添加所有文件
-A 添加所有变更
-p 交互式添加
-u 仅添加已跟踪文件 |
准备提交 |
git rm |
删除文件 |
--cached 仅从暂存区删除
-r 递归删除目录
-f 强制删除 |
移除文件 |
git mv |
移动/重命名文件 |
无特殊参数 |
重命名文件 |
git restore |
恢复文件 |
--staged 取消暂存
--worktree 恢复工作区 |
撤销修改 |
提交管理
| 命令 |
功能说明 |
常用参数 |
使用场景 |
git commit |
提交暂存区内容 |
-m "message" 提交信息
-a 自动暂存已跟踪文件
--amend 修改最后一次提交
-v 显示差异 |
保存变更 |
git commit --fixup |
创建修复提交 |
<commit> 指定要修复的提交 |
修复历史提交 |
git commit --squash |
创建压缩提交 |
<commit> 指定要压缩的提交 |
合并提交准备 |
实用场景
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| git add . && git commit -m "feat: 添加新功能"
git commit --amend -m "fix: 修正提交信息"
git add forgotten_file.txt git commit --amend --no-edit
git restore --staged <file> git reset HEAD <file>
git restore <file> git checkout -- <file>
git add -p <file>
|
3. 版本历史与回退 🔄
历史查看
| 命令 |
功能说明 |
常用参数 |
使用场景 |
git log |
查看提交历史 |
--oneline 单行显示
--graph 图形化显示
-n <num> 限制显示数量
--since="2 weeks ago" 时间过滤 |
查看项目历史 |
git log --follow |
跟踪文件历史 |
<file> 指定文件 |
查看文件变更历史 |
git show |
显示提交详情 |
<commit-id> 指定提交
--stat 显示统计信息
--name-only 仅显示文件名 |
查看具体提交 |
git blame |
查看文件修改记录 |
-L <start>,<end> 指定行范围
-w 忽略空白字符 |
追踪代码作者 |
git shortlog |
按作者分组的提交摘要 |
-n 按提交数排序
-s 仅显示统计 |
生成变更日志 |
高级历史查看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
git log --follow -p <file>
git log master..feature-branch
git log --author="John Doe"
git log --grep="bug fix"
git log --since="2023-01-01" --until="2023-12-31"
|
版本回退与重置
| 命令 |
功能说明 |
常用参数 |
使用场景 |
git reset |
重置到指定状态 |
--soft 保留工作区和暂存区
--mixed 保留工作区
--hard 完全重置 |
撤销提交 |
git revert |
创建反向提交 |
--no-edit 不编辑提交信息
-n 不自动提交 |
安全撤销 |
git checkout |
切换分支或恢复文件 |
<file> 恢复文件
<commit> -- <file> 恢复到指定版本 |
恢复文件 |
git reflog |
查看引用日志 |
--all 显示所有引用 |
恢复丢失提交 |
回退场景示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| git reset --soft HEAD~1
git reset --hard HEAD~1
git reset --hard HEAD~3
git reset --hard <commit-id>
git revert HEAD git revert <commit-id>
git reflog git reset --hard <commit-id>
|
4. 分支管理策略 🌿
分支基础操作
| 命令 |
功能说明 |
常用参数 |
使用场景 |
git branch |
分支管理 |
-a 显示所有分支
-r 显示远程分支
-d 删除分支
-D 强制删除
-m 重命名分支 |
管理分支 |
git checkout |
切换分支 |
-b 创建并切换
- 切换到上一个分支
-t 跟踪远程分支 |
分支切换 |
git switch |
切换分支(新命令) |
-c 创建并切换
- 切换到上一个分支 |
现代分支切换 |
git branch --set-upstream-to |
设置上游分支 |
origin/<branch> 指定远程分支 |
关联远程分支 |
分支合并与变基
| 命令 |
功能说明 |
常用参数 |
使用场景 |
git merge |
合并分支 |
--no-ff 禁用快进合并
--squash 压缩合并
--abort 中止合并 |
整合分支 |
git rebase |
变基操作 |
-i 交互式变基
--onto 指定新基点
--abort 中止变基
--continue 继续变基 |
整理提交历史 |
git cherry-pick |
挑选提交 |
-n 不自动提交
-x 记录原提交信息 |
选择性合并 |
分支工作流示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| git checkout -b feature/user-auth git switch -c feature/user-auth
git branch -a
git branch -m old-name new-name
git branch -d feature/completed-feature git branch -D feature/abandoned-feature
git push origin --delete feature/old-feature
git branch --set-upstream-to=origin/main main
git checkout main git merge --no-ff feature/user-auth
git merge --squash feature/user-auth git commit -m "feat: 添加用户认证功能"
git checkout feature/user-auth git rebase main git checkout main git merge feature/user-auth
git rebase -i HEAD~3
git cherry-pick <commit-id>
|
5. 远程仓库协作 🌐
远程仓库管理
| 命令 |
功能说明 |
常用参数 |
使用场景 |
git remote |
管理远程仓库 |
-v 显示详细信息
add <name> <url> 添加远程仓库
remove <name> 删除远程仓库
rename <old> <new> 重命名 |
配置远程仓库 |
git remote show |
显示远程仓库信息 |
<remote-name> 指定远程仓库 |
查看远程状态 |
git remote prune |
清理远程分支引用 |
<remote-name> 指定远程仓库 |
清理无效引用 |
数据同步
| 命令 |
功能说明 |
常用参数 |
使用场景 |
git fetch |
获取远程更新 |
--all 获取所有远程分支
--prune 清理无效引用
--tags 获取标签 |
同步远程数据 |
git pull |
拉取并合并 |
--rebase 使用变基合并
--ff-only 仅快进合并
--no-ff 禁用快进 |
更新本地分支 |
git push |
推送到远程 |
-u 设置上游分支
--force-with-lease 安全强推
--tags 推送标签
--delete 删除远程分支 |
发布更改 |
协作场景
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| git remote add origin https://github.com/user/repo.git git remote add upstream https://github.com/original/repo.git
git remote -v git remote show origin
git push -u origin main
git fetch origin git fetch --all
git pull origin main git pull --rebase origin main
git push origin feature-branch
git push --force-with-lease origin main
git push --tags git push origin v1.0.0
git push origin --delete feature-branch
git remote prune origin
|
6. 标签管理 🏷️
标签操作
| 命令 |
功能说明 |
常用参数 |
使用场景 |
git tag |
标签管理 |
-a 创建注释标签
-d 删除标签
-l 列出标签
-f 强制创建 |
版本标记 |
git tag -l |
列出标签 |
"v1.*" 模式匹配 |
查找特定标签 |
git show <tag> |
显示标签信息 |
无特殊参数 |
查看标签详情 |
git push --tags |
推送标签 |
--follow-tags 仅推送注释标签 |
发布版本 |
标签使用示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| git tag v1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"
git tag -a v0.9.0 <commit-id> -m "Version 0.9.0"
git tag git tag -l "v1.*"
git show v1.0.0
git push origin v1.0.0 git push --tags
git tag -d v1.0.0
git push origin --delete v1.0.0
git checkout -b version-1.0.0 v1.0.0
|
7. 暂存与恢复 💾
暂存操作
| 命令 |
功能说明 |
常用参数 |
使用场景 |
git stash |
暂存当前工作 |
push -m "message" 添加描述
-u 包含未跟踪文件
-a 包含所有文件 |
临时保存工作 |
git stash list |
列出暂存列表 |
无特殊参数 |
查看暂存记录 |
git stash pop |
恢复并删除暂存 |
stash@{n} 指定暂存 |
恢复工作状态 |
git stash apply |
恢复但保留暂存 |
stash@{n} 指定暂存 |
重复使用暂存 |
git stash drop |
删除暂存 |
stash@{n} 指定暂存 |
清理暂存 |
git stash clear |
清空所有暂存 |
无特殊参数 |
清理所有暂存 |
暂存使用场景
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| git stash git stash push -m "临时保存:修复bug前的工作状态"
git stash -u
git stash list
git stash pop
git stash apply stash@{1}
git stash show git stash show -p stash@{1}
git stash drop stash@{1}
git stash clear
git stash branch new-feature stash@{1}
|
8. 子模块管理 📦
子模块操作
| 命令 |
功能说明 |
常用参数 |
使用场景 |
git submodule add |
添加子模块 |
<url> <path> 指定URL和路径 |
引入外部项目 |
git submodule init |
初始化子模块 |
无特殊参数 |
初始化配置 |
git submodule update |
更新子模块 |
--recursive 递归更新
--remote 更新到远程最新 |
同步子模块 |
git submodule foreach |
对所有子模块执行命令 |
<command> 指定命令 |
批量操作 |
子模块使用示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| git submodule add https://github.com/user/library.git libs/library
git clone --recursive https://github.com/user/project.git
git submodule init git submodule update
git submodule update --remote
git submodule foreach git pull origin main
git submodule deinit libs/library git rm libs/library
|
9. 实用技巧与别名配置 ⚡
推荐别名配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.unstage 'reset HEAD --' git config --global alias.last 'log -1 HEAD' git config --global alias.visual '!gitk'
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" git config --global alias.ll "log --oneline --graph --decorate --all" git config --global alias.ls "log --pretty=format:'%C(yellow)%h %C(blue)%ad %C(red)%d %C(reset)%s %C(green)[%cn]' --decorate --date=short" git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"
git config --global alias.amend 'commit --amend --no-edit' git config --global alias.uncommit 'reset --soft HEAD~1' git config --global alias.unstage 'reset HEAD --' git config --global alias.discard 'checkout --' git config --global alias.graph 'log --graph --oneline --decorate --all' git config --global alias.aliases "config --get-regexp '^alias\.'"
|
.gitignore 常用模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| node_modules/ vendor/ bower_components/
dist/ build/ out/ target/ *.min.js *.min.css
*.log logs/ npm-debug.log* yarn-debug.log* yarn-error.log*
.DS_Store .DS_Store? ._* .Spotlight-V100 .Trashes ehthumbs.db Thumbs.db
.vscode/ .idea/ *.swp *.swo *~ .project .classpath .settings/
.env .env.local .env.development.local .env.test.local .env.production.local
.cache/ .parcel-cache/ .next/ .nuxt/
coverage/ *.lcov
*.tmp *.temp .tmp/
|
Git钩子示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
npm run lint if [ $? -ne 0 ]; then echo "代码检查失败,请修复后再提交" exit 1 fi
commit_regex='^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .{1,50}' if ! grep -qE "$commit_regex" "$1"; then echo "提交信息格式错误,请使用: type(scope): description" exit 1 fi
|
10. 常见问题解决方案 🔧
典型场景处理
撤销最后一次提交但保留更改
修改已推送的提交信息
1 2
| git commit --amend -m "新的提交信息" git push --force-with-lease
|
合并多个提交
解决合并冲突
1 2 3 4 5 6 7 8 9
| git status
git add <resolved-file> git commit
git merge --abort
|
恢复误删的分支
1 2 3 4 5
| git reflog
git checkout -b <branch-name> <commit-id>
|
清理本地分支
1 2 3 4 5
| git branch --merged | grep -v "\*\|main\|master" | xargs -n 1 git branch -d
git remote prune origin
|
撤销git add
1 2 3 4 5
| git reset HEAD
git reset HEAD <file>
|
修改历史提交
1 2 3 4 5 6
| git rebase -i HEAD~3
git commit --fixup <commit-id> git rebase -i --autosquash HEAD~3
|
11. 高级技巧 🎯
搜索与查找
1 2 3 4 5 6 7 8 9 10 11 12 13
| git log -S "function_name" --source --all
git log --grep="bug fix" --oneline
git bisect start git bisect bad HEAD git bisect good v1.0.0
git grep "TODO" HEAD~5
|
性能优化
1 2 3 4 5 6 7 8 9
| git gc --aggressive --prune=now
git count-objects -vH
git reflog expire --expire=now --all git gc --prune=now
|
工作流集成
1 2 3 4 5 6 7 8 9 10 11 12
| git flow init
git flow feature start new-feature
git flow feature finish new-feature
git flow release start 1.0.0 git flow release finish 1.0.0
|
12. 学习资源推荐 📚
官方资源
交互式学习
速查手册
进阶阅读
- 《Pro Git》书籍 - Scott Chacon & Ben Straub
- 《Git版本控制管理》- Jon Loeliger & Matthew McCullough
- 《Git权威指南》- 蒋鑫
💡 小贴士: 建议将常用命令设置为别名,并定期练习分支操作和冲突解决,这样能大大提升日常开发效率!
🔖 快速查找: 使用 Ctrl+F 搜索特定命令或功能关键词,快速定位所需内容。
⚡ 效率提升: 掌握这些命令后,建议学习Git Flow工作流和语义化提交规范,进一步提升团队协作效率。