文章

Git常用命令

Git常用命令列表

基本操作

# 初始化新仓库 git init # 克隆代码库 git clone git@github.com:pnnh/ebase58shared.git # 克隆代码库到指定目录 git clone git@github.com:pnnh/ebase58shared.git ebase58shared # 克隆指定分支 git clone -b branch_name git@github.com:pnnh/ebase58shared.git # 查看当前状态 git status # 查看本地变更 git diff # 查看暂存区变更 git diff --cached # 查看工作区和HEAD的差异 git diff HEAD # 添加文件到暂存区 git add filename # 添加所有变更到暂存区 git add . # 交互式添加文件 git add -p # 提交变更 git commit -m "commit message" # 修改最后一次提交 git commit --amend # 添加并提交 git commit -am "commit message"

分支操作

# 查看所有分支 git branch -a # 查看本地分支 git branch # 查看远程分支 git branch -r # 创建新分支 git branch branch_name # 切换分支 git checkout branch_name # 创建并切换到新分支 git checkout -b branch_name # 使用switch命令切换分支(推荐) git switch branch_name # 创建并切换到新分支(使用switch) git switch -c branch_name # 删除本地分支 git branch -d branch_name # 强制删除本地分支 git branch -D branch_name # 删除远程分支 git push origin --delete branch_name # 重命名当前分支 git branch -m new_branch_name # 重命名指定分支 git branch -m old_branch_name new_branch_name # 查看分支的最后一次提交 git branch -v # 查看已合并到当前分支的分支 git branch --merged # 查看未合并到当前分支的分支 git branch --no-merged

远程仓库操作

# 查看远程仓库 git remote -v # 添加远程仓库 git remote add origin git@github.com:user/repo.git # 修改远程仓库地址 git remote set-url origin git@github.com:user/repo.git # 删除远程仓库 git remote remove origin # 重命名远程仓库 git remote rename old_name new_name # 拉取远程更新(不合并) git fetch origin # 拉取所有远程分支 git fetch --all # 拉取并合并远程更新 git pull origin branch_name # 拉取并变基 git pull --rebase origin branch_name # 推送到远程仓库 git push origin branch_name # 推送所有分支 git push --all origin # 强制推送(慎用) git push -f origin branch_name # 设置上游分支 git push -u origin branch_name # 查看远程仓库详细信息 git remote show origin

标签操作

# 查看所有标签 git tag # 创建轻量标签 git tag tag_name # 创建附注标签 git tag -a tag_name -m "tag message" # 为特定提交创建标签 git tag -a tag_name commit_hash # 查看标签信息 git show tag_name # 删除本地标签 git tag -d tag_name # 删除远程标签 git push origin --delete tag_name # 推送指定标签到远程 git push origin tag_name # 推送所有标签到远程 git push origin --tags # 检出标签 git checkout tag_name

日志查看

# 查看提交历史 git log # 查看简化的提交历史 git log --oneline # 查看最近n条提交 git log -n 5 # 查看图形化分支历史 git log --graph --oneline --all # 查看某个文件的提交历史 git log -- filename # 查看某个作者的提交 git log --author="author_name" # 查看指定时间范围的提交 git log --since="2 weeks ago" git log --after="2024-01-01" --before="2024-12-31" # 查看提交的详细变更 git log -p # 查看提交统计信息 git log --stat # 查看简短统计信息 git log --shortstat # 查看每次提交的文件变更 git log --name-status # 格式化输出 git log --pretty=format:"%h - %an, %ar : %s" # 查看引用日志(包括被删除的提交) git reflog

撤销和回退

# 撤销工作区的修改 git checkout -- filename # 使用restore撤销工作区修改(推荐) git restore filename # 撤销暂存区的文件 git reset HEAD filename # 使用restore撤销暂存(推荐) git restore --staged filename # 回退到上一次提交 git reset --soft HEAD^ # 回退到上一次提交并取消暂存 git reset --mixed HEAD^ # 回退到上一次提交并丢弃所有更改(慎用) git reset --hard HEAD^ # 回退到指定提交 git reset --hard commit_hash # 创建一个新提交来撤销指定提交 git revert commit_hash # 撤销多个提交 git revert commit_hash1..commit_hash2 # 强制撤销本地修改 git reset --hard # 清理未跟踪的文件(预览) git clean -n # 清理未跟踪的文件 git clean -f # 清理未跟踪的文件和目录 git clean -fd

合并操作

# 合并指定分支到当前分支 git merge branch_name # 合并时创建合并提交(即使可以快进) git merge --no-ff branch_name # 快进合并(如果可能) git merge --ff-only branch_name # 查看合并冲突的文件 git diff --name-only --diff-filter=U # 中止合并 git merge --abort # 使用ours策略合并 git merge -X ours branch_name # 使用theirs策略合并 git merge -X theirs branch_name

变基操作

# 变基到指定分支 git rebase branch_name # 交互式变基 git rebase -i HEAD~3 # 继续变基 git rebase --continue # 跳过当前提交 git rebase --skip # 中止变基 git rebase --abort # 变基到远程分支 git rebase origin/main

暂存操作

# 暂存当前工作区 git stash # 暂存时添加说明 git stash save "stash message" # 查看暂存列表 git stash list # 应用最近的暂存 git stash apply # 应用并删除最近的暂存 git stash pop # 应用指定的暂存 git stash apply stash@{n} # 删除最近的暂存 git stash drop # 删除指定的暂存 git stash drop stash@{n} # 清空所有暂存 git stash clear # 查看暂存的内容 git stash show # 查看暂存的详细差异 git stash show -p

