如何返回到旧提交,进行一些更改并在旧提交中提交这些更改,这也会更新所有以下提交

How to go back to an old commit, make some changes and commit those changes at the old commit which also updates all the following commits

我正在处理一个文件,我提交了一些更改,然后我意识到我想要我最近在旧提交中所做的一些更改,因为它是旧提交的一部分。

所以,我想回到提交 2c57de7 并在同一个提交中添加一些我还没有添加到暂存区的更改。我不想创建新的提交。

* 59939c3 - Sun, 16 Jan 2022 14:19:40 +0530 (3 hours ago) (HEAD -> main)
|           Problems - Added Problem 03
* 2c57de7 - Sun, 16 Jan 2022 12:52:04 +0530 (4 hours ago)
|           Problems - Added Problem 02
| * 3031e71 - Sun, 16 Jan 2022 13:37:14 +0530 (3 hours ago) (Trees)
| |           Level Wise
| * 7aaa1dd - Sun, 16 Jan 2022 12:44:59 +0530 (4 hours ago)
| |           Introduction
| * 5c26db8 - Sat, 15 Jan 2022 18:13:23 +0530 (23 hours ago)
|/            Vectors
* 692c42e - Wed, 12 Jan 2022 19:10:02 +0530 (4 days ago)
|           Problems - Added Problem 01

这是我的提交历史

如有任何帮助,我们将不胜感激!

which also updates all the following commits

这意味着您需要在更新后的旧提交之上对后续提交进行变基。

来自您当前的主:

git branch tmp
git stash
git switch -C main 2c57de7  # reset main back to 2c57de7 
git stash pop
git add .
git commit --amend -m "Problems - Added Problem 02, amended"
git switch tmp
git rebase main
git switch main
git merge tmp
git branch -d tmp