git squash commits

发布时间 2023-07-13 19:32:58作者: LogicBai

git squash commits

key words: git squash 删除 压缩 commit 提交

起因

在做新功能测试的时候在开发分支(branch dev)上创建了一个新的分支(branch dev_experiment),在dev_experiment中做了很多尝试了,做了较多的提交,有些提交是中间过程,commit message也写的比较草率,不适合并入remote repo中,所以需要删除一些commit信息。
但是commit是不能删除的,只能压缩(squash)也就是,将多个commits合并成一个commit,这样提交记录就比较干净了。

用法

git rebase -i commit_hash^

NOTE: commit_hash^中的^用于指示是从该commit到HEAD

然后弹出编辑界面,如下。

pick 9ca62a2 commit_msg_xxxxxxxx
pick da462a1 commit_msg_yyyyyyyy
...
pick da462a1 commit_msg_zzzzzzzz

# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.

比如说我想将9ca62a2da462a1合并,那就要将pick da462a1 commit_msg_yyyyyyyy改成squash da462a1 commit_msg_yyyyyyyy.
NOTE: 都是向上合并的# s, squash <commit> = use commit, but meld into previous commit