如何反转 git 中的特定提交块?
我想知道是否有一种方法可以快速逆转提交的特定部分。
我可以在两次提交之间生成 diff
或 HEAD
来查看差异。
我如何仅逆转其中一个帅哥(或者更好的是,逆转一组特定帅哥)?
I'm wondering if there is a way to quickly reverse specific hunk of a commit.
I can generate a diff
between two commits or HEAD
to see the difference.
How do I reverse just one of those hunks (or better yet, a set of specific hunks)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
例如,
其中
$REF
是引用名称或提交 ID,指定要从中获取文件状态的提交。例如,要选择性地恢复上次提交中所做的更改,请使用HEAD^
。e.g.,
Where
$REF
is a ref name or commit ID that specifies the commit you want to take the file state from. For example, to selectively revert changes made in the last commit, useHEAD^
.要从特定提交中恢复一个或多个块,请执行以下操作 - 本质上使用
add -p
:未来的 git 版本可能会直接支持
git revert -p
...注意:像 git checkout/restore -p ... 这样的方法不会始终从特定提交中恢复块,而是从某个文件状态中选取部分 - 可能会丢失其他后续提交中的更改。
To revert one or more hunks from specific commit(s) do like this - using
add -p
in essence:Future git versions may support
git revert -p
directly...Note: Methods like
git checkout/restore -p ...
do not consistently revert hunks from a specific commit but go to pick parts from a certain file state - possibly loosing changes in other later commits.其中
$REF
是引用名称或提交 ID,指定要从中获取文件状态的提交。例如,要选择性地恢复上次提交中所做的更改,请使用 HEAD^。@cdhowie 已经回答了这个问题,但我发现使用像
meld
这样的交互式 difftool 来选择性地恢复旧的代码块/代码行会更好一些,特别是如果有新引入的硬-查找代码中的错误。where
$REF
is a ref name or commit ID that specifies the commit you want to take the file state from. For example, to selectively revert changes made in the last commit, use HEAD^.This question was already answered by @cdhowie, but I find it somewhat nicer to use an interactive difftool like
meld
to selectively restore old hunks/lines of code, especially if there is a newly-introduced, hard-to-find bug in the code.要从以前的提交恢复已删除的文件,我在这里使用了答案:
查找并在 Git 存储库中恢复已删除的文件
To recover a deleted file from a previous commit I used the answer here:
Find and restore a deleted file in a Git repository
我喜欢这样做的方法是使用:
git commit --amend
。git rebase --continue
结束 rebase。The way I like to do this is to use:
git rebase -i HEAD~
n where 'n' is the number of commits back.git add -p
or manual edits.git commit --amend
.git rebase --continue
to end the rebase.