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

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

问题描述

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

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?

假设我在文件 A 中添加了打印,但我修改了文件 B.B 是我想要提交的,而 A 是我想要返回到它的旧状态.

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.

推荐答案

根据您对文件 A 所做的更改,有三种基本方法可以执行此操作.如果您尚未将更改添加到索引或提交它们,然后您只想使用 checkout 命令 - 这将更改工作副本的状态以匹配存储库:

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

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

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

另一方面,如果你已经提交了它,但是提交涉及到很多你不想恢复的文件,那么上面的方法可能涉及很多重置B"命令.在这种情况下,您可能希望使用这种方法:

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

另一种方法,需要使用 rebase -i 命令.如果您有多个提交要编辑,则此方法很有用:

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天全站免登陆