44

git 删除无用的 commit - 鸡尾酒的笔记

 4 years ago
source link: https://blog.cocktail1024.top/archives/134.html?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

git 删除无用的 commit

写代码的时候,往往都是写一个功能,然后不断fix bug
提交记录就会变成 以下这种情况 了。。。

commit c99106a39a32f7d2388ef55dbef7126f406089cc (HEAD -> master)
Author: xxx <[email protected]>
Date:   Wed Oct 16 14:18:13 2019 +0800

    fix bug3

commit 055521f18710831df56a5a2d90ba65ac05ac0b69
Author:  xxx <[email protected]>
Date:   Wed Oct 16 14:17:28 2019 +0800

    fix bug2


commit 634266787bec4c7116f7a1ede8bfebbe0ea9c8c1
Author:  xxx <[email protected]>
Date:   Wed Oct 16 14:16:28 2019 +0800

   hello world

作为一个优秀的 复制粘贴工程师,当然不允许这种情况发生

解决方案一:使用 git reset --soft

git reset --soft HEAD~2 // 其中2 是 要 合并的commit 数量,注意,第一次commit 不能合并

这时候 fix bug3fix bug2 的修改都被合并到 git 的 暂存区, 例如:

$ git status
位于分支 master
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

    修改:     test.txt

然后 就可以愉快的 commit 并且 push 了

$ git commit -m "no bug"
[master d9557a2] no bug
 1 file changed, 2 insertions(+)
$ git push -f origin master // 如果之前push过,这里要加 -f 参数,强制推送到远程

git log 看一下

commit d9557a2aee352ec3debc765df017ada64f333f73 (HEAD -> master) // commit id 会变
Author:  xxx <[email protected]>
Date:   Wed Oct 16 14:23:27 2019 +0800

    no bug

commit 634266787bec4c7116f7a1ede8bfebbe0ea9c8c1
Author:  xxx <[email protected]>
Date:   Wed Oct 16 14:17:28 2019 +0800

    fix bug

完美的写出了没有bug 的代码

解决方案二:使用git rebase -i

这个命令比较强大,当然也会比较复杂

git rebase -i HEAD~2 //其中2 是 要 修改的commit 数量,注意,第一次commit 不能修改
pick 055521f fix bug2
pick c99106a fix bug3

# 变基 6342667..c99106a 到 6342667(2 个提交)
#
# 命令:
# p, pick = 使用提交
# r, reword = 使用提交,但修改提交说明
# e, edit = 使用提交,但停止以便进行提交修补
# s, squash = 使用提交,但和前一个版本融合
# f, fixup = 类似于 "squash",但丢弃提交说明日志
# x, exec = 使用 shell 运行命令(此行剩余部分)
# d, drop = 删除提交
#
# 这些行可以被重新排序;它们会被从上至下地执行。
#
# 如果您在这里删除一行,对应的提交将会丢失。
#
# 然而,如果您删除全部内容,变基操作将会终止。
#
# 注意空提交已被注释掉

注释里面也写的很清楚,按照提示操作,将 第二个pick 修改成 s(这里第一个不能改成s,否则报错)

pick 055521f fix bug2
s c99106a fix bug3

然后保存退出
如果没有问题,会出现git commit 修改界面,

将 fix bug2 修改成想要的commit 文案
然后将 fix bug3 注释掉就好了
# 这是一个 2 个提交的组合。
# 这是第一个提交说明:

no bug 

# 这是提交说明 #2:

#fix bug3 

# 请为您的变更输入提交说明。以 '#' 开始的行将被忽略,而一个空的提交
# 说明将会终止提交。
#
# 日期:  Wed Oct 16 14:18:13 2019 +0800
#
# 交互式变基操作正在进行中;至 6342667
# 最后一条命令已完成(2 条命令被执行):
#    pick 055521f fix bug2
#    squash c99106a fix bug3
# 未剩下任何命令。
# 您在执行将分支 'master' 变基到 '6342667' 的操作。
#
# 要提交的变更:
#       修改:     test.txt

git log 看一下

commit c7007c859203c3d0eff4f5540ac8dab69e372697 (HEAD -> master)
Author: lijingwei <[email protected]>
Date:   Wed Oct 16 14:18:13 2019 +0800

    no bug

commit 634266787bec4c7116f7a1ede8bfebbe0ea9c8c1
Author: lijingwei <[email protected]>
Date:   Wed Oct 16 14:17:28 2019 +0800

    fix bug

git rebase -i 也可以合并其中几个,或者删除其中几个,比git reset --soft 要强大的多,比较推荐这种做法


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK