如何在我之前的提交中回滚1个文件更改

mic*_*ael 7 git

我在git中做了2次提交(我没有推),其中'Commit 2'是最新的'':

 git log -2
commit 791962d776f66253d656586b097b2677eaa983d1
Author: michael <michael@michael-laptop.(none)>
Date:   Tue Jun 29 23:20:58 2010 -0700

    Commit 2

commit b743075e81f6fe25fe48ddbef2b5e7cc06623533
Author: michael <michael@michael-laptop.(none)>
Date:   Tue Feb 16 23:09:53 2010 -0800

    Commit 1
Run Code Online (Sandbox Code Playgroud)

在我的提交1 b743075e81f6fe25fe48ddbef2b5e7cc06623533中,我触摸/更改了许多文件:

   dir1/file1.cpp
   dir1/file1.h
   dir1/file2.cpp
   dir1/file2.h
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何回滚我对提交1的dir1/file2.cpp,dir1/file2.h所做的更改?并保持其他一切相同?

谢谢.

Von*_*onC 5

最简单的解决方案是,从您的最新提交(HEAD)到:

  • 在旧版本中检出这两个文件,
  • 添加它们,
  • 然后提交.
    git checkout b743075e81 -- dir1/file2.cpp
    git checkout b743075e81 -- dir1/file2.h
    git add dir1/file2.cpp # only if you made additional changes
    git add dir1/file2.h   # only if you made additional changes
    git commit -m "revert dir1/file2.cpp and dir1/file2.h"

正如Chris Johnsen在评论中提到的那样:

git checkout使用pathspecs更新索引(如git reset使用pathspecs)和工作树,因此git add除非在结帐后进行其他更改,否则不需要.