Git 撤消某些文件中的更改
在编码时,我将打印语句添加到一些文件中以跟踪发生的情况。
当我完成后,是否可以恢复某些文件中的更改,但提交我实际处理的文件?
假设我在文件 A
中添加了打印,但我修改了文件 B
。 B
是我想要提交的内容,而 A
是我想要设置回其旧状态的内容。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有三种基本方法可以执行此操作,具体取决于您对文件 A 所做的更改。如果您尚未将更改添加到索引或提交它们,那么您只想使用 checkout 命令 - 这将发生变化工作副本的状态以匹配存储库:
如果您已将其添加到索引,请使用重置:
如果您已提交它,则使用恢复命令:
如果另一方面,您已提交它,但提交如果涉及很多您不想恢复的文件,那么上述方法可能涉及很多“重置 B”命令。 在这种情况下,您可能想使用此方法:
又一个方法,需要使用 rebase -i 命令。 如果您有多个提交需要编辑,这一点可能会很有用:
There are three basic ways to do this depending on what you have done with the changes to the file A. If you have not yet added the changes to the index or committed them, then you just want to use the checkout command - this will change the state of the working copy to match the repository:
If you added it to the index already, use reset:
If you had committed it, then you use the revert command:
If on the other hand, you had committed it, but the commit involved rather a lot of files that you do not also want to revert, then the above method might involve a lot of "reset B" commands. In this case, you might like to use this method:
Another method again, requires the use of the rebase -i command. This one can be useful if you have more than one commit to edit:
来源:http://git-scm.com/book/en/Git-Basics-Undoing-事情
git checkout --modifiedfile.java
1)$ git status
你会看到修改后的文件
2)$git checkout --modifiedfile.java
3)$git status
Source : http://git-scm.com/book/en/Git-Basics-Undoing-Things
git checkout -- modifiedfile.java
1)$ git status
you will see the modified file
2)$git checkout -- modifiedfile.java
3)$git status
man git-checkout:
git checkout A
man git-checkout:
git checkout A
是的;
将仅提交文件。 然后您可以用来
撤消其他文件中的本地更改。
可能还有其他我不知道的方法...
编辑:或者,正如 NicDumZ 所说,git-checkout 只是您想要撤消更改的文件(最佳解决方案取决于是否有更多文件要提交)或更多要撤消的文件:-)
Yes;
will commit just FILE. Then you can use
to undo local changes in other files.
There may be other ways too that I don't know about...
edit: or, as NicDumZ said, git-checkout just the files you want to undo the changes on (the best solution depends on wether there are more files to commit or more files to undo :-)
为什么不能简单地使用“git add ”(甚至“git add --interactive”,或者“git gui”,它有交互式提交的选项),然后使用“git commit”而不是“git commit -a”?
在您的情况下(对于您的示例),它将是:
仅提交对文件 B 的更改,并且文件 A 将保留为“脏”,即在工作区域版本中包含那些打印语句。 就足够了。
当您想删除这些打印语句时,使用或
恢复到提交版本(来自 HEAD 的版本,即“git show HEAD:A”版本)
Why can't you simply mark what changes you want to have in a commit using "git add <file>" (or even "git add --interactive", or "git gui" which has option for interactive comitting), and then use "git commit" instead of "git commit -a"?
In your situation (for your example) it would be:
Only changes to file B would be comitted, and file A would be left "dirty", i.e. with those print statements in the working area version. When you want to remove those print statements, it would be enought to use
or
to revert to comitted version (version from HEAD, i.e. "git show HEAD:A" version).