Git Diff - 如何还原选定的行或块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10826228/
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
Git Diff - How do I revert selected lines or chunks
提问by Kenoyer130
In Git GUI I can select parts of a diff and stage just those lines or chunks. How would I do the opposite, as in roll back changed lines in a file. Usually these are accidental white space changes I just want to revert out but still stage/commit other parts of the same file.
在 Git GUI 中,我可以选择差异的一部分并仅暂存那些行或块。我将如何做相反的事情,就像在回滚文件中更改的行一样。通常这些是意外的空白更改,我只想恢复但仍暂存/提交同一文件的其他部分。
采纳答案by Anil
For Git Gui: First, click Rescan to scan for the modified file. Next, click the icon to the left of the filename to stage all modifications for commit. Then, right click on the debug line and chose Unstage Line From Commit.
对于 Git Gui:首先,单击重新扫描以扫描修改后的文件。接下来,单击文件名左侧的图标以暂存所有修改以进行提交。然后,右键单击调试行并选择 Unstage Line From Commit。
The above information from: http://nathanj.github.com/gitguide/tour.html
回答by ellotheth
Stage the parts you want with git add -p
, then discard (git checkout -- filename
) the unstaged changes.
使用 暂存您想要的部分git add -p
,然后丢弃 ( git checkout -- filename
) 未暂存的更改。
Update for Git 1.6.5+
Git 1.6.5+ 更新
In version 1.6.5, Git learned to checkout with a -p/--patch
flag. You can discard chunks in one step with git checkout -p -- filename
.
在 1.6.5 版本中,Git 学会了使用-p/--patch
标志签出。您可以使用 一步丢弃块git checkout -p -- filename
。
From the docs:
从文档:
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.
在 <tree-ish>(或索引,如果未指定)和工作树之间的差异中交互选择块。然后将选定的大块反向应用于工作树(如果指定了 <tree-ish>,则为索引)。
这意味着您可以使用 git checkout -p 有选择地丢弃当前工作树中的编辑。
回答by Leif Gruenwoldt
In Git Gui,
在 Git Gui 中,
- Stage the entire file that contains the unwanted change(s)
- Unstage the lines/hunks you want reverted
- Select the file in the Unstaged Changespane and Commit->Revert Changesto undo all (unstaged) changes to that file
- 暂存包含不需要的更改的整个文件
- 取消你想要还原的台词/帅哥
- 在Unstaged Changes窗格中选择文件,然后Commit- >Revert Changes以撤消对该文件的所有(未暂存)更改
Source: http://git.661346.n2.nabble.com/Revert-hunk-td4991128.html
来源:http: //git.661346.n2.nabble.com/Revert-hunk-td4991128.html
回答by Matt Zimmerman
I use git stash -p
for this purpose. It has a side effect of creating a stash with the changes which were removed from the working tree, which is occasionally useful to quickly restore them.
我git stash -p
用于这个目的。它有一个副作用,即使用从工作树中删除的更改创建存储,这有时有助于快速恢复它们。
回答by Ronino
Since Git 2.24, Git GUI features the "Revert Hunk" and "Revert Line" options in the context menu, similar to staging. You can even "Undo Last Revert" which is a great addition.
自 Git 2.24 起,Git GUI 在上下文菜单中具有“Revert Hunk”和“Revert Line”选项,类似于暂存。您甚至可以“撤消上次还原”,这是一个很好的补充。
Here's the commit that added this functionality: https://github.com/git/git/commit/62bd99934bb2b1a07f786fdf9b94d7b692768e30
这是添加此功能的提交:https: //github.com/git/git/commit/62bd99934bb2b1a07f786fdf9b94d7b692768e30