Git取消某些文件中的更改 [英] Git undo changes in some files

查看:418
本文介绍了Git取消某些文件中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编码时,我将打印语句添加到某些文件中以跟踪发生的情况。



当我完成时,是否可以恢复某些文件中的更改,但提交我实际工作的文件?假设我在文件 A 中添加了打印,但我修改了文件 B B 是我想提交的内容, A ,我想要回到原来的状态。

解决方案

有三种基本的方法可以做到这一点,这取决于您对文件A所做的更改。如果您还没有添加对索引进行更改或提交它们,那么您只是想使用checkout命令 - 这会更改工作副本的状态以匹配存储库:

  git checkout A 

如果您已将其添加到索引,请使用reset :

  git reset A 

如果你已经提交了,那么你使用revert命令:

 #the -n表示,不要提交回复尚未
git revert -n< sha1>
#现在确保我们只是将回复提交给A
git reset B
git commit

另一方面,如果您已经提交了它,但提交涉及很多您不想还原的文件,则上述方法可能涉及大量重置B命令。在这种情况下,您可能想要使用此方法:

 #revert,但不要提交
git revert -n< sha1>
#清除索引中的所有更改
git reset
#现在只需添加A
git add A
git commit

另一种方法是需要使用rebase -i命令。如果您有多个提交进行编辑,这个可能很有用:

 #使用rebase -i来樱桃选择提交想要编辑
#在你想要编辑的那个之前指定提交的sha1
#你得到一个带有一个文件和一堆以pick开头的行的编辑器
#改变一个或多个你想编辑的编辑,然后保存文件
git rebase -i< sha1>
#现在你进入一个循环,对于你设置为edit的每个提交,你基本上会重做从头开始的提交
#假设我们只是选择了一个提交了错误的A提交
git reset A
git commit --amend
#返回循环的开始处
git rebase --continue


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.

解决方案

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:

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:

# 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:

# 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:

# 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

这篇关于Git取消某些文件中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