还原 Git 中文件的 select 行
Revert select lines of a file in Git
我提交了更改,添加了一些代码并删除了一些代码。现在,我想撤消它。文件中还有其他更改(以及提交中更改的其他文件)我不想撤消,因此标准 git 还原不会执行。我想要的是创建一个提交,通过提供行号来撤消这些 select 更改。最好是一种允许我使用我的 GUI 客户端(Github 桌面)来 select 差异中的更改/行号的技术。我该怎么做?
可能有多种方法可以实现这一点,但我会采用类似的方法:
# stage a revision of the given commit without making a commit of this revision
git revert -n <hash-of-commit-to-partially revert>
# Unstage these changes so the revision is only in the working tree
git reset
# Selectively stage only the parts which make the revisions which you want
git add -p <path-of-interest>
# Optional: discard other changes from the working tree for testing
# (you might need to "git clean" if you are discarding the revision
# of a change that originally removed a file)
git checkout -- .
# Commit the selected revision
git commit
从 this answer 扩展你可以:
git checkout [commit-id] --patch -- /path/to/file
我提交了更改,添加了一些代码并删除了一些代码。现在,我想撤消它。文件中还有其他更改(以及提交中更改的其他文件)我不想撤消,因此标准 git 还原不会执行。我想要的是创建一个提交,通过提供行号来撤消这些 select 更改。最好是一种允许我使用我的 GUI 客户端(Github 桌面)来 select 差异中的更改/行号的技术。我该怎么做?
可能有多种方法可以实现这一点,但我会采用类似的方法:
# stage a revision of the given commit without making a commit of this revision
git revert -n <hash-of-commit-to-partially revert>
# Unstage these changes so the revision is only in the working tree
git reset
# Selectively stage only the parts which make the revisions which you want
git add -p <path-of-interest>
# Optional: discard other changes from the working tree for testing
# (you might need to "git clean" if you are discarding the revision
# of a change that originally removed a file)
git checkout -- .
# Commit the selected revision
git commit
从 this answer 扩展你可以:
git checkout [commit-id] --patch -- /path/to/file