Git log 不太好看,我们来合并 commit 吧

最近在修改博客时脑子有点蒙,几个 commit 提交得都有点问题,提交到线上才发现又写了好几个 bug ,导致网站经常 500,没办法,赶紧修复呗,这也没什么,反正都是本地 git,但是之后博客准备开源的,这么糟糕的 commit 真的要放到 github 上吗?

合并 commit,唯一的优点,就是看起来相当干净,commit 看起来也是相当完美,算了,编不下去了,快点开始吧。
首先git log查看下

commit 1a76bac5e0edbc5d3d70e017eced9ee334f78a60
Author: jourdon <admin@qiehe.net>
Date:   Mon Apr 2 09:55:40 2018 +0000

   test 3

commit b59c48ce22a5de512fcecbaa66d23cf03fc27b5f
Author: jourdon <admin@qiehe.net>
Date:   Mon Apr 2 08:16:44 2018 +0000

    fix bug

commit 89b42221a2517a307979658e8e3159110fde2700
Author: jourdon <admin@qiehe.net>
Date:   Thu Mar 15 05:32:01 2018 +0000

  test 2  

commit a665822532841654e87e2a7ea5ba7a896b57c045
Author: jourdon <admin@qiehe.net>
Date:   Wed Mar 14 03:20:37 2018 +0000

    test 1

fix bug 这条 commit 不好看,干掉他。

git rebase -i 89b42221a // 这里跟的 commit 是 test2 的 commit-id,test2 是不会被干掉的。

正常情况下,敲了命令会出来 VIM 的编辑器,但是这里你可能会遇到问题,出现的可能是另外的编辑器,比如 nano ,这个实在不好用,我们还是要换回 vim

update-alternatives --config editor

你会看到下面的界面

There are 4 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
 *0            /bin/nano            40        auto mode
  1            /bin/ed             -100       manual mode
  2            /bin/nano            40        manual mode
  3            /usr/bin/vim.basic   30        manual mode
  4            /usr/bin/vim.tiny    10        manual mode
Press <enter> to keep the current choice[*], or type selection number:

vim.basic 是一个完整版的 vim ,但没有图像界面,菜单栏
vim.tiny 是一个 vim 的缩减版
这里选 3 就可以了。
我们继续

pick b59c48c fix bug
pick 1a76bac test 3

# Rebase a665822..1a76bac onto a665822 (3 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

上面的界面,先列出当前分支最新的2个 commit (越下面越新)。每个commit 前面有一个操作命令,默认是 pick ,表示该行 commit 被选中,要进行 rebase 操作。2个 commit 的下面是一大堆注释,列出可以使用的命令。

  • pick:正常选中
  • reword:选中,并且修改提交信息;
  • edit:选中,rebase时会暂停,允许你修改这个commit(参考这里)
  • squash:选中,会将当前commit与上一个commit合并
  • fixup:与squash相同,但不会保存当前commit的提交信息
  • exec:执行其他shell命令

squashfixup 都可以用来合并 commit ,唯一的区别是 squash 会把 commit message 也合并,而 fixup只会留下最新的一条 commit message, 现在我们先把需要合并的 commit 前面的动词,改成 fixup (或者f),然后就是 :wq 保存吧。

需要注意的是合并 commit 是不可以把全部 commit 合并成一条的,因为默认是需要有一条前置的 commit ,也就是第一条 commit 是没办法合并的

保存完成后,你有两个选择

git rebase --continue  //确认 rebase
git rebase --abort //取消 rebase

确认后,就可以上传到远程了。

git push -f  //强制覆盖远程

去服务器上看一下

commit 1a76bac5e0edbc5d3d70e017eced9ee334f78a60
Author: jourdon <admin@qiehe.net>
Date:   Mon Apr 2 09:55:40 2018 +0000

   test 3

commit b59c48ce22a5de512fcecbaa66d23cf03fc27b5f
Author: jourdon <admin@qiehe.net>
Date:   Mon Apr 2 08:16:44 2018 +0000

    fix bug

commit 89b42221a2517a307979658e8e3159110fde2700
Author: jourdon <admin@qiehe.net>
Date:   Thu Mar 15 05:32:01 2018 +0000

  test 2  

commit a665822532841654e87e2a7ea5ba7a896b57c045
Author: jourdon <admin@qiehe.net>
Date:   Wed Mar 14 03:20:37 2018 +0000

    test 1

发现并没有推上来,这里是因为我们本地推送到仓库是强制覆盖的,站点目录需要 git pull 下来。但是仓库与站点目录现在有冲突,所以我们需要再强制覆盖本地。

git fetch --all
git reset --hard origin/master
git pull

服务器返回 Already up-to-date
再次 git log

commit 1a76bac5e0edbc5d3d70e017eced9ee334f78a60
Author: jourdon <admin@qiehe.net>
Date:   Mon Apr 2 09:55:40 2018 +0000

   test 3

commit 89b42221a2517a307979658e8e3159110fde2700
Author: jourdon <admin@qiehe.net>
Date:   Thu Mar 15 05:32:01 2018 +0000

  test 2  

commit a665822532841654e87e2a7ea5ba7a896b57c045
Author: jourdon <admin@qiehe.net>
Date:   Wed Mar 14 03:20:37 2018 +0000

    test 1

搞定!

这里是重点

本文说的是合并,但是强烈不建议合并,我这里只是自己的一个小项目为了好看而已,但是如果你真的项目中这么做了,只会被队友嫌弃,不,是唾弃,你已经无法与队友合作完成项目了,因为你合并后的项目不管多么漂亮,都是一个乱七八糟的项目,Git 的意义已经不复存在了。

原文地址: 茄盒网 - git log 不太好看,我们来合并 commit 吧

本作品采用《CC 协议》,转载必须注明作者和本文链接
Good Good Study , Day Day Up!!
Jourdon
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 2

大佬你发个不强制的吧,谢谢大佬

2年前 评论
Jourdon (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!