Git(10)git修改历史提交

git rebase -i <command>

<command>有六个:

1、pick

        pick只是意味着包括提交。重新进行命令时,不做任何操作则不改变提交的任何内容及信息;若新安排pick命令之间的顺序则会更改提交的顺序;若选择不包括提交,则应删除整行。

2、reword

        该reword命令与pick相似,使用后将暂停重新设置功能,并为您提供修改提交消息的机会,提交所包括的任何更改均不受影响。

3、edit

        如果您选择edit提交,则将有机会修改提交内容,这意味着可以完全添加或更改提交内容。您还可以进行更多提交,然后再继续进行变基。这使您可以将大型提交拆分为较小的提交,或者删除在提交中所做的错误更改。

4、squash

        该命令使您可以将两个或多个提交合并为一个提交。提交被压缩到其上方的提交中。Git使您有机会编写描述这两个更改的新提交消息。

5、fixup

        这类似于squash,但是要合并的提交已丢弃其消息。提交仅合并到其上方的提交中,并且较早提交的消息用于描述这两个更改。

6、exec

        这使您可以对提交运行任意的Shell命令。

前置工作

# 我们初始化一个项目
git init


## 制造一些提交
touch base.txt
git add .
git commit -m "add base"

touch 1.txt
git add .
git commit -m "add 1"

touch 2.txt
git add .
git commit -m "add 2"

touch 3.txt
git add .
git commit -m "add 3"

touch 4.txt
git add .
git commit -m "add 4"

touch 5.txt
git add .
git commit -m "add 5"


## 查看现在的提交
git log
commit a75ed742838ebc1ef1073502623478f73e1ec21f
Author: 
Date:   Wed Mar 4 10:02:51 2020 +0800

    add 5

commit 8b485bb4768b2abf8f6400dcba069f1a650ed5ec
Author: 
Date:   Wed Mar 4 09:59:27 2020 +0800

    add 4

commit 63ce9fb010da550c668aca66758c45fbfad46e2b
Author:
Date:   Wed Mar 4 09:59:04 2020 +0800

    add 3

commit 9cd34c4d42f52cfb40026dae613c8ad29d7cbc66
Author: 
Date:   Wed Mar 4 09:58:45 2020 +0800

    add 2

commit 77bd0eb1a97e1676367ea236c1c47c155eaa8430
Author: 
Date:   Wed Mar 4 09:58:23 2020 +0800

    add 1

一、使用pick更改提交顺序、删除提交

1、改变提交 5.txt 和 4.txt 的顺序,该怎么操作

        更改涉及到了两次提交,最早提交次数为2(4.txt是倒数第二次提交),改变倒数第2次后的提交

(1)执行命令

git rebase -i HEAD~2

接着,git给你一个文本,告诉你我知道了,你说的这些可以有以下操作下面是执行命令后的样子

pick 8b485bb add 4
pick a75ed74 add 5

# Rebase 63ce9fb..a75ed74 onto 63ce9fb (2 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
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
~
~
~

解释:我们刚刚执行的命令中 HEAD~2代表选择离HEAD最近的两条提交,下面注释的是提示,我们不需要管,只要专注前两行就ok

(2)把 第一行 和 第二行 交换顺序,变成下面的样子

pick a75ed74 add 5
pick 8b485bb add 4

(3)接着按Esc后输入:wq保存退出,输入命令git log查看,4和5的顺序改变

(4)git push -f 强制推送改变的结果,远程仓库的提交记录中,4和5的顺序也改变了

2、删除 某一个提交,该怎么操作

我们来删除 add 4 的那条提交

(1)执行命令

git rebase -i HEAD~2

出现如下

pick a75ed74 add 5
pick 8b485bb add 4

# Rebase 575fd8b..bb2a77d onto 575fd8b (1 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
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
~
~
~

(2)删除第二行内容,只剩下pick a75ed74 add 5

(3)接着按Esc后输入:wq保存退出,输入命令git log查看,只剩下5、3、2、1的提交记录

(4)git push -f 强制推送改变的结果,远程仓库的提交记录中,也只剩下5、3、2、1的提交记录

二、使用reword修改提交信息(内容保持不变)

修改一下 add 2 的提交消息,该怎么操作

(1)git log 查看 add 2 距离 HEAD 有多少的距离,得:3    注:之前删除了4的提交记录

(2)执行命令

git rebase -i HEAD~3

出现如下

pick 9cd34c4 add 2
pick 63ce9fb add 3
pick 575fd8b add 5

# Rebase 77bd0eb..575fd8b onto 77bd0eb (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
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
~
~
~

(3)只需要修改 第一行 add 2 ,其余保持不变

reword 9cd34c4 add 2
pick 63ce9fb add 3
pick 575fd8b add 5

# Rebase 77bd0eb..575fd8b onto 77bd0eb (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
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
~
~
~

(4)接着按Esc后输入:wq保存退出,会弹出一个编辑窗口

add 2

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Wed Mar 4 09:58:45 2020 +0800
#
# interactive rebase in progress; onto 77bd0eb
# Last command done (1 command done):
#    r 9cd34c4 add 2
# Next commands to do (2 remaining commands):
#    pick 63ce9fb add 3
#    pick 575fd8b add 5
# You are currently editing a commit while rebasing branch 'master' on '77bd0eb'.
#
# Changes to be committed:
#       new file:   2.txt
#

(5)编辑窗口中修改提交信息为    add 2 ~ new comment

(6)接着按Esc后输入:wq保存退出,输入命令git log查看,消息 “add 2” 变为了 “add 2 ~ new comment”

注意:这里只修改了2的提交信息,其他提交没有任何的变动

三、edit修改提交

在 add 3 和 add 5 之间 添加一条提交

(1)执行命令

git rebase -i HEAD~2

出现如下: 

pick 6934312 add 3
pick 5ce6dde add 5

# Rebase 7f9d45d..5ce6dde onto 7f9d45d (2 command(s))
# ....

将pick 6934312 add 3修改为e 6934312 add 3

(2)接着 Esc,:wq 保存退出

$ git rebase -i HEAD~2
Stopped at 6934312135c150bf74bead26e371df1443273ca4... add 3
You can amend the commit now, with

        git commit --amend

Once you are satisfied with your changes, run

        git rebase --continue


xxxxxx MINGW32 ~/Desktop/git-demo (master|REBASE-i 1/2)

(3)可以看到,我们的master分支多了REBASE-i 1/2,我们尝试做一些修改,给3.txt 增加一些内容,然后提交

git add 3.txt
git commit -m "edit 3.txt"

出现如下 :

[detached HEAD 7262a57] edit 3.txt
 1 file changed, 1 insertion(+)

(4)接着,我们继续 rebase

git rebase --continue

出现如下 :

Successfully rebased and updated refs/heads/master.

(5)输入git log,在 add 5 和 add 3 中间 增加了我们刚刚的修改

文章参考:【git 整理提交】git rebase -i 命令详解_the_power的博客-CSDN博客

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值