git - 删除以前的提交但保留上次提交的更改

git - delete a previous commit but keep changes from last commit

如何删除以前的提交但保留 git 中最后一次提交的更改?

第一次提交:

nano first //writes "this is first" and save
git add first
git commit -m "first commit"

第二次提交:

nano second //writes "this is second" and save
git add second
git commit -m "second commit"

Git 日志:

commit 014179d534ed3185dba994d4df3713334893a2c7 (HEAD -> master)
Author: Email <email@gmail.com>
Date:   Fri Mar 18 20:29:20 2022 +0800

    second commit

commit 22b9f9a0a60b5b52c2b8d3e1981b1db524c9d2fd
Author: Email <email@gmail.com>
Date:   Fri Mar 18 20:22:46 2022 +0800

    first commit

我想删除 first commit 但保留 files。我该怎么做?

比如,您想将其转换为保留两个文件的单个提交?

有很多方法...最简单(最省力):

git reset --soft HEAD~ # set branch _pointer_ in previous revision
# files are not changed. second will be in index (check with git status)
git commit --amend -m "this is the squashed version"

大功告成。

您还可以这样做:

git rebase -i --root
# you will get a list of two revisions
# set the second to be s (or squash)
# save and exit
# rebase starts
# git will show you a commit windows telling you about the squash
# and the comment from both revision
# set it to whatever you want
# save and exit

大功告成。