Git命令
> 本手册涵盖了日常开发中最常用的 Git 操作,从基础提交到高级子模块管理,附带实用说明和注意事项。基础操作
# 初始化一个新的 Git 仓库
git init
# 克隆远程仓库到本地
git clone <repository_url> [local_directory]
# 查看当前工作区状态(哪些文件被修改、暂存等)
git status
# 将文件添加到暂存区(即“追踪”该文件)
git add <file_or_directory>
# 添加所有变更(新增、修改、删除)
git add -A
# 添加当前目录下所有变更,但不包含删除的文件
git add .
# 提交暂存区内容,并附加提交信息
git commit -m "commit message"
# 跳过暂存区,直接提交所有已追踪文件的修改(慎用)
git commit -a -m "message"
# 推送本地提交到远程仓库
git push <remote_name> <branch_name>
# 首次推送并设置上游分支
git push -u origin main
# 从远程仓库拉取更新并合并到当前分支
git pull <remote_name> <branch_name>
# 只拉取不合并(相当于 fetch)
git fetch <remote_name>
# 查看提交历史
git log
# 简洁单行显示
git log --oneline
# 图形化显示分支结构
git log --graph --oneline --all
分支管理
# 列出所有本地分支
git branch
# 列出所有远程分支
git branch -r
# 列出所有分支(本地+远程)
git branch -a
# 创建新分支(但不切换)
git branch <branch_name>
# 创建并切换到新分支
git checkout -b <branch_name>
# 切换到指定分支
git checkout <branch_name>
# 或使用新式命令
git switch <branch_name>
# 删除本地分支(已合并)
git branch -d <branch_name>
# 强制删除(未合并也会删,谨慎)
git branch -D <branch_name>
# 删除远程分支
git push origin --delete <branch_name>
# 重命名当前分支
git branch -m <new_name>
# 重命名指定分支
git branch -m <old_name> <new_name>
# 删除本地存在但远程已不存在的分支
git remote prune origin
# 删除已经合并到 master 的分支(Linux/Mac 可用)
git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d
# 查看远程分支和本地分支的追踪关系
git remote show origin
提交修改
# 重写最后一次提交信息
git commit --amend -m "new message"
# 修改最新的提交而不更改提交消息(比如补充漏掉的文件)
git add forgotten_file
git commit --amend --no-edit
# 撤销暂存区的文件(即取消 add)
git reset HEAD <file>
# 撤销工作区的修改(慎用,会丢失未提交的改动)
git checkout -- <file>
# 或使用新式命令
git restore <file>
# 回退到某个提交(保留工作区修改)
git reset --soft <commit_hash>
# 回退并丢弃工作区修改(危险)
git reset --hard <commit_hash>
# 撤销某个提交,并生成一个新的反向提交(安全撤销,保留历史)
git revert <commit_hash>
合并与变基
# 合并指定分支到当前分支
git merge <branch_name>
# 以变基方式合并(使提交历史线性)
git rebase <base_branch>
# 交互式变基(用于合并、修改、删除提交,通常用于整理历史)
git rebase -i HEAD~n # 最近 n 个提交
# 变基过程中中止
git rebase --abort
# 变基后继续(解决冲突后执行)
git add .
git rebase --continue
# 合并时采用递归策略,自动压缩提交(用于 pull 时)
git pull --rebase
冲突解决
当合并或变基出现冲突时,Git 会标记冲突文件,需要手动编辑解决。
# 查看冲突文件列表
git status
# 手动编辑冲突文件(删除 <<<<<<<, =======, >>>>>>> 标记,保留最终内容)
# 编辑完成后,添加文件到暂存区
git add <conflicted_file>
# 如果是合并冲突,解决后执行:
git commit -m "merge: resolve conflicts"
# 如果是变基冲突,解决后执行:
git rebase --continue
# 若想放弃本次合并,恢复到合并前状态
git merge --abort
# 若想放弃本次变基
git rebase --abort
# 使用工具对比冲突(如 meld, kdiff3 等)
git mergetool
配置与优化
# 获取配置帮助信息
git help config
# 配置全局用户名
git config --global user.name "Your Name"
# 配置全局邮箱
git config --global user.email "[email protected]"
# 配置全局颜色,对输出进行美化
git config --global color.ui auto
# 在文本编辑器中编辑全局配置文件
git config --global --edit
# 删除全局配置项
git config --global --unset <entry-name>
# 查看当前仓库的所有配置(包括本地和全局)
git config --list
# 解决中文文件名乱码
git config --global core.quotepath false
# 忽略文件权限变化(避免因 chmod 产生无关改动)
git config core.fileMode false
# 设置大小写敏感(Windows/macOS 默认不敏感)
git config core.ignorecase false # 设置为 true 表示不敏感
# 若远程有两个同名的不同大小写目录,可先清除缓存再提交
git rm -r --cached <dirOrFile>
代理配置
# 查看当前代理设置
git config --global http.proxy
git config --global https.proxy
git config --global socks.proxy
# 设置全局 HTTP/HTTPS 代理(适用于 privoxy 等转换工具)
git config --global http.proxy http://127.0.0.1:1080
git config --global https.proxy http://127.0.0.1:1080
git config --global socks.proxy 127.0.0.1:1080
# 取消全局代理
git config --global --unset http.proxy
git config --global --unset https.proxy
git config --global --unset socks.proxy
# 仅对 GitHub 设置代理(SOCKS5)
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
git config --global https.https://github.com.proxy socks5://127.0.0.1:1080
# 取消 GitHub 代理
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy
贮藏与标签
# 临时贮藏当前工作区修改(包括未追踪文件可选)
git stash save "description"
# 或简写
git stash
# 列出所有贮藏
git stash list
# 应用最近的贮藏(不删除贮藏记录)
git stash apply
# 应用并删除贮藏
git stash pop
# 删除某个贮藏
git stash drop stash@{n}
# 清空所有贮藏
git stash clear
# 创建轻量标签
git tag <tag_name>
# 创建附注标签(包含作者、日期等信息)
git tag -a <tag_name> -m "tag message"
# 推送标签到远程
git push origin <tag_name>
# 推送所有本地标签
git push origin --tags
# 删除本地标签
git tag -d <tag_name>
# 删除远程标签
git push origin --delete <tag_name>
统计与查询
# 统计当前分支的总提交数
git log --oneline | wc -l
# 查看某文件的每一行最近由谁修改
git blame <file-name>
# 查看仓库中所有对象的总大小(压缩后)
git count-objects -vH
# 查看工作区所有文件的大小
git ls-files | xargs -r du -hs
# 在提交日志中搜索指定文本(支持正则)
git log --all --grep='<given-text>'
# 按作者统计代码增删行数(需替换 username)
git log --author="username" --pretty=tformat: --numstat | awk \
'{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
# 统计每位作者的增删行数
git log --format='%aN' | sort -u |\
while read name; do echo -en "$name\t";\
git log --author="$name" --pretty=tformat: --numstat | awk \
'{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done
# 查看提交者排名(取前10)
git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 10
子模块 (Submodule)
子模块允许将一个仓库作为另一个仓库的子目录,适合管理依赖项目。
# 克隆包含子模块的仓库(同时初始化并更新子模块)
git clone <repo_url> --recursive
# 查看当前仓库中的子模块状态
git submodule status
# 添加新子模块
git submodule add <repo_url> <submodule_path>
# 初始化子模块(通常在 clone 后未用 --recursive 时执行)
git submodule init
# 更新子模块到父仓库记录的版本
git submodule update
# 更新到远程子模块的最新提交
git submodule update --remote
# 删除子模块(较新版本)
git submodule deinit <path_to_submodule>
git rm <path_to_submodule>
子模块进阶操作
# 进入子模块目录,切换到特定提交
cd <path_to_submodule>
git checkout <commit_hash>
# 在父仓库中记录子模块的新提交
cd <path_to_submodule>
git checkout <commit_hash>
cd ..
git add <path_to_submodule>
git commit -m "update submodule"
# 切换父仓库到某次提交,并更新子模块
git checkout <commit_hash>
git submodule update --remote
# 将子模块更新到最新标签
cd <path_to_submodule>
git fetch --tags
git checkout $(git describe --tags $(git rev-list --tags --max-count=1))
cd ..
git add <path_to_submodule>
git commit -m "update submodule to latest tag"
# 对所有子模块执行同一命令(递归)
git submodule foreach --recursive '<command>'
# 示例:拉取所有子模块的主分支最新代码
git submodule foreach --recursive git pull origin main
# 示例:重置所有子模块到父仓库记录的提交(丢弃本地修改)
git submodule foreach --recursive git checkout .
git submodule foreach --recursive git reset --hard origin/main # 强制重置到远程
其他实用命令
# 查看远程仓库信息
git remote -v
# 添加远程仓库
git remote add <name> <url>
# 修改远程仓库 URL
git remote set-url <name> <new_url>
# 查看某个命令的详细帮助
git help <command>
# 例如:git help merge
# 清理工作区未追踪的文件(-n 预览,-f 真正删除)
git clean -n
git clean -f
# 查看两个分支的差异
git diff <branch1> <branch2>
# 查看某次提交的改动详情
git show <commit_hash>
# 二分查找定位引入 bug 的提交
git bisect start
git bisect bad <bad_commit>
git bisect good <good_commit>
# 之后按提示标记每次测试结果,直到找到问题提交
提示:以上命令均可在 Git Bash 或终端中运行。部分命令(如
git switch、git restore)需要 Git 2.23 以上版本,若版本较低请使用checkout替代。
评论