git选择性恢复文件的本地更改 [英] git selective revert local changes from a file
问题描述
现在我想恢复这些更改(如svn我希望能够查看文件上的差异,放弃(恢复)我不想要的更改(请参阅恢复),但仅限部分文件。
并保留我想要的更改。
git add - 我
命令似乎可以选择这样做,但我不想这样做。
您可以直接使用 git checkout -p
来做到这一点。请参阅下面的 Daniel Stutzbach的答案。
旧回答(介绍 checkout -p
之前):
<你可以这样做:
git add -i
pre>
(选择你想保留的区块)
git提交-mtmp
现在您只提交了要保留的更改,
git reset --hard HEAD
此时,未提交的更改已被丢弃,因此您有一个干净的工作目录,并且想要保留最后提交的更改。
git reset --mixed HEAD ^
这将删除最后一个提交('tmp'),但将修改保留在您的工作目录中,不进行任何操作。
编辑:替换
- -soft
与- 混合
,清理临时区域。In my git repo which is tracking a svn repo I have made a number of edits to a single file.
Now I want to revert those changes(like svn revert), but only portions of the file.
I want to be able to view the diffs on the file, discard(revert) the changes that I don't want and retain the changes I want.
the
git add -i
command seems to have an option to do that but I don't want to stage this yet.
解决方案You can do that directly with
git checkout -p
. See Daniel Stutzbach's answer below.
Old answer (before
checkout -p
was introduced):You can do it like this:
git add -i
(select the hunks you want to keep)
git commit -m "tmp"
Now you have a commit with only the changes you want to keep, and the rest is unstaged.
git reset --hard HEAD
At this point, uncommitted changes have been discarded, so you have a clean working directory, with the changes you want to keep committed on top.
git reset --mixed HEAD^
This removes the last commit ('tmp'), but keeps the modifications in your working directory, unstaged.
EDIT: replaced
--soft
with--mixed
, to clean up the staging area.这篇关于git选择性恢复文件的本地更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!