【Git】使用git rebase -i命令来合并提交

首先

有时候会想要发表一些恰当的评论或做出一些无关紧要的修改然后提交,对吧~~~
现在我们可以解决这个问题!可以将多个提交合并为一个。

git 交互式变基

假设有以下这样的三个提交。

$ git log

commit ea86c0afbd528e265a1f0bde50307ebdbf41d857
Author: westhouseK <hogehoge@mail.com>
Date:   Sat Jan 30 21:17:04 2021 +0900

    third commit

commit abd34263c1667972302f393edd07086c30d9ba47
Author: westhouseK <hogehoge@mail.com>
Date:   Sat Jan 30 21:14:47 2021 +0900

    second commit

commit dcec1b77867265edaf473290caaa786e902ef3a6
Author: westhouseK <hogehoge@mail.com>
Date:   Sat Jan 30 20:34:27 2021 +0900

    first commit

请在这里执行以下命令。(您可以修改提交注释的时间。)

$ git rebase -i HEAD~~~
pick dcec1b7 first commit
pick abd3426 second commit
pick ea86c0a third commit

# Rebase dcec1b7..ea86c0a onto dcec1b7 (3 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

以下是修正方法:
【步骤】因为是vi模式,所以按下i进入INSERT模式,在进行修正后按下esc,然后用:wq保存更改。

git rebase -i HEAD~~~

pick dcec1b7 first commit
f abd3426 second commit
f ea86c0a third commit

再一次查看git日志时

$ git log
commit 56df6baebebb0bc2017fca3d37a640d6bc7edf8d
Author: westhouseK <hogehoge@gmail.com>
Date:   Sat Jan 30 20:34:27 2021 +0900

    first commit

我們已經將這些提交合併為一個?
這僅僅是刪除了“second commit”和“third commit”的提交訊息,修正內容本身已經正確反映出來了。請放心?

请注意

每当提交ID被重设并改变的时候

【git rebase前】・・・ ①
commit dcec1b77867265edaf473290caaa786e902ef3a6 ← ここ
Author: westhouseK <hogehoge@gmail.com>
Date:   Sat Jan 30 20:34:27 2021 +0900

    first commit


【git rebase後】・・・ ②
commit 56df6baebebb0bc2017fca3d37a640d6bc7edf8d ← ここ 
Author: westhouseK <hogehoge@gmail.com>
Date:   Sat Jan 30 20:34:27 2021 +0900

    first commit

换句话说,即使①和②的评论相同,提交的内容也是不同的意思。
因此,以下会被责备。

$ git_demo $ git push
To github.com:hogehoge/git_demo.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'git@github.com:hogehoge/git_demo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

由于推送了与之前的提交ID不同的内容,因此请使用git push -f进行强制推送。

如果你感到迷惑不清…

git rebase --abort

如果执行此操作,则可以将其恢复为rebase之前的状态。(但如果git rebase -i成功,则无法恢复)

结束

现在可以轻松地进行一些小修正了呢。我们要努力创建一个整洁的分支!!!

bannerAds