git 撤消git中未暂存更改的部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1981830/
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 07:35:49  来源:igfitidea点击:

Undo part of unstaged changes in git

git

提问by asmeurer

How do I undo parts of my unstaged changes in git but keep the rest as unstaged? The way I figured out is:

如何撤消 git 中未暂存更改的部分内容,但将其余部分保持为未暂存状态?我想出来的方法是:

git commit --interactive
# Choose the parts I want to delete
# Commit the changes
git stash
git rebase -i master # (I am an ancestor of master)
# Delete the line of the most recent commit
git stash apply

This works, but it would be nice if there were something like git commit --interactiveonly for reverting changes. Any better methods?

这有效,但如果有类似git commit --interactive仅用于恢复更改的东西会很好。有什么更好的方法吗?

回答by Brian Campbell

You can use git checkout -p, which lets you choose individual hunks from the diff between your working copy and index to revert. Likewise, git add -pallows you to choose hunks to add to the index, and git reset -pallows you to choose individual hunks from the diff between the index and HEAD to back out of the index.

您可以使用git checkout -p,它允许您从工作副本和索引之间的差异中选择单个大块以进行还原。同样,git add -p允许您选择要添加到索引的块,并git reset -p允许您从索引和 HEAD 之间的差异中选择单个块以退出索引。

$ git checkout -p file/to/partially/revert
# or ...
$ git checkout -p .

If you wish to snapshot your git repository beforehand to preserve these changes before reverting them, I like to do:

如果您希望在还原它们之前预先对 git 存储库进行快照以保留这些更改,我喜欢这样做:

$ git stash; git stash apply

If you use that often, you might want to alias it:

如果你经常使用它,你可能想给它起别名:

[alias]
    checkpoint = !git stash; git stash apply

Reverting individual hunks or lines can be even easier if you use a good editor mode or plugin, which may provide support for selecting lines directly to revert, as -pcan be a bit clumsy to use sometimes. I use Magit, an Emacs mode that is very helpful for working with Git. In Magit, you can run magit-status, find the diffs for the changes that you want to revert, select the lines you wish to revert (or simply put the cursor on hunks you wish to revert if you want to revert a hunk at a time instead of a line at a time), and press kto revert those specific lines. I highly recommend Magit if you use Emacs.

如果您使用良好的编辑器模式或插件,恢复单个大块或行会更容易,这可能支持直接选择行进行恢复,因为-p有时使用起来有点笨拙。我使用Magit,这是一种对使用 Git 非常有帮助的 Emacs 模式。在 Magit 中,您可以运行magit-status,找到要还原的更改的差异,选择要还原的行(或者如果您想一次还原一个大块而不是简单地将光标放在要还原的大块上一次一行),然后按k恢复这些特定行。如果您使用 Emacs,我强烈推荐 Magit。

回答by octoberblu3

git diff > patchfile

Then edit the patchfile and remove the parts you don't want to undo, then:

然后编辑补丁文件并删除您不想撤消的部分,然后:

patch -R < patchfile

回答by Drew Hoskins

You could do

你可以做

git checkout master -- path/to/file

For each file you want to reset.

对于您要重置的每个文件。

回答by Abizern

How about

怎么样

  1. Back out the affected files with changes from the index
  2. use git add -pto only add back the changes that you want to the index.
  1. 从索引中退出受影响的文件
  2. 用于git add -p仅将您想要的更改添加回索引。

回答by G-Wiz

Brian Campbell's answercrashes my git, version 1.9.2.msysgit.0, for reasons unknown, so my approach is to stage hunks I want to keep, discard changes in the working copy, then unstage.

Brian Campbell 的回答使我的 git 版本 1.9.2.msysgit.0 崩溃,原因未知,所以我的方法是暂存我想保留的大块头,丢弃工作副本中的更改,然后取消暂存。

$ git add -p
   ... select the hunks to keep
$ git checkout -- .
$ git reset HEAD .

回答by Jonathan Leffler

When I run 'git status', it says:

当我运行 'git status' 时,它说:

$ git status
# On branch fr/fr.002
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   makefile
#
no changes added to commit (use "git add" and/or "git commit -a")
$

So, to cancel the unstaged edits, it tells me to run:

因此,要取消未暂存的编辑,它会告诉我运行:

git checkout -- makefile

回答by Andrew

you can do git checkoutand give it name names of the parts you want to undo.

您可以执行git checkout并为其命名要撤消的部分的名称。