如何重新定义(挤压)上次提交以掌握和更改提交消息

时间:2014-04-09 10:20:53

标签: git git-interactive-rebase

在GitI中有以下情况:

o "ok" b6ca869   [my_branch*]
|
o "ok" 479d27c
|
o "ok" c80fad5
|
o "ok" 8f7fe87
|
o "master commit message" [master][remotes/origin/master]

我想挤压那些4" ok"提交并进一步更改消息

o "my_branch commit message"    [my_branch*]
|
o "master commit message" [master][remotes/origin/master]

所以我做的是

git rebase --interactive master

GNU nano(我在Ubuntu上编辑)编辑器已打开

GNU nano 2.2.2             File: /home/.git/modules/src/android/frameworks/base/rebase-merge/git-rebase-todo                                

pick 8f7fe87 ok
pick c80fad5 ok
pick 479d27c ok
pick b6ca869 ok

# Rebase aeedb8f..b6ca869 onto aeedb8f
#
# 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
#
# 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 个答案:

答案 0 :(得分:2)

你需要将最后3次提交压缩到第一次提交(s代表squash):

pick 8f7fe87 ok
s c80fad5 ok
s 479d27c ok
s b6ca869 ok

保存然后退出,并且应该弹出一个包含所有提交消息的新缓冲区。只需使用/修改您想要的提交,保存并退出。

答案 1 :(得分:1)

你可以将最后4次提交压缩在一起;因为你在本地提交了它们。为此,您可以将文件编辑为:

pick 8f7fe87 ok
s c80fad5 ok
s 479d27c ok
s b6ca869 ok

之后,您将能够更改(组合)提交消息。

然而 这不会在最后一次提交时用“主提交消息”压缩它们。问题是你的“主提交”已被推送到服务器;所以你不能简单地将提交附加到已经推送到服务器的提交。

该提交具有一定的“哈希”代码;并且因为其他用户可能已经提取了该代码; git没有(或不容易)允许附加到已经被推送到服务器的提交。如果你想这样做,你可以看到这个question

答案 2 :(得分:0)

将三个较低的pick更改为squash,然后保存并退出。将弹出另一个编辑器,允许您更改提交消息。

如果您想以任何理由撤消rebase,请查看git reflog。它将显示rebase之前状态的sha1,并且您可以通过执行git reset --hard sha1返回到该状态。还有一个引用reflog的快捷方式git reset --hard @{1}