文件操作

# 删除文件 git rm filename # 删除文件但保留在工作区 git rm --cached filename # 重命名/移动文件 git mv old_filename new_filename # 忽略已跟踪的文件 git update-index --assume-unchanged filename # 取消忽略已跟踪的文件 git update-index --no-assume-unchanged filename # 查看被忽略的文件 git ls-files -v | grep '^[a-z]'

配置操作

# 查看所有配置 git config --list # 查看全局配置 git config --global --list # 设置用户名 git config --global user.name "Your Name" # 设置邮箱 git config --global user.email "your.email@example.com" # 设置默认编辑器 git config --global core.editor "vim" # 设置别名 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 color.ui auto # 设置默认分支名 git config --global init.defaultBranch main # 查看某个配置项 git config user.name # 删除配置项 git config --global --unset user.name

子模块

# 添加子模块 git submodule add git@github.com:pnnh/ebase58shared.git pnnh/ebase58shared # 查看子模块状态 git submodule status # 递归同步子模块 git submodule sync —recursive # 递归初始化子模块 git submodule update --init —recursive

移除Git子模块的步骤

# 移除.git/config配置文件中的子模块配置 git submodule deinit -f path/to/submodule # 移除.git/modules下的子模块目录 rm -rf .git/modules/path/to/submodule # 移除.gitmodules文件中的子模块配置,并移除path/to/submodule目录 git rm -f path/to/submodule

特殊操作

# 获取当前分支的修订版本号 git rev-parse HEAD # 获取短版本号 git rev-parse --short HEAD # 强制撤销本地修改 git reset --hard # 查看某个文件的归属(blame) git blame filename # 查找引入某个字符串的提交 git log -S "search_string" # 查找包含某个字符串的提交信息 git log --grep="pattern" # 显示对象的类型和大小 git cat-file -t commit_hash git cat-file -s commit_hash # 显示文件的每一行最后修改的提交信息 git blame -L 10,20 filename # 比较两个提交之间的差异 git diff commit1 commit2 # 查看两个分支之间的差异 git diff branch1..branch2 # 查看文件在某个提交时的内容 git show commit_hash:filename # 列出所有冲突的文件 git diff --name-only --diff-filter=U # 使用归档命令导出项目 git archive --format=zip --output=project.zip HEAD # 统计代码贡献 git shortlog -sn # 查找丢失的提交 git fsck --lost-found # 优化仓库 git gc # 计算仓库大小 git count-objects -vH

推送命令

# 仅推送所有标签 git push origin --tags # 推送代码同时推送标签 git push --follow-tags

修改远程源

# 查看远程仓库地址 git remote -v # 修改远程仓库地址 git remote set-url origin new_url # 将HTTPS地址改为SSH地址 git remote set-url origin git@github.com:user/repo.git # 将SSH地址改为HTTPS地址 git remote set-url origin https://github.com/user/repo.git # 添加多个远程仓库 git remote add upstream git@github.com:original/repo.git # 从多个远程仓库拉取 git fetch upstream git merge upstream/main

Cherry-pick操作

# 将指定提交应用到当前分支 git cherry-pick commit_hash # 应用多个提交 git cherry-pick commit_hash1 commit_hash2 # 应用一个范围的提交 git cherry-pick commit_hash1..commit_hash2 # Cherry-pick时不自动提交 git cherry-pick -n commit_hash # 继续cherry-pick git cherry-pick --continue # 中止cherry-pick git cherry-pick --abort # 跳过当前提交 git cherry-pick --skip

比较和差异

# 比较工作区和暂存区 git diff # 比较暂存区和最后一次提交 git diff --cached # 比较工作区和最后一次提交 git diff HEAD # 比较两个分支 git diff branch1 branch2 # 只显示文件名 git diff --name-only # 显示统计信息 git diff --stat # 忽略空白字符的差异 git diff -w # 比较两个提交 git diff commit1 commit2

搜索和查找

# 在工作区搜索内容 git grep "search_term" # 在指定提交中搜索 git grep "search_term" commit_hash # 显示行号 git grep -n "search_term" # 显示匹配的文件名 git grep -l "search_term" # 统计匹配次数 git grep -c "search_term" # 搜索正则表达式 git grep -E "regex_pattern"

工作流相关

# 查看当前分支的上游分支 git branch -vv # 设置上游分支 git branch --set-upstream-to=origin/branch_name # 取消上游分支 git branch --unset-upstream # 同步fork的仓库 git fetch upstream git checkout main git merge upstream/main git push origin main

调试和诊断

# 二分查找引入bug的提交 git bisect start git bisect bad # 标记当前版本为有问题 git bisect good commit_hash # 标记某个版本为正常 # 测试后继续标记 git bisect good/bad # 结束二分查找 git bisect reset # 查看钩子 ls .git/hooks/ # 追踪文件的修改历史 git log --follow filename # 查看文件的完整历史(包括重命名) git log --all --full-history -- filename

性能和维护

# 垃圾回收 git gc # 积极的垃圾回收 git gc --aggressive # 修复损坏的仓库 git fsck # 验证仓库完整性 git fsck --full # 清理无法访问的对象 git prune # 查看仓库大小 du -sh .git # 压缩仓库 git repack -a -d --depth=250 --window=250
本文由作者按照 CC BY 4.0 进行授权