选择性还原或检出Git中文件的更改? [英] Selectively revert or checkout changes to a file in Git?

查看:120
本文介绍了选择性还原或检出Git中文件的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个命令允许您部分撤消对工作目录中文件(或多个文件)的更改?



假设您已经编辑了很多文件但你意识到你想撤消一些变化回到提交状态,但不是其他变化。



我想象一个选项 git checkout ,它的工作原理与 git add -p 很相似,即它通过hunk来检查文件大小,并询问是否要保留您可以使用

 

git add -p<路径>

将您想保留在特定文件中的块分阶段处理,然后

  git checkout  - <路径> 

放弃您不想保留的工作树更改,方法是检出暂存版本



最后,您可以使用

  git reset - <路径> 

将文件的暂存版本还原为最近提交的文件版本,您的更改将停止。


Is there a command that allows you to partially undo the changes to a file (or files) in the working directory?

Suppose you have edited a file a lot but you realize that you want to undo some of the changes back to the committed state, but not the other changes.

I'm envisioning an option for git checkout that works a lot like git add -p, i.e. it goes through the file hunk by hunk and asks if you want to keep it or not.

解决方案

You could use

git add -p <path>

to stage the chunks that you want to keep in a particular file, then

git checkout -- <path>

to discard the working tree changes that you didn't want to keep, by checking out the staged version of the file.

Finally, you can use

git reset -- <path>

to revert the staged version of the file to the most recent committed version of the file to leave you with your changes unstaged.

这篇关于选择性还原或检出Git中文件的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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