git场景化操作(一): commit梳理

902 阅读6分钟

commit梳理

1.修改提交信息

1.修改最新一次提交信息

修改最新一次的提交信息比较简单,使用git commit --amend命令即可修改,具体操作如下:

Step 1.查看当前提交记录

commit 5dda4591f28955e4eaba649e46301e0d20869ffa (HEAD -> master, tag: test)
Author: xxx <xxx@xxx.com>
Date:   Sun Dec 13 19:02:57 2020 +0800

    add demo.html and modify readme

commit 84ed89a79f290cbdab68bdc9468859a05d9fac77
Author: xxx <xxx@xxx.com>
Date:   Sun Dec 13 18:57:21 2020 +0800

    init proj

Step 2.执行该命令进入交互模式编辑最新的提交信息,修改完成后保存退出

git commit --amend

Step 3.再次查看git log

commit d580b0531381c4e2384296374515183365bfcefb (HEAD -> master)
Author: xxx <xxx@xxx.com>
Date:   Sun Dec 13 19:02:57 2020 +0800

    add demo.html and modify readme has been change

commit 84ed89a79f290cbdab68bdc9468859a05d9fac77
Author: xxx <xxx@xxx.com>
Date:   Sun Dec 13 18:57:21 2020 +0800

    init proj

注意,这里可以发现commit的hash已经改变,说明其实是重新创建了一个新的commit

2.修改以前的提交信息

修改过去的commit信息就需要使用到git rebase命令了,具体操作如下:

Step 1.查看当前提交记录,想要修改中间一次的提交(commit: d580b0531381)

commit 988031473715ddbb3ba675aaaec8ee708b45cfb0 (HEAD -> master)
Author: your name <your@email.com>
Date:   Wed Dec 16 00:19:02 2020 +0800

    modify demo.html

commit d580b0531381c4e2384296374515183365bfcefb
Author: your name <your@email.com>
Date:   Sun Dec 13 19:02:57 2020 +0800

    add demo.html and modify readme has been change

commit 84ed89a79f290cbdab68bdc9468859a05d9fac77
Author: your name <your@email.com>
Date:   Sun Dec 13 18:57:21 2020 +0800

    init proj

Step 2.使用git rebase 命令,以要改动的提交的上一次提交作为基准(commit: 84ed89a79f290)

git rebase -i 84ed89a79f290

#进入交互模式,信息如下

pick d580b05 add demo.html and modify readme has been change
pick 9880314 modify demo.html

# Rebase 84ed89a..9880314 onto 84ed89a (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.
#
# Note that empty commits are commented out

Step 3.可以看到rebase命令中为我们列出了许多的选项,其中pick就是单纯的将commit挑选出来并追加到rebase指定的基准上,这里根据描述可以发现,我们应使用reword命令(选出来的同时修改commit信息)

reword d580b05 add demo.html and modify readme has been change
pick 9880314 modify demo.html
...

# 保存退出后会进入到编辑commit信息的交互中,修改commit信息后再次保存退出,git会提示你修改成功

Step 4.查看git log

commit 031d6955860af34ae5b0af4d333012665a54a0c0 (HEAD -> master)
Author: your name <your@email.com>
Date:   Wed Dec 16 00:19:02 2020 +0800

    modify demo.html

commit a527ec35ded4028b924d02e25cf954f2f9ef6fa0
Author: your name <your@email.com>
Date:   Sun Dec 13 19:02:57 2020 +0800

    add demo.html and modify readme has been changed again!

commit 84ed89a79f290cbdab68bdc9468859a05d9fac77
Author: your name <your@email.com>
Date:   Sun Dec 13 18:57:21 2020 +0800

    init proj

可以看到中间一次的commit信息已经修改("has been change" -> "has been changed again!"),且commit的hash id也已经改变,不再试原有提交而是重新生成的提交对象追加到基准之后

整合提交

多次连续的提交整合为一个提交

开发过程中可能会遇到需要整合多次commit的需求,具体操作方式如下

Step 1.首先观察下当前的提交记录

commit 2696a6b43c3252ed26f1eecd06d0fb443de6ca81 (HEAD -> master)
Author: your name <your@email.com>
Date:   Sun Dec 20 00:44:59 2020 +0800

    test commit

commit 031d6955860af34ae5b0af4d333012665a54a0c0
Author: your name <your@email.com>
Date:   Wed Dec 16 00:19:02 2020 +0800

    modify demo.html

commit a527ec35ded4028b924d02e25cf954f2f9ef6fa0
Author:  your name <your@email.com>
Date:   Sun Dec 13 19:02:57 2020 +0800

    add demo.html and modify readme has been changed again!

commit 84ed89a79f290cbdab68bdc9468859a05d9fac77
Author:  your name <your@email.com>
Date:   Sun Dec 13 18:57:21 2020 +0800

    init proj

Step 2.这里以将中间两次commit合并为目的进行操作,依旧使用rebase命令,以第一次提交作为基准

> git rebase -i 84ed89a79f290

#进入交互模式,pick要合并的第一个commit,并将后面需要合并的commit 使用squash参数

pick a527ec3 add demo.html and modify readme has been changed again!
squash 031d695 modify demo.html
pick 2696a6b test commit

...

squash参数的描述如下,可以看出刚好符合我们这次的需求

s, squash <commit> = use commit, but meld into previous commit

Step 3.保存退出上一步后,将自动跳转到描述信息编写步骤,为新的commit指定描述信息

# This is a combination of 2 commits.
commbo commit

# This is the 1st commit message:

add demo.html and modify readme has been changed again!

# This is the commit message #2:

modify demo.html

Step 4.查看新的git log,可以发现已经被合并成功了

commit 83bc3c142f7dfda3dd2cae387b69bd7c7162d9b0 (HEAD -> master)
Author: your name <your@email.com>
Date:   Sun Dec 20 00:44:59 2020 +0800

    test commit

commit 20bbb14ede2297040f0d08fdde9712d80aea6984
Author: your name <your@email.com>
Date:   Sun Dec 13 19:02:57 2020 +0800

    commbo commit

    add demo.html and modify readme has been changed again!

    modify demo.html

commit 84ed89a79f290cbdab68bdc9468859a05d9fac77
Author: your name <your@email.com>
Date:   Sun Dec 13 18:57:21 2020 +0800

    init proj

不连续的多次提交整合为一个提交

具体操作方法与上述步骤类似,区别就是需要将不连续的几次commit顺序调整下,使其放到一起,例如想要整合第3次,第5次提交,就在rebase时将第五次提交放到第三次提交后面,并使用squash参数即可;

需要注意的是,调整commit顺序很有可能会引起冲突,需要解决冲突