DEV Community

WDSEGA
WDSEGA

Posted on

Git高级技巧:10个提升效率的命令组合

Git高级技巧:10个提升效率的命令组合

Git是每个开发者的必备工具,但很多人只使用了它的基础功能。本文将介绍10个高级Git技巧,大幅提升你的开发效率。

1. 交互式暂存(Interactive Staging)

当你修改了多个文件,但只想提交部分修改时:

# 交互式选择要暂存的修改
git add -p

# 或者交互式选择文件
git add -i
Enter fullscreen mode Exit fullscreen mode

交互式暂存会逐块显示修改,你可以选择:

  • y - 暂存此块
  • n - 不暂存此块
  • s - 分割成更小的块
  • e - 手动编辑此块
  • q - 退出

实际应用场景

# 你修改了10个文件,其中3个是功能代码,7个是格式调整
# 只想提交功能代码
git add -p
# 逐个选择功能相关的修改块
Enter fullscreen mode Exit fullscreen mode

2. 智能暂存(Stash with Keep Index)

在开发过程中需要切换分支,但当前工作未完成:

# 基础stash
git stash

# 带描述的stash
git stash save "WIP: 用户登录功能"

# stash同时保留暂存区
git stash --keep-index

# 只stash未暂存的修改
git stash --only-untracked

# 恢复最近的stash
git stash pop

# 恢复但不删除stash
git stash apply

# 查看stash列表
git stash list

# 从stash创建分支
git stash branch feature-branch
Enter fullscreen mode Exit fullscreen mode

Stash工作流示例

# 正在开发功能A,突然需要修复紧急bug
git stash save "功能A开发中"
git checkout -b hotfix/urgent-bug
# 修复bug...
git commit -am "修复紧急bug"
git checkout main
git merge hotfix/urgent-bug
git stash pop  # 继续功能A开发
Enter fullscreen mode Exit fullscreen mode

3. 自动变基(Auto Rebase)

保持提交历史整洁:

# 拉取并变基
git pull --rebase

# 设置默认变基
git config --global pull.rebase true

# 交互式变基最近3个提交
git rebase -i HEAD~3

# 变基到特定提交
git rebase -i abc123
Enter fullscreen mode Exit fullscreen mode

交互式变基操作

# 执行 git rebase -i HEAD~3 后的编辑器内容
pick abc123 第一个提交
squash def456 第二个提交  # 合并到前一个
reword ghi789 第三个提交   # 修改提交信息
edit jkl012 第四个提交     # 暂停以修改
drop mno345 第五个提交     # 删除此提交
Enter fullscreen mode Exit fullscreen mode

4. 选择性合并(Cherry-pick)

只合并特定的提交:

# 合并单个提交
git cherry-pick abc123

# 合并多个提交
git cherry-pick abc123 def456 ghi789

# 合并提交范围
git cherry-pick abc123..def456

# 只应用修改但不提交
git cherry-pick -n abc123

# 解决冲突后继续
git cherry-pick --continue
Enter fullscreen mode Exit fullscreen mode

实际应用场景

# 从release分支紧急修复应用到develop分支
git checkout develop
git cherry-pick release/bugfix-commit-hash
Enter fullscreen mode Exit fullscreen mode

5. 提交搜索与定位

快速找到需要的提交:

# 搜索提交信息
git log --grep="修复"

# 搜索代码变更
git log -S "function_name"

# 搜索正则表达式
git log -G "pattern.*"

# 查看谁修改了某行代码
git blame -L 10,20 file.js

# 二分查找定位问题提交
git bisect start
git bisect bad          # 当前版本有问题
git bisect good v1.0    # v1.0版本正常
# Git会自动定位到问题提交
Enter fullscreen mode Exit fullscreen mode

Bisect自动化

# 自动运行测试脚本定位问题
git bisect start HEAD v1.0
git bisect run npm test
Enter fullscreen mode Exit fullscreen mode

6. 差异比较技巧

# 比较两个分支
git diff main..feature

# 比较暂存区和工作区
git diff

# 比暂存区和上次提交
git diff --cached

# 只看文件名
git diff --name-only main..feature

# 查看统计信息
git diff --stat main..feature

# 比较特定文件的历史版本
git diff abc123 def456 -- path/to/file.js

# 词级别的差异
git diff --word-diff
Enter fullscreen mode Exit fullscreen mode

高级diff输出

# 使用更好的diff工具
git difftool -t meld main..feature

# 设置默认diff工具
git config --global diff.tool meld
Enter fullscreen mode Exit fullscreen mode

7. 历史重写

修正历史提交:

# 修改最近一次提交信息
git commit --amend -m "新的提交信息"

# 修改最近一次提交的作者
git commit --amend --author="Name <email>"

# 重置到指定提交(保留修改)
git reset --soft abc123

# 重置到指定提交(丢弃修改)
git reset --hard abc123

# 撤销特定提交(创建新提交)
git revert abc123
Enter fullscreen mode Exit fullscreen mode

批量修改历史

# 批量修改提交信息
git filter-branch --msg-filter 'sed "s/bug/fix/"' HEAD~5..HEAD

# 批量修改作者
git filter-branch --env-filter '
OLD_EMAIL="old@example.com"
CORRECT_NAME="New Name"
CORRECT_EMAIL="new@example.com"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
' HEAD~10..HEAD
Enter fullscreen mode Exit fullscreen mode

8. 子模块管理

管理项目依赖:

# 添加子模块
git submodule add https://github.com/user/repo.git lib/repo

# 初始化子模块
git submodule init
git submodule update

# 克隆包含子模块的项目
git clone --recursive https://github.com/user/main-repo.git

# 更新所有子模块
git submodule update --remote

# 对所有子模块执行命令
git submodule foreach 'git checkout main'
Enter fullscreen mode Exit fullscreen mode

子模块常见问题解决

# 子模块显示为空目录
git submodule update --init --recursive

# 子模块版本不一致
git submodule sync
git submodule update --init --recursive
Enter fullscreen mode Exit fullscreen mode

9. 工作流快捷命令

# 创建并切换分支
git checkout -b feature/new-feature

# 切换到上一个分支
git checkout -

# 删除已合并的分支
git branch -d feature/merged-branch

# 强制删除分支
git branch -D feature/unmerged-branch

# 删除远程分支
git push origin --delete feature/old-branch

# 清理已删除的远程分支引用
git fetch --prune

# 查看所有分支(包括远程)
git branch -a

# 查看分支追踪关系
git branch -vv
Enter fullscreen mode Exit fullscreen mode

📌 更多精彩内容,关注我的博客,每周更新!

Top comments (0)