在编码时,我在一些文件中添加了打印语句以跟踪发生的情况。
完成后,是否有可能还原某些文件的更改,但提交我实际上正在工作的文件?
比如我在文件
完成后,是否有可能还原某些文件的更改,但提交我实际上正在工作的文件?
比如我在文件
A
中添加了打印语句,但修改了文件B
。 B
是我要提交的内容,而A
应该恢复到旧状态。A
中添加了打印语句,但修改了文件B
。 B
是我要提交的内容,而A
应该恢复到旧状态。根据您对文件A所做的更改情况,有三种基本方法可供选择。如果您尚未将更改添加到索引或提交它们,则只需使用checkout命令-此操作将更改工作副本的状态以匹配存储库:
git checkout A
如果您已将其添加到索引中,请使用reset:git reset A
如果你已经提交了它,那么你可以使用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
如果相反,你已经提交了这个修改,但是这次提交涉及到很多你不想撤销的文件,那么上面的方法可能会涉及很多“重置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
另一种方法是使用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
来源: http://git-scm.com/book/zh/v2/Git-基础-撤销操作
使用命令 git checkout -- modifiedfile.java 可以撤销对文件 modifiedfile.java 的修改。
1) 执行命令 git status,你会看到被修改的文件。
2) 执行命令 git checkout -- modifiedfile.java。
3) 再次执行命令 git status。
git checkout
已经完成了任务,谢谢! - ᴍᴇʜᴏᴠgit add B # Add it to the index
git reset A # Remove it from the index
git commit # Commit the index
git commit FILE
将只提交文件。然后你可以使用
git reset --hard
撤销其他文件的本地更改。
可能还有其他方法,我不太清楚...
编辑:或者,如NicDumZ所说,只在想要撤消更改的文件上使用git-checkout(最佳解决方案取决于提交的文件是否更多还是需要撤消的文件是否更多:-)
prompt> git add B
prompt> git commit
只有对文件B的更改才会被提交,而文件A将保持“脏”,即在工作区版本中具有这些打印语句。当您想要删除这些打印语句时,只需使用
prompt> git reset A
或者
prompt> git checkout HEAD -- A
回滚到已提交的版本(即来自HEAD的版本,例如“git show HEAD:A”版本)。