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

栏目: 编程工具 · 发布时间: 5年前

内容简介:关于为什么要修改历史提交的作者信息和提交信息,我就不多说了,也许是项目规范要求,也许是自己强迫症,总之是有这样的需求。开始之前先说明一下:开始之前我们需要一个git代码库作为实验对象,该代码库的概况如下,输入如下命令查看:

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

开始之前先说明一下:

  • 本文介绍的方法只适用于本地仓库,提交到远程以后,我没测试过,也不太清楚。
  • 本文介绍的方法基于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。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

A Philosophy of Software Design

A Philosophy of Software Design

John Ousterhout / Yaknyam Press / 2018-4-6 / GBP 14.21

This book addresses the topic of software design: how to decompose complex software systems into modules (such as classes and methods) that can be implemented relatively independently. The book first ......一起来看看 《A Philosophy of Software Design》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换