是否可以在旧的git提交中编辑文件以及如何编辑?

时间:2017-04-12 16:56:29

标签: git

让我说我用提交jave一个git repo

HEAD
commit999: Edit file999
commit998: Edit file998
...
commit3: Edit file3
commit2: Edit file2 and file1
commit1: Edit file1

我意识到file1不应该在commit2中编辑过。我想在commit2中恢复file1上的更改,但我不想为此版本提交新的提交。

有可能做我想要的吗?如果是这样,我该怎么做?

(只有我使用这个git repo。所以我不关心任何脏东西,例如push -f origin master

1 个答案:

答案 0 :(得分:0)

这可以通过“修改修订版2”来完成,然后挑选在它上面完成的所有事情......就像这样:

git checkout commit2 git checkout HEAD~1 -- <path-to-file1> # checkout file1 as it was on the previous revision git commit --amend --no-edit # create a _new_ revision git cherry-pick commit2..commit999 # use old rev IDs for commit2 and commit999 git checkout -b new-branch # create and checkout new branch if you feel like it

这应该有效。