git选择性还原文件中的本地更改

Pra*_*eep 146 git version-control git-svn

在我跟踪svn repo的git repo中,我对一个文件进行了大量编辑.

现在我想恢复这些更改(如svn revert),但只是部分文件.

我希望能够查看文件中的差异,丢弃(还原)我不想要的更改并保留我想要的更改.

git add -i 
Run Code Online (Sandbox Code Playgroud)

命令似乎有一个选项可以做到这一点,但我不想暂时这样做.

Dan*_*ach 305

我相信你可以做到最简单:

git checkout -p <optional filename(s)>
Run Code Online (Sandbox Code Playgroud)

从联机帮助页:

   ?p, ??patch
       Interactively select hunks in the difference between the <tree?ish>
       (or the index, if unspecified) and the working tree. The chosen
       hunks are then applied in reverse to the working tree (and if a
       <tree?ish> was specified, the index).
Run Code Online (Sandbox Code Playgroud)
       This means that you can use git checkout ?p to selectively discard
       edits from your current working tree.
Run Code Online (Sandbox Code Playgroud)


Pao*_*tti 90

你可以直接用它来做git checkout -p.请参阅下面的Daniel Stutzbach的回答.


旧答案(之前checkout -p介绍):

你可以这样做:

git add -i
Run Code Online (Sandbox Code Playgroud)

(选择你想要保留的帅哥)

git commit -m "tmp"
Run Code Online (Sandbox Code Playgroud)

现在您只提交了要保留的更改,其余的都是未提交的.

git reset --hard HEAD
Run Code Online (Sandbox Code Playgroud)

此时,未提交的更改已被丢弃,因此您有一个干净的工作目录,并且您希望将更改保留在最前面.

git reset --mixed HEAD^
Run Code Online (Sandbox Code Playgroud)

这将删除最后一次提交('tmp'),但会将修改保留在您的工作目录中,而不是已暂存.

编辑:替换--soft--mixed,以清理临时区域.