如何反转 git 中的特定提交块?

发布于 2024-10-04 07:43:46 字数 137 浏览 1 评论 0原文

我想知道是否有一种方法可以快速逆转提交的特定部分。

我可以在两次提交之间生成 diffHEAD 来查看差异。

我如何仅逆转其中一个帅哥(或者更好的是,逆转一组特定帅哥)?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

恋竹姑娘 2024-10-11 07:43:46
git checkout -p $REF -- path/to/file

例如,

git checkout -p HEAD^ myfile

其中 $REF 是引用名称或提交 ID,指定要从中获取文件状态的提交。例如,要选择性地恢复上次提交中所做的更改,请使用 HEAD^

git checkout -p $REF -- path/to/file

e.g.,

git checkout -p HEAD^ myfile

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^.

梦在夏天 2024-10-11 07:43:46

要从特定提交中恢复一个或多个块,请执行以下操作 - 本质上使用 add -p

git revert --no-commit <commit>…​ 
git reset                              # unstage things
git add -p [OPTIONS] [<pathspec>…​]     # choose hunks interactively
git restore .                          # wipe the rest (in root directory)
... maybe further changes ...
git commit [--fixup] ...

未来的 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:

git revert --no-commit <commit>…​ 
git reset                              # unstage things
git add -p [OPTIONS] [<pathspec>…​]     # choose hunks interactively
git restore .                          # wipe the rest (in root directory)
... maybe further changes ...
git commit [--fixup] ...

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.

原来是傀儡 2024-10-11 07:43:46
git difftool $REF -- /path/to/file

其中 $REF 是引用名称或提交 ID,指定要从中获取文件状态的提交。例如,要选择性地恢复上次提交中所做的更改,请使用 HEAD^。

@cdhowie 已经回答了这个问题,但我发现使用像 meld 这样的交互式 difftool 来选择性地恢复旧的代码块/代码行会更好一些,特别是如果有新引入的硬-查找代码中的错误。

git difftool $REF -- /path/to/file

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.

绅刃 2024-10-11 07:43:46

要从以前的提交恢复已删除的文件,我在这里使用了答案:

查找并在 Git 存储库中恢复已删除的文件

git checkout <deleting_commit>^ -- <file_path>

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 checkout <deleting_commit>^ -- <file_path>
拥有 2024-10-11 07:43:46

我喜欢这样做的方法是使用:

  1. git rebase -i HEAD~n 其中“n”是提交的次数。
  2. 在要从中删除块的提交上使用“编辑”。
  3. 接下来使用 git add -p 或手动编辑。
  4. git commit --amend
  5. git rebase --continue 结束 rebase。

The way I like to do this is to use:

  1. git rebase -i HEAD~n where 'n' is the number of commits back.
  2. Use 'edit', on the commit you want to remove the hunk from.
  3. Follow this with git add -p or manual edits.
  4. git commit --amend.
  5. git rebase --continue to end the rebase.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文