基于Git rebase修改历史提交信息

说明

关于为什么要修改历史提交的作者信息和提交信息,我就不多说了,也许是项目规范要求,也许是自己强迫症,总之是有这样的需求。

开始之前先说明一下:

  • 本文介绍的方法只适用于本地仓库,提交到远程以后,我没测试过,也不太清楚。
  • 本文介绍的方法基于rebase实现,适用于修改任意历史提交,不限于最近一条。

开始之前我们需要一个git代码库作为实验对象,该代码库的概况如下,输入如下命令查看:

git log --graph --pretty='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an %ae>%Creset'
复制代码

输出如下:

* e5f026b - (HEAD -> dev) 添加分辨率获取 (17 hours ago) <foo foo@foo.com>
* 51c6763 - 获取core number、device model、ram、diskspace (17 hours ago) <foo foo@foo.com>
* 7075919 - Add commnet (17 hours ago) <foo foo@foo.com>
* 06c4637 - Get SSID and BSSID (17 hours ago) <foo foo@foo.com>
* 4406428 - Get DNS (17 hours ago) <foo foo@foo.com>
* 28169b0 - Get router IP (17 hours ago) <foo foo@foo.com>
* db9b32c - Rename: Device+Info -> Device+Networking (17 hours ago) <foo foo@foo.com>
* 53d87f9 - Get WiFi and Celluar IP Address (17 hours ago) <foo foo@foo.com>
复制代码

下文中的操作都是基于该代码库实现。

修改历史提交的作者信息

界定修改范围

在修改之前,首先需要明确自己的修改范围,rebase操作执行时,允许我们指定rebase范围,比如要修改最近两条提交的作者信息,输入如下命令:

git reabse -i 7075919
复制代码

该命令中-i参数表明我们需要以交互式的方式来执行rebase操作。7075919为rebase操作的起始点,表示对(7075919, HEAD]之间的提交进行rebase,注意不包含7075919这条提交本身。

那么问题来了,如何对所有的提交进行rebase呢?单纯地输入:

git reabse -i 53d87f9
复制代码

是不行的,因为该命令不会将提交53d87f9纳入rebase。

如果要从第一条提交开始rebase,输入如下命令即可:

git rebase -i --root
复制代码

该命令会将所有提交纳入rebase。

选择要更改的提交

执行git reabse -i 7075919命令后,git默认的编辑器会打开,显示内容如下:

pick 51c6763 获取core number、device model、ram、diskspace
pick e5f026b 添加分辨率获取

# Rebase 7075919..e5f026b onto 7075919 (2 commands)
#
# 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.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
复制代码

可以看到,待修改的提交前面都有一个命令,默认是pick,表示rebae过中直接使用该提交,不做修改,提交列表下面的注释说明中还列举了其他命令,感兴趣的话大家可以去看下,此处便不赘述了。

此处我们要修改提交e5f026b的作者信息,直接编辑该提交前面的命令,将pick改成edit,也可以简写为e,效果一样的,编辑的后的内容如下,此处我省略了注释文案:

pick 51c6763 获取core number、device model、ram、diskspace
edit e5f026b 添加分辨率获取

# ...
复制代码

然后保存修改,并关闭编辑重启,我用的是vim,直接:wq退出,控制台会输出如下内容:

➜  Test git:(dev) git rebase -i 7075919
Stopped at e5f026b...  添加分辨率获取
You can amend the commit now, with

  git commit --amend

Once you are satisfied with your changes, run

  git rebase --continue
➜  Test git:(e5f026b)
复制代码

这表明git当前处于交互式rebase过程中,初始时指向第一条需要修改的提交。控制台输出表明当前正在对提交e5f026b进行rebase,这正是我们需要修改的第一条提交。

更改作者信息

输入如下命令,修改作者信息:

git commit --amend --author="bar <bar@bar.com>" --no-edit && git rebase --continue
复制代码

此处我们只有一条提交需要rebase,上述命令执行结束后,控制台输出如下:

➜  Test git:(e5f026b) git commit --amend --author="bar <bar@bar.com>" --no-edit && git rebase --continue
[detached HEAD fa3caab] 添加分辨率获取
 Author: bar <bar@bar.com>
 Date: Mon Apr 29 10:49:08 2019 +0800
 3 files changed, 28 insertions(+)
Successfully rebased and updated refs/heads/dev.
➜  Test git:(dev)
复制代码

表明rebase结束,修改已生效,当前提交的HEAD指针已指向了dev。此处由于我们只有一条提交需要修改,所以该提交的修改结束也意味着整个rebase过程的结束,如果有多条提交需要修改,每完成一条提交的rebase以后,rebase便会停下来,并进入下一条提交的处理。

