git git有选择地从文件中恢复本地更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1109069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:42:56  来源:igfitidea点击:

git selective revert local changes from a file

gitversion-controlgit-svn

提问by Pradeep

In my git repo which is tracking a svn repo I have made a number of edits to a single file.

在跟踪 svn 存储库的 git 存储库中,我对单个文件进行了多次编辑。

Now I want to revert those changes(like svn revert), but only portions of the file.

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

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.

command 似乎有一个选项可以做到这一点,但我还不想进行。

采纳答案by Paolo Capriotti

You can do that directly with git checkout -p. See Daniel Stutzbach's answerbelow.

您可以直接使用git checkout -p. 请参阅下面的Daniel Stutzbach 的回答



Old answer (before checkout -pwas introduced):

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

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.

这将删除最后一次提交 ('tmp'),但将修改保留在您的工作目录中,未暂存。

EDIT: replaced --softwith --mixed, to clean up the staging area.

编辑:替换--soft--mixed, 以清理暂存区。

回答by Daniel Stutzbach

I believe you can do it most simply with:

我相信您可以通过以下方式最简单地做到这一点:

git checkout -p <optional filename(s)>

From the manpage:

从联机帮助页:

   ?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).
   ?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).
       This means that you can use git checkout ?p to selectively discard
       edits from your current working tree.

回答by Greg Hewgill

You could run git diffon the file, save the resulting diff, edit it to remove the changes you dowant to save, then run it through patch -Rto undo the remaining diffs.

您可以运行git diff该文件,保存所生成的差异,修改或移除你的变化希望保存,然后运行它通过patch -R撤消剩余的diff。

git diff file.txt >patch.tmp
# edit patch.tmp to remove the hunks you want to keep
patch -R <patch.tmp

回答by Kent Fredric

Looks like you want

看起来你想要

 git revert --no-commit $REVSISON 

You can then use

然后你可以使用

 git diff --cached

to see what change will be made before commiting ( as reverting is just a commit in a forwards direction that replicates the inverse of a change in the past )

在提交之前查看将进行哪些更改(因为还原只是向前方向的提交,它复制了过去更改的相反方向)

If you were with a pure Git repository, you could possibly, depending on your goals, utilise interactive rebase (git rebase -i) to go back to the commit you didn't like and edit the commit retroactively so that the changes you don't like never happened, but thats generally only for if you KNOWyou'll never want to see it again.

如果您使用的是纯 Git 存储库,则可以根据您的目标,利用交互式变基 ( git rebase -i) 返回到您不喜欢的提交并追溯编辑该提交,以便您不喜欢的更改从未发生,但这通常只是为了如果你知道你永远不想再看到它。

回答by CB Bailey

Re-reading the question, it sounds like you want to revert changes that are in your working tree and not changes that have been previously committed but some of the other answers make it sound like my reading may be wrong. Can you clarify?

重新阅读这个问题,听起来您想恢复工作树中的更改,而不是以前提交的更改,但其他一些答案听起来好像我的阅读可能是错误的。你能澄清一下吗?

If the changes are just in your working copy then the easiest way to do this is to stage the changes you want to keep with:

如果更改仅在您的工作副本中,那么最简单的方法是暂存您要保留的更改:

git add -i <file>

Then throw away the changes that you don't want to keep by checking out the index version:

然后通过检查索引版本来丢弃您不想保留的更改:

git checkout -- <file>

Then unstage the changes if you don't want them staged yet:

如果您不想暂存更改,则取消暂存更改:

git reset -- <file>

This recipe only reverts selected changes to the file (or files that you specify) and doesn't create any temporary commit that then needs reverting.

此秘籍仅还原对文件(或您指定的文件)的选定更改,并且不会创建任何需要还原的临时提交。

If you want to selectively apply only some of the changes made in previous commits then you can reset a file to a previous committed state first:

如果您只想有选择地仅应用先前提交中所做的一些更改,那么您可以先将文件重置为先前提交的状态:

git reset <commit_before_first_unwanted_change> -- <file>

Then you can follow the previous recipe of git add -i <file>to stage those changes that you want to keep, git checkout -- <file>to throw away the unwanted changes and git reset -- <file>to 'unstage' the changes.

然后,您可以按照前面的方法git add -i <file>来暂存您想要保留的那些更改,git checkout -- <file>丢弃不需要的更改并git reset -- <file>“取消暂存”更改。

回答by Abhishek Anand

The command line options described in the answers here are handy when the file is on a server which I am accessing via a ssh terminal. However, when the file is on my local machine I prefer the following way:

当文件位于我通过 ssh 终端访问的服务器上时,此处的答案中描述的命令行选项很方便。但是,当文件在我的本地机器上时,我更喜欢以下方式:

Open the file in the netbeans editor (which comes with git support). Netbeans puts red/green/blue marks at line numbers to indicate where stuff was deleted/added/modified (respectively).

在 netbeans 编辑器(附带 git 支持)中打开文件。Netbeans 在行号处放置红色/绿色/蓝色标记以指示删除/添加/修改内容(分别)的位置。

Right clicking any of these marks gives you an option to undo that change. In addition, you can right click on red and blue marks to find see the old version in a popup.

右键单击这些标记中的任何一个,您都可以选择撤消该更改。此外,您可以右键单击红色和蓝色标记以在弹出窗口中查看旧版本。