Git 命令速查手册
整理开发常用Git全套命令,包含配置、仓库、文件、分支、远程、标签、储藏、重置、变基等操作,适合开发人员快速查阅使用。
1 分钟阅读194 字
一、基础配置命令
1. 配置用户身份
Bash
# 配置全局用户名
git config --global user.name "Your Full Name"
# 配置全局用户邮箱
git config --global user.email "your_email@domain.com"
2. 查看配置详情
Bash
# 列出所有 Git 配置项
git config --list
# 查看当前配置的用户名
git config user.name
# 查看当前配置的用户邮箱
git config user.email
3. 配置命令别名
Bash
# 简化 git commit 为 git ci
git config --global alias.ci commit
# 简化 git checkout 为 git co
git config --global alias.co checkout
# 简化 git branch 为 git br
git config --global alias.br branch
二、仓库核心操作
1. 初始化/克隆仓库
Bash
# 在当前目录创建新的 Git 仓库
git init
# 克隆远程仓库(HTTPS 方式)
git clone https://github.com/username/repo.git
# 克隆远程仓库(SSH 方式)
git clone git@github.com:username/repo.git
# 克隆远程仓库并指定本地目录名
git clone <远程仓库地址> <本地目录名>
2. 查看仓库状态与历史
Bash
# 查看工作区和暂存区的详细状态
git status
# 查看简洁的状态信息
git status -s
# 查看完整的提交历史
git log
# 查看最近 n 条提交记录
git log -n 5
# 查看提交历史并显示文件修改统计
git log --stat
# 以图形化展示分支合并历史
git log --graph --oneline --all
三、文件修改与提交
1. 暂存文件操作
Bash
# 暂存指定单个文件
git add filename.txt
# 暂存指定目录下的所有文件
git add src/
# 暂存所有修改、新增的文件
git add .
# 交互式暂存文件(可选择部分修改)
git add -p
2. 撤销暂存/修改
Bash
# 撤销指定文件的暂存状态
git restore --staged filename.txt
# 撤销工作区指定文件的所有修改
git checkout -- filename.txt
# 恢复工作区到上一次提交状态(谨慎使用)
git restore .
3. 提交修改到本地仓库
Bash
# 提交暂存区文件并添加提交信息
git commit -m "feat: 新增用户登录功能"
# 提交时包含所有已跟踪文件的修改(跳过暂存)
git commit -a -m "fix: 修复登录验证逻辑bug"
# 修改最后一次提交的信息
git commit --amend -m "feat: 优化用户登录流程"
四、分支管理命令
1. 分支查看与创建
Bash
# 列出本地所有分支(当前分支标*)
git branch
# 列出所有本地和远程分支
git branch -a
# 查看分支的最后一次提交
git branch -v
# 创建新分支
git branch feature/user-profile
# 创建并切换到新分支
git checkout -b feature/user-profile
# 基于远程分支创建本地分支
git checkout -b dev origin/dev
2. 分支切换与合并
Bash
# 切换到指定分支
git checkout main
# 切换到上一个分支
git checkout -
# 将指定分支合并到当前分支
git merge feature/user-profile
# 以快进模式合并(无冲突时)
git merge --ff-only feature/user-profile
# 合并时生成合并提交(强制保留分支记录)
git merge --no-ff feature/user-profile
3. 分支删除与清理
Bash
# 删除已合并的本地分支
git branch -d feature/user-profile
# 强制删除未合并的本地分支
git branch -D feature/user-profile
# 删除远程分支
git push origin --delete feature/user-profile
# 清理本地无效的远程追踪分支
git remote prune origin
五、远程仓库交互
1. 远程仓库管理
Bash
# 添加远程仓库(默认别名origin)
git remote add origin <远程仓库地址>
# 查看远程仓库信息
git remote -v
# 查看指定远程仓库的详细信息
git remote show origin
# 修改远程仓库地址
git remote set-url origin <新的远程仓库地址>
# 删除指定远程仓库
git remote remove origin
2. 拉取/推送代码
Bash
# 拉取远程仓库指定分支的代码并合并
git pull origin main
# 拉取远程代码但不自动合并
git fetch origin
# 将本地分支推送到远程仓库
git push origin main
# 首次推送时关联本地与远程分支
git push -u origin main
# 强制推送(谨慎使用,会覆盖远程提交)
git push -f origin main
六、标签管理命令
1. 创建与查看标签
Bash
# 创建轻量级标签(仅版本号)
git tag v1.0.0
# 创建带附注的标签(推荐)
git tag -a v1.0.0 -m "发布v1.0.0版本,包含核心功能"
# 为指定提交记录创建标签
git tag -a v0.9.0 <commit-hash> -m "修复初始版本bug"
# 查看所有标签
git tag
# 查看标签详细信息
git show v1.0.0
2. 推送/删除标签
Bash
# 推送指定标签到远程仓库
git push origin v1.0.0
# 推送所有本地标签到远程仓库
git push origin --tags
# 删除本地标签
git tag -d v1.0.0
# 删除远程仓库标签
git push origin --delete tag v1.0.0
七、高级实用命令
1. 暂存工作区修改
Bash
# 暂存当前工作区的所有修改
git stash
# 暂存时添加备注
git stash save "未完成的用户资料页面开发"
# 查看所有暂存记录
git stash list
# 应用最近一次暂存(保留暂存记录)
git stash apply
# 应用最近一次暂存并删除记录
git stash pop
# 应用指定的暂存记录
git stash apply stash@{1}
# 删除所有暂存记录
git stash clear
2. 版本回退与重置
Bash
# 查看提交版本号(前几位即可)
git log --oneline
# 软重置:回退版本,保留工作区和暂存区
git reset --soft <commit-hash>
# 混合重置(默认):回退版本,保留工作区,清空暂存区
git reset --mixed <commit-hash>
# 硬重置:彻底回退到指定版本,清空工作区和暂存区(谨慎)
git reset --hard <commit-hash>
# 回退到上一个版本
git reset --hard HEAD^
# 撤销最近一次提交,但保留修改内容
git reset --soft HEAD~1
3. 变基操作
Bash
# 将当前分支基于指定分支变基
git rebase main
# 交互式变基(可编辑提交历史,如合并、修改提交信息)
git rebase -i HEAD~3
# 解决变基冲突后继续变基
git add . && git rebase --continue
# 放弃变基操作
git rebase --abort
4. 其他常用操作
Bash
# 查看文件的修改记录(追溯每一行的修改)
git blame filename.txt
# 查看两个提交之间的差异
git diff <commit-hash1> <commit-hash2>
# 查看暂存区与工作区的差异
git diff
# 查看暂存区与最后一次提交的差异
git diff --staged
# 清理工作区未跟踪的文件(谨慎使用)
git clean -fd
版权声明 · CC BY-NC-ND 4.0
署名-非商业性使用-禁止演绎 4.0 国际
评论
由 GitHub Discussions 驱动