撤消部分未分档的git变更 [英] Undo part of unstaged changes in git
问题描述
如何在git中取消部分未暂存的更改,但将其余部分保留为未暂存?我发现的方式是:
$ b $ pre $ git commit --interactive
#选择我想要删除的部分
#提交更改
git stash
git rebase -i master#(我是master的祖先)
#删除最近提交的行
git stash apply
这有效,但如果有像 git commit --interactive
仅用于恢复更改。任何更好的方法?
您可以使用 git checkout -p
,它允许您从工作副本和索引之间的差异中选择个别区块进行还原。同样, git add -p
允许您选择要添加到索引的区块,并且 git reset -p
允许您从索引和HEAD之间的差异中选择个别区块以退出索引。
$ git checkout -p file / to / partially / revert
#or ...
$ git checkout -p。
如果您希望在恢复之前预先为您的git存储库进行快照以保留这些更改,我喜欢做:
$ git stash; git stash apply
如果你经常使用它,你可能想要别名:
[别名]
checkpoint =!git stash; git stash apply
如果您使用良好的编辑器模式或插件,恢复个别的hunk或行可能更容易,这可能会为直接选择行来恢复提供支持,因为 -p
有时可能有点笨拙。我使用 Magit ,这是一种Emacs模式,对于使用Git非常有帮助。在Magit中,你可以运行 magit-status
,找到想要恢复的变化的差异,选择你想要恢复的行(或者简单地把光标放在hunk如果您想一次恢复一个大块而不是一次一行),然后按 k
恢复这些特定行。如果您使用Emacs,我强烈推荐Magit。
How do I undo parts of my unstaged changes in git but keep the rest as unstaged? The way I figured out is:
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 --interactive
only for reverting changes. Any better methods?
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 -p
allows you to choose hunks to add to the index, and git reset -p
allows you to choose individual hunks from the diff between the index and HEAD to back out of the index.
$ 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 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 -p
can 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 k
to revert those specific lines. I highly recommend Magit if you use Emacs.
这篇关于撤消部分未分档的git变更的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!