压扁/变基最后提交而不丢失git的任何更改?

时间:2018-10-23 07:50:41

标签: git

我正在为此git rebase / squash苦苦挣扎。

我目前有4次提交,这4次提交有多个更改。

我希望将这4次提交全部变为1,不管我是否丢失提交消息。

所以我尝试了这个:

Squash my last X commits together using Git

git rebase -i HEAD~4.

不知道我的提交历史记录中的哪一部分发送给我,但是我有许多修改过的文件,并且其中有几个文件在上次提交时已过时。

将所有内容恢复为原始状态,然后我尝试了

git reset --hard HEAD~5
git merge --squash HEAD@{1}

git reset起作用并且将我发送到提交,但是--squash无法执行

git merge with --no-ff and --squash

所以基本上也不能使用它,也无法弄清楚no-ff的工作原理而无需进行大量更改

有什么办法可以使它工作?

3 个答案:

答案 0 :(得分:0)

运行git rebase -i HEAD~4时,git将打开一个文本编辑器,其中包含一个类似于以下内容的文件:

pick fc7d27a Change 1
pick 09dd4e6 Change 2
pick 683af92 Change 3
pick a72e15a Change 4

# Rebase 3d8944f..a72e15a onto 3d8944f (4 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
# 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

此“在文本编辑器中打开文件”是一个界面,使用该界面,您可以告诉git您想做什么,然后进行重新设置。

对于要压缩到先前提交中的提交,将pick更改为squash(或仅更改为s)。例如:然后保存并退出编辑器。

pick fc7d27a Change 1
squash 09dd4e6 Change 2
squash 683af92 Change 3
squash a72e15a Change 4

变更2、3和4将被压缩为变更1。

答案 1 :(得分:0)

当我想一次提交多个提交时,我先git reset --soft然后提交。

假设这是我要压缩为单个修订版的最后4个提交:

git reset --soft HEAD~4
git commit -m "Turning 4 revisions into a single one"

完成!

答案 2 :(得分:-1)

git reset --hard HEAD~5
git merge --squash HEAD@{1}

执行完此操作后,您需要运行git commit

相关问题