Git(5) -- commit提交管理


Git 提交信息修改:

未push到远程仓库的代码使用以下方法。对于已经将代码push到远程仓库的情况,需要在最后多执行一条强制push到远程仓库:git push --force origin 分支名的命令。

Git(5) -- commit提交修改

修改最新commit的message

本次操作在IntelliJ IDEA的Terminal命令窗口中完成

使用命令git commit –amend允许您打开编辑器,修改您更改最后一次提交消息。

// 查看最近一次的提交记录
D:\IdeaProjects\spring-boot-study>git log -n1
commit b73d13300ee6af0b84cb8ee6349a85b6bebf5255 (HEAD -> master)
Author: 王清雷 <wang@rr.com>
Date:   Mon Jun 28 01:26:48 2021 +0800

    feat:默认分页起始页从1开始 修改最近的一次 commit message
D:\IdeaProjects\spring-boot-study>git commit --amend

输入git commit –amend后 按回车进入到 vim编辑器(熟悉LInux的同学已经知道如何编辑退出了

"D:/IdeaProjects/spring-boot-study/.git/COMMIT_EDITMSG" [unix] 15L, 535B written
feat:默认分页起始页从1开始 修改最近的一次 commit message

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Mon Jun 28 01:26:48 2021 +0800
#
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 1 and 1 different commits each, respectively.
#   (use "git pull" to merge the remote branch into yours)
#
# Changes to be committed:
#       modified:   src/main/java/com/rrc/constant/CommonConst.java

输入 **i ** 文档变成可编辑状态。在可编辑状态下便可以修改提交状态 **:wq ** 保存并退出

-- INSERT --

IntelliJ IDEA的Terminal命令在操作时发现了一些不方便的地方,导致自己以为命令输入有误

  • 按Esc键之后光标定位到代码编辑器了,导致 :wq 输入到.java文件中了

  • 保存退出后Terminal命令窗口的显示如下图,第一眼以为没退出呢。敲了好几个回车才反应过来,输入 **cls ** 清屏。不要怀疑,一切都是假象。

    光标定位问题

  • 注意修改注释的时候可能使用的是中文输入法,在输入 :wq 的时候一定要切换到英文

修改老旧commit的message

使用命令git rebase –i commitID允许修改老旧commit的提交消息。

查看最近3次的代码提交

D:\IdeaProjects\spring-boot-study>git log -3
commit 70e32372f96345f1447da020c95f5b1d8e40eaf3 (HEAD -> master)
Author: 王清雷 <wang@rr.com>
Date:   Mon Jun 28 01:26:48 2021 +0800

    feat:默认分页起始页从1开始 修改最近的一次 commit message

commit 0380f63f9c1d4df1309f53fad1728d1e763643f2
Author: 王清雷 <wang@rr.com>
Date:   Mon Jun 28 00:35:09 2021 +0800

    feat:集成Redis

commit fe056300b80f84a2caf9d92662d94e8e3fe218f1
Merge: a42c966 83f0da7
Author: 王清雷 <wang@rr.com>
Date:   Sun Jun 20 01:37:37 2021 +0800

    Merge remote-tracking branch 'origin/master'

    # Conflicts:
    #       src/main/java/com/rrc/constant/CommonConst.java

我们选择修改倒数第二次的提交信息作变更,但是我们要选的基base应该为它的上一个commitID:fe056300b80f84a2caf9d92662d94e8e3fe218f1

我们执行命令git rebase –i fe056300b80f84a2caf9d92662d94e8e3fe218f1

pick 8f6f3e2 feat:集成Redis 实现修改历史commit message
pick f1b7a7f feat:默认分页起始页从1开始

# Rebase fe05630..f1b7a7f onto fe05630 (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.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
D:/IdeaProjects/spring-boot-study/.git/rebase-merge/git-rebase-todo [unix] (00:52 30/06/2021)                                                                        1,1 Top
-- INSERT --

我们将pick 8f6f3e2 feat:集成Redis 实现修改历史commit message中的pick修改为r。**:wq**保存退出。注释# r, reword <commit> = use commit, but edit the commit message即只修改commit message。

feat:集成Redis 实现修改历史commit message

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Mon Jun 28 00:35:09 2021 +0800
#
# interactive rebase in progress; onto fe05630
# Last command done (1 command done):
#    reword 8f6f3e2 feat:集成Redis 实现修改历史commit message
# Next command to do (1 remaining command):
#    pick f1b7a7f feat:默认分页起始页从1开始
# You are currently editing a commit while rebasing branch 'master' on 'fe05630'.
#
# Changes to be committed:
#       new file:   src/main/java/com/rrc/aspect/RequestParameterAop.java
#       modified:   src/main/java/com/rrc/config/RedisConfig.java
#       modified:   src/main/java/com/rrc/constant/RedisConstant.java
#       modified:   src/main/java/com/rrc/listener/RedisReceiver.java
#       modified:   src/test/java/Test_1.java
#
~
D:/IdeaProjects/spring-boot-study/.git/COMMIT_EDITMSG [unix] (00:58 30/06/2021)                                                                                      1,1 All
-- INSERT --

以上提示为Please enter the commit message for your changes。我们将feat:集成Redis 实现修改历史commit message修改为feat:集成Redis。**:wq**保存退出。

再次使用git log -3查看commit提交信息,发现除了第二次的commit message改变,相应的commitID也发生了改变。

D:\IdeaProjects\spring-boot-study>git log -3
commit b995b18c781edde9df53df82d8312644c25b6f7d (HEAD -> master)
Author: 王清雷 <wang@rr.com>
Date:   Mon Jun 28 01:26:48 2021 +0800

    feat:默认分页起始页从1开始

commit e2d0ac8803245313e46389a72dc620a9cbd7eced
Author: 王清雷 <wang@rr.com>
Date:   Mon Jun 28 00:35:09 2021 +0800

    feat:集成Redis

commit fe056300b80f84a2caf9d92662d94e8e3fe218f1
Merge: a42c966 83f0da7
Author: 王清雷 <wang@rr.com>
Date:   Sun Jun 20 01:37:37 2021 +0800

    Merge remote-tracking branch 'origin/master'

    # Conflicts:
    #       src/main/java/com/rrc/constant/CommonConst.java

连续多个commit整理成1个

使用命令git rebase –i commitID允许将连续多个commit整理成1个commit。

查看最近3次的代码提交

D:\IdeaProjects\spring-boot-study>git log -3
commit b995b18c781edde9df53df82d8312644c25b6f7d (HEAD -> master)
Author: 王清雷 <wang@rr.com>
Date:   Mon Jun 28 01:26:48 2021 +0800

    feat:默认分页起始页从1开始

commit e2d0ac8803245313e46389a72dc620a9cbd7eced
Author: 王清雷 <wang@rr.com>
Date:   Mon Jun 28 00:35:09 2021 +0800

    feat:集成Redis

commit fe056300b80f84a2caf9d92662d94e8e3fe218f1
Merge: a42c966 83f0da7
Author: 王清雷 <wang@rr.com>
Date:   Sun Jun 20 01:37:37 2021 +0800

    Merge remote-tracking branch 'origin/master'

    # Conflicts:
    #       src/main/java/com/rrc/constant/CommonConst.java

D:\IdeaProjects\spring-boot-study>git rebase -i fe056300b80f84a2caf9d92662d94e8e3fe218f1

我们选择将前两个的提交信息进行合并,但是我们要选的基base应该为它的上一个commitID:fe056300b80f84a2caf9d92662d94e8e3fe218f1

我们执行命令git rebase –i fe056300b80f84a2caf9d92662d94e8e3fe218f1

pick e2d0ac8 feat:集成Redis
pick b995b18 feat:默认分页起始页从1开始

# Rebase fe05630..b995b18 onto fe05630 (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.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
D:/IdeaProjects/spring-boot-study/.git/rebase-merge/git-rebase-todo [unix] (22:33 30/06/2021)                                                                        1,1 All
-- INSERT --

我们将pick b995b18 feat:默认分页起始页从1开始中的pick修改为s。**:wq**保存退出。注释# s, squash <commit> = use commit, but meld into previous commit

pick 表示使用本次提交
squash,表示将本次提交合并到上一次pick的提交

pick e2d0ac8 feat:集成Redis
s b995b18 feat:默认分页起始页从1开始
:wq!

[detached HEAD 0dd5958] 合并两次请求 
 Date: Mon Jun 28 00:35:09 2021 +0800
 6 files changed, 145 insertions(+), 7 deletions(-)
 create mode 100644 src/main/java/com/rrc/aspect/RequestParameterAop.java
Successfully rebased and updated refs/heads/master.

再次使用git log -3查看commit提交信息,发现已经合并。

D:\IdeaProjects\spring-boot-study>git log -3
commit 0dd595879b8c093992250b6845c1df8fdc8a4f2e (HEAD -> master)
Author: 王清雷 <wang@rr.com>
Date:   Mon Jun 28 00:35:09 2021 +0800

    合并两次请求

    feat:集成Redis

    feat:默认分页起始页从1开始

commit fe056300b80f84a2caf9d92662d94e8e3fe218f1
Merge: a42c966 83f0da7
Author: 王清雷 <wang@rr.com>
Date:   Sun Jun 20 01:37:37 2021 +0800

    Merge remote-tracking branch 'origin/master'

    # Conflicts:
    #       src/main/java/com/rrc/constant/CommonConst.java

commit a42c966e039d7db874673a55f3dad62c07b3f216
Author: 王清雷 <wang@rr.com>
Date:   Sun Jun 20 01:36:27 2021 +0800

    feat:Redis发布订阅学习

文章作者: WangQingLei
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 WangQingLei !
  目录