Git 撤消某些文件中的更改

发布于 2024-07-22 21:20:28 字数 202 浏览 9 评论 0 原文

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

当我完成后,是否可以恢复某些文件中的更改,但提交我实际处理的文件?

假设我在文件 A 中添加了打印,但我修改了文件 BB 是我想要提交的内容,而 A 是我想要设置回其旧状态的内容。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

無處可尋 2024-07-29 21:20:28

有三种基本方法可以执行此操作,具体取决于您对文件 A 所做的更改。如果您尚未将更改添加到索引或提交它们,那么您只想使用 checkout 命令 - 这将发生变化工作副本的状态以匹配存储库:

git checkout A

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

git reset A

如果您已提交它,则使用恢复命令:

# 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

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
一绘本一梦想 2024-07-29 21:20:28

来源:http://git-scm.com/book/en/Git-Basics-Undoing-事情

git checkout --modifiedfile.java


1)$ git status

你会看到修改后的文件

2)$git checkout --modifiedfile.java

3)$git status

Source : http://git-scm.com/book/en/Git-Basics-Undoing-Things

git checkout -- modifiedfile.java


1)$ git status

you will see the modified file

2)$git checkout -- modifiedfile.java

3)$git status

此生挚爱伱 2024-07-29 21:20:28
git add B # Add it to the index
git reset A # Remove it from the index
git commit # Commit the index
git add B # Add it to the index
git reset A # Remove it from the index
git commit # Commit the index
海螺姑娘 2024-07-29 21:20:28

是的;

git commit FILE

将仅提交文件。 然后您可以用来

git reset --hard

撤消其他文件中的本地更改。

可能还有其他我不知道的方法...

编辑:或者,正如 NicDumZ 所说,git-checkout 只是您想要撤消更改的文件(最佳解决方案取决于是否有更多文件要提交)或更多要撤消的文件:-)

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

陌路黄昏 2024-07-29 21:20:28

为什么不能简单地使用“git add ”(甚至“git add --interactive”,或者“git gui”,它有交互式提交的选项),然后使用“git commit”而不是“git commit -a”?

在您的情况下(对于您的示例),它将是:

prompt> git add B
prompt> git commit

仅提交对文件 B 的更改,并且文件 A 将保留为“脏”,即在工作区域版本中包含那些打印语句。 就足够了。

prompt> git reset A

当您想删除这些打印语句时,使用或

prompt> git checkout HEAD -- A

恢复到提交版本(来自 HEAD 的版本,即“git show HEAD:A”版本)

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"?

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

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文