Ham*_*aya 184 git version-control file undo revert
编码时我将print语句添加到某些文件中以跟踪发生的情况.
当我完成后,是否可以恢复某些文件中的更改,但提交我实际工作的文件?
说我在文件中添加了print A,但是我修改了文件B.B是我想要提交的A,我希望回到原来的状态.
180*_*ION 287
有三种基本方法可以执行此操作,具体取决于您对文件A的更改所做的操作.如果您尚未将更改添加到索引或提交它们,那么您只想使用checkout命令 - 这将更改与存储库匹配的工作副本的状态:
git checkout A
Run Code Online (Sandbox Code Playgroud)
如果已将其添加到索引中,请使用reset:
git reset A
Run Code Online (Sandbox Code Playgroud)
如果您已提交,则使用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
Run Code Online (Sandbox Code Playgroud)
另一方面,如果你已经提交了它,但是提交涉及很多你不想还原的文件,那么上面的方法可能涉及很多"重置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
Run Code Online (Sandbox Code Playgroud)
另一种方法,需要使用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
Run Code Online (Sandbox Code Playgroud)
dek*_*dev 20
来源:http://git-scm.com/book/en/Git-Basics-Undoing-Things
git checkout - modifiedfile.java
1)$ git状态
你会看到修改过的文件
2)$ git checkout - modifiedfile.java
3)$ git状态
git add B # Add it to the index
git reset A # Remove it from the index
git commit # Commit the index
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
150869 次 |
| 最近记录: |