如何在git中的先前提交中回滚文件中的更改

luc*_*ius 1 git

我做了3'git commit',但我没有做过'git push'.

1. commit 1
2. commit 2
   touches fileA
   touches fileB
   touches fileC
3. commit 3
Run Code Online (Sandbox Code Playgroud)

那我该怎么办?

  1. 回滚我在文件b中为提交2做的更改?(因为我已经'git commit',我不能再进行'git checkout - fileB'了,我怎么能回滚我的更改?
  2. 在fileC中进行更改并使其成为commit 2的一部分?我想我现在可以去改变文件,然后运行'git rebase -i HEAD~2'正确吗?

Mik*_*zur 5

这应该工作:

1. git rebase -i HEAD~2
2. in your editor, select the following:

edit 9b86592 commit 2
pick f3907cb commit 3

3. at this point roll back the changes you made in fileB, for example with
   `git checkout <version_you_want>` or by manually editing the file
4. make the changes in fileC you want to be part of commit 2
5. `git add fileB fileC`
6. `git commit --amend`
7. `git rebase --continue`
Run Code Online (Sandbox Code Playgroud)

如果git尝试应用提交时存在冲突,则可能需要解决合并问题.解决后git rebase --continue再次运行.