此时查看修改后的代码库状态如下:

* fa3caab - (HEAD -> dev) 添加分辨率获取 (12 minutes ago) <bar bar@bar.com>
* 51c6763 - 获取core number、device model、ram、diskspace (17 hours ago) <foo foo@foo.com>
* 7075919 - Add commnet (17 hours ago) <foo foo@foo.com>
* 06c4637 - Get SSID and BSSID (17 hours ago) <foo foo@foo.com>
* 4406428 - Get DNS (17 hours ago) <foo foo@foo.com>
* 28169b0 - Get router IP (17 hours ago) <foo foo@foo.com>
* db9b32c - Rename: Device+Info -> Device+Networking (17 hours ago) <foo foo@foo.com>
* 53d87f9 - Get WiFi and Celluar IP Address (17 hours ago) <foo foo@foo.com>
复制代码

可以发现,提交的作者信息已经成功修改,并且生成了新的SHA-1。

修改历史提交的提交信息

修改历史提交信息与修改作者信息类似,同样基于rebase实现。此处仍以修改最近两条提交的提交信息为例,阐明修改过程。

执行git reabse -i 7075919命令界定修改范围以后,在编辑器内选择要修改的提交,此处我们选择修改提交51c6763的提交信息,将pick改成reword,如下所示:

reword 51c6763 获取core number、device model、ram、diskspace
pick fa3caab 添加分辨率获取

# ...
复制代码

保存并退出编辑器。

更改提交信息

上一步退出编辑器后,rebase开始执行,此时会再次打开默认的编辑器,显示如下:

获取core number、device model、ram、diskspace

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Author:    foo <foo@foo.com>
# Date:      Wed Apr 24 15:32:49 2019 +0800
#
# interactive rebase in progress; onto 7075919
# Last command done (1 command done):
#    reword 51c6763 获取core number、device model、ram、diskspace
# Next command to do (1 remaining command):
#    pick fa3caab 添加分辨率获取
# You are currently editing a commit while rebasing branch 'dev' on '7075919'.
#
# Changes to be committed:
#	modified:   DeviceInfo.xcodeproj/project.pbxproj
#	new file:   DeviceInfo/UIDevice+Hardware.h
#	new file:   DeviceInfo/UIDevice+Hardware.m
#	modified:   DeviceInfo/ViewController.m
#
复制代码

直接编辑提交信息进行修改即可,此处我们将提交信息修改为:

修改历史提交信息:获取core number、device model、ram、diskspace

# ...
复制代码

修改完成后保存退出,即完成了对历史提交的修改,控制台输出如下:

➜  Test git:(dev) git rebase -i 7075919
[detached HEAD ac74c92] 修改历史信息:获取core number、device model、ram、diskspace
 Author: foo <foo@foo.com>
 Date: Wed Apr 24 15:32:49 2019 +0800
 4 files changed, 79 insertions(+), 1 deletion(-)
 create mode 100644 DeviceInfo/UIDevice+Hardware.h
 create mode 100644 DeviceInfo/UIDevice+Hardware.m
Successfully rebased and updated refs/heads/dev.
➜  Test git:(dev)
复制代码

表明修改成功。

此时查看修改后的代码库状态如下:

* d5cedac - (HEAD -> dev) 添加分辨率获取 (71 seconds ago) <bar bar@bar.com>
* ac74c92 - 修改历史信息:获取core number、device model、ram、diskspace (8 minutes ago) <foo foo@foo.com>
* 7075919 - Add commnet (20 hours ago) <foo foo@foo.com>
* 06c4637 - Get SSID and BSSID (20 hours ago) <foo foo@foo.com>
* 4406428 - Get DNS (20 hours ago) <foo foo@foo.com>
* 28169b0 - Get router IP (20 hours ago) <foo foo@foo.com>
* db9b32c - Rename: Device+Info -> Device+Networking (20 hours ago) <foo foo@foo.com>
* 53d87f9 - Get WiFi and Celluar IP Address (20 hours ago) <foo foo@foo.com>
复制代码

可以发现,历史提交的提交信息已经成功修改,并且生成了新的SHA-1。

总结

本文基于rebase,阐明了如何修改历史提交的的作者信息和提交信息。其实从执行过程中可以看到,rebase的功能并不止于此,我们只用到了edit、pick、reword三种命令,除此之外,rebase还支持fixup、exec、drop能命令,可供完成更复杂的需求。

参考

www.git-tower.com/learn/git/f…

转载于:https://juejin.im/post/5cd12672f265da038733b6c2

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值