git Git撤消某些文件中的更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/933329/
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 undo changes in some files
提问by Hamza Yerlikaya
While coding I added print statements into some files to keep track of what was going on.
在编码时,我将打印语句添加到一些文件中以跟踪正在发生的事情。
When I am done, is it possible to revert changes in some files, but commit the file I actually worked on?
完成后,是否可以还原某些文件中的更改,但提交我实际处理的文件?
Say I added print in file A
, but I modified file B
. B
is what I want to commit and A
, I want to be set back to its old state.
假设我在 file 中添加了 print A
,但我修改了 file B
。B
是我想提交的A
,我想回到原来的状态。
回答by 1800 INFORMATION
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:
根据您对文件 A 所做的更改,有三种基本方法可以执行此操作。如果您尚未将更改添加到索引或提交它们,那么您只想使用 checkout 命令 - 这将更改与存储库匹配的工作副本的状态:
git checkout A
If you added it to the index already, use reset:
如果您已经将其添加到索引中,请使用重置:
git reset A
If you had committed it, then you use the revert command:
如果您已提交,则使用 revert 命令:
# the -n means, do not commit the revert yet
git revert -n <sha1>
# now make sure we are just going to commit the revert to A
git reset B
git commit
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:
另一方面,如果您已经提交了它,但是提交涉及到很多您也不想还原的文件,那么上述方法可能涉及很多“重置 B”命令。在这种情况下,您可能希望使用此方法:
# revert, but do not commit yet
git revert -n <sha1>
# clean all the changes from the index
git reset
# now just add A
git add A
git commit
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:
另一种方法,需要使用 rebase -i 命令。如果您有多个提交要编辑,则此方法很有用:
# use rebase -i to cherry pick the commit you want to edit
# specify the sha1 of the commit before the one you want to edit
# you get an editor with a file and a bunch of lines starting with "pick"
# change the one(s) you want to edit to "edit" and then save the file
git rebase -i <sha1>
# now you enter a loop, for each commit you set as "edit", you get to basically redo that commit from scratch
# assume we just picked the one commit with the erroneous A commit
git reset A
git commit --amend
# go back to the start of the loop
git rebase --continue
回答by dekdev
Source : http://git-scm.com/book/en/Git-Basics-Undoing-Things
来源:http: //git-scm.com/book/en/Git-Basics-Undoing-Things
git checkout -- modifiedfile.java
git checkout -- modifiedfile.java
1)$ git status
1)$ git 状态
you will see the modified file
你会看到修改后的文件
2)$git checkout -- modifiedfile.java
2)$git checkout -- modifiedfile.java
3)$git status
3)$git 状态
回答by Stefan Mai
git add B # Add it to the index
git reset A # Remove it from the index
git commit # Commit the index
回答by Nicolas Dumazet
man git-checkout: git checkout A
人 git-checkout:git checkout A
回答by Jay
Yes;
是的;
git commit FILE
will commit just FILE. Then you can use
将只提交文件。然后你可以使用
git reset --hard
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 :-)
编辑:或者,正如 NicDumZ 所说,git-checkout 只是您想要撤消更改的文件(最佳解决方案取决于是否有更多文件要提交或更多文件要撤消:-)
回答by Jakub Nar?bski
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"?
为什么不能简单地使用“ git add <file>”(甚至“ git add --interactive”或具有交互式提交选项的“ git gui”)标记您想要在提交中进行的更改,然后使用“git commit”而不是“git commit -a”?
In your situation (for your example) it would be:
在您的情况下(对于您的示例),它将是:
prompt> git add B
prompt> git commit
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
只会提交对文件 B 的更改,而文件 A 将被保留为“脏”,即工作区版本中的那些打印语句。当你想删除那些打印语句时,使用它就足够了
prompt> git reset A
or
或者
prompt> git checkout HEAD -- A
to revert to comitted version (version from HEAD, i.e. "git show HEAD:A" version).
恢复到提交的版本(来自 HEAD 的版本,即“git show HEAD:A”版本)。