git有5个提交,能合并第2 3个,或拆分第4个成两个吗

执行:

git rebase -i HEAD~5

会出现:

pick A
pick B
pick C
pick D
pick E

把第 3 个改成 squash(或 s):

pick A
pick B
squash C
pick D
pick E

 

之后 保存并退出编辑器 就可以了,Git 会自动继续执行 rebase。

 

//////////////////////////////////////

Git 会继续:

  1. 执行 B
  2. 把 C 合并到 B
  3. 弹出一个 commit message 编辑界面

类似:

# This is a combination of 2 commits.
# The first commit message:

B message

# The second commit message:

C message

你可以:

  • 保留 B (#add,类似vim操作,删除上面不是#开头的部分文字)
  • 删除 C
  • 或写一个新 message

 

然后再次:

:wq

完成。

 

///////////////////////////////////////

如果这个分支 之前已经 push 过,需要:

git push -f

或者更安全的:

git push --force-with-lease

推荐第二个,它可以防止 覆盖别人新提交

 

cherry pick:

git checkout dev_AIMSDraui_0331_bkp   

 

git fetch origin dev_draui_tmp_test

 

git cherry-pick ebf2c4251a81162a8d71f2971c8c0a1de97e5711

 

 

忽略某个文件的修改:

cd到 目标文件目录下:

git update-index --skip-worktree DrauiiLog.h    #忽略

git ls-files -v | grep '^S'     #查看

 

git update-index --no-skip-worktree DrauiiLog.h  #取消忽略

 

 

 

 

修改多个commit的message:

 

 

情况1:修改最近 3 个 commit 的 message(最常用)

假设 MR 分支上有 3 个 commit:

git log --oneline -3

例如:

abc111 fix bug A
def222 add feature B
ghi333 update C

执行:

git rebase -i HEAD~3

会进入编辑界面:

pick abc111 fix bug A
pick def222 add feature B
pick ghi333 update C

把需要修改 message 的 commit 前面的 pick 改成 reword(或 r):

reword abc111 fix bug A
pick def222 add feature B
reword ghi333 update C

保存退出。

Git 会依次让你修改 message。(#add,每个改完后:wq)


修改完成后强制推送

因为 commit hash 变了,需要:

git push --force-with-lease

不要直接用:

git push -f

--force-with-lease 更安全,避免覆盖别人新提交。

 

posted on 2026-07-07 09:48  maxweii  阅读(3)  评论(0)    收藏  举报