Git 只恢复提交中的一些文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36201607/
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
Git revert just some files in commit
提问by Matrosov Alexander
I have few commits on github that I want to change. So in my previous commit I've changed some file/folders and I need to revert changes only for some of them. What is the best solution to make it.
我在 github 上几乎没有要更改的提交。所以在我之前的提交中,我更改了一些文件/文件夹,我只需要恢复其中一些的更改。什么是最好的解决方案。
回答by JKillian
You can use git checkout:
您可以使用git checkout:
git checkout HEAD~ -- file/to/revert
to stage a version of the file from the previous commit. Then just commit the changes and you're good to go! Of course, you can replace HEAD~
, which references the previous commit, with a hash of a commit, a further-back ancestor, or any "tree-ish" object you wish.
暂存上次提交的文件版本。然后只需提交更改,您就可以开始了!当然,您可以用HEAD~
提交的哈希值、更远的祖先或您希望的任何“树状”对象替换引用先前提交的 。
回答by CodeWizard
I have few commits on github that I want to change.
I need to revert changes only for some of them
我在 github 上几乎没有要更改的提交。
我只需要还原其中一些更改
Few options:
几个选项:
Checkout the desired files from the desired commit
git checkout <commit> path/to/file
从所需的提交中检出所需的文件
git checkout <commit> path/to/file
Interactive rebase
// X is the number of commits you wish to edit git rebase -i HEAD~X
交互式变基
// X is the number of commits you wish to edit git rebase -i HEAD~X
Once you squash your commits - choose the e
for edit the commit.
压缩提交后 - 选择e
编辑提交。