1219 字
6 分钟
Git Help
我来为您整理一份全面的 Git 指令集,涵盖从基础到高级的各种操作场景。
🚀 Git 指令集大全
一、基础配置
# 配置用户名和邮箱git config --global user.name "Your Name"git config --global user.email "your.email@example.com"
# 查看配置git config --listgit config user.name
# 设置默认编辑器git config --global core.editor "vim"
# 设置别名(快捷命令)git config --global alias.st statusgit config --global alias.co checkoutgit config --global alias.br branchgit config --global alias.ci commit二、仓库操作
# 初始化新仓库git init
# 克隆远程仓库git clone <repository-url>git clone <repository-url> <directory-name> # 指定目录名
# 查看远程仓库地址git remote -v
# 添加远程仓库git remote add origin <repository-url>
# 修改远程仓库地址git remote set-url origin <new-url>三、基本工作流程
# 查看仓库状态git status
# 添加文件到暂存区git add <file-name> # 添加指定文件git add . # 添加所有修改git add -A # 添加所有变化(包括删除)
# 提交更改git commit -m "提交信息"git commit -am "提交信息" # 跳过 add,直接提交已跟踪文件
# 查看提交历史git loggit log --oneline # 简洁模式git log --graph --oneline # 图形化显示分支git log -n 5 # 最近5条四、分支管理
# 查看分支git branch # 本地分支git branch -a # 所有分支(含远程)git branch -r # 仅远程分支
# 创建分支git branch <branch-name> # 创建新分支git checkout -b <branch-name> # 创建并切换git switch -c <branch-name> # 新方式(Git 2.23+)
# 切换分支git checkout <branch-name>git switch <branch-name> # 新方式
# 合并分支git merge <branch-name> # 合并指定分支到当前分支
# 删除分支git branch -d <branch-name> # 删除已合并分支git branch -D <branch-name> # 强制删除
# 重命名分支git branch -m <old-name> <new-name>五、远程同步
# 拉取代码git pull origin <branch>git pull --rebase origin <branch> # 使用 rebase 方式
# 推送代码git push origin <branch>git push -u origin <branch> # 首次推送并关联git push origin --delete <branch> # 删除远程分支
# 获取远程更新(不合并)git fetch origin
# 同步远程分支列表git remote prune origin六、撤销与回退
# 撤销工作区修改git checkout -- <file> # 丢弃文件修改git restore <file> # 新方式(Git 2.23+)
# 撤销暂存区git reset HEAD <file> # 取消暂存git restore --staged <file> # 新方式
# 版本回退git reset --soft HEAD~1 # 回退到上一版本,保留修改到暂存区git reset --mixed HEAD~1 # 回退到上一版本,保留修改到工作区(默认)git reset --hard HEAD~1 # 彻底回退,丢弃修改
# 查看所有操作记录(用于找回)git reflog七、储藏(Stash)
# 临时保存修改git stashgit stash save "描述信息"
# 查看储藏列表git stash list
# 恢复储藏git stash pop # 恢复并删除git stash apply # 恢复不删除git stash apply stash@{0} # 恢复指定
# 删除储藏git stash drop stash@{0} # 删除指定git stash clear # 清空所有八、标签管理
# 创建标签git tag <tag-name> # 轻量标签git tag -a <tag-name> -m "描述" # 附注标签git tag <tag-name> <commit-id> # 给指定提交打标签
# 推送标签到远程git push origin <tag-name>git push origin --tags # 推送所有标签
# 删除标签git tag -d <tag-name>git push origin :refs/tags/<tag-name> # 删除远程标签九、高级操作
# 变基(Rebase)git rebase <branch> # 将当前分支变基到指定分支git rebase -i HEAD~3 # 交互式变基最近3个提交
# 拣选提交git cherry-pick <commit-id> # 将指定提交应用到当前分支
# 二分查找(定位问题提交)git bisect startgit bisect badgit bisect good <commit-id>
# 查看文件修改历史git blame <file>
# 查看某行代码最后修改者git blame -L 10,20 <file>十、常用组合场景
| 场景 | 命令组合 |
|---|---|
| 日常提交 | git add . → git commit -m "msg" → git push |
| 拉取最新代码 | git pull origin main |
| 创建功能分支 | git checkout -b feature/xxx → 开发 → git push -u origin feature/xxx |
| 合并功能分支 | git checkout main → git pull → git merge feature/xxx → git push |
| 临时保存工作 | git stash → 切换分支处理 → git checkout - → git stash pop |
| 撤销错误提交 | git reset --soft HEAD~1 → 修改后重新提交 |
十一、.gitignore 模板
# 依赖node_modules/vendor/
# 编译输出dist/build/*.exe*.dll
# IDE.idea/.vscode/*.iml
# 日志与临时文件*.log*.tmp.DS_Store
# 敏感信息.envconfig.local.php十二、实用技巧
# 查看某文件的修改历史git log -p <file>
# 查看两个分支差异git diff branch1 branch2
# 查看工作区与暂存区差异git diff
# 查看暂存区与最新提交差异git diff --cached
# 只查看文件名git diff --name-only
# 统计提交贡献git shortlog -sn
# 生成补丁文件git format-patch HEAD~3
# 应用补丁git apply patch-file如需了解某个指令的详细用法,可以使用 git <command> --help 查看官方文档。