在不丢失git任何更改的情况下挤压/重新设置上次提交的基础?

在不丢失git任何更改的情况下挤压/重新设置上次提交的基础?,git,Git,我正在用这个git rebase/squash玩 我目前有4个提交的代码,这4个提交有多个更改 我想把所有这4个提交都变成1,不管我是否丢失了提交消息 所以我试了一下: 我不知道提交历史记录的哪一部分发送给我,但我有许多修改过的文件,其中有几个文件在上次提交时已经过时 把一切都恢复到原始状态,然后我试着 git reset --hard HEAD~5 git merge --squash HEAD@{1} git重置成功并将我发送到提交,但是-squash无法执行 因此,基本上也不能使用它,也

我正在用这个git rebase/squash玩

我目前有4个提交的代码,这4个提交有多个更改

我想把所有这4个提交都变成1,不管我是否丢失了提交消息

所以我试了一下:

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

把一切都恢复到原始状态,然后我试着

git reset --hard HEAD~5
git merge --squash HEAD@{1}
git重置成功并将我发送到提交,但是-squash无法执行

因此,基本上也不能使用它,也不能在没有太多东西需要改变的情况下想出如何使用无ff

有什么方法可以让它工作吗?

当您运行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
更改2、3和4将被压缩为更改1

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

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

当我想将多个提交转换为单个提交时,我需要git reset-soft,然后提交

假设是最后4次提交,我想将其压缩成一个修订版:

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

完成了

请告诉我们为什么git合并挤压头{1}不工作?你可能有冲突吗?当你说just S意味着按S键时?我以为这是一条日志信息,我写道:wq!当您运行GitRebase-i时,它会打开一个文本编辑器,这是一个允许您准确地告诉git您希望它做什么的界面。听起来编辑器是vim,所以您需要按i进入插入模式,然后将拾取更改为挤压。然后按escape,然后按wq。请参阅以了解有关vim的更多信息。您还可以将git使用的文本编辑器更改为您更熟悉的版本。看到运行这个命令时,我只看到我的4个提交中的第一个,之前的提交在我的分支之前,在这种情况下我应该怎么做?你确定你正在运行git rebase-I HEAD~4吗?您确定要在以前的状态下启动。也许可以检查你的git日志当我创建git日志时,我看到这个特定分支的4次提交,以前的所有提交都来自远程主机。这会抛出一个错误,正如我前面提到的:致命:你不能将-squash与-no ff组合。
git reset --hard HEAD~5
git merge --squash HEAD@{1}
git reset --soft HEAD~4
git commit -m "Turning 4 revisions into a single one"