git: revert (reset) a single file

本文介绍如何使用Git撤销工作区中未提交的更改,包括如何使用`git checkout`命令回退到最近一次提交的状态,以及如何区分使用此命令的不同形式以避免意外删除其他未提交的更改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

This one is hard to find out there so here it is. If you have an uncommitted change (its only in your working copy) that you wish to revert (in SVN terms) to the copy in your latest commit, do the following:

git checkout filename

This will checkout the file from HEAD, overwriting your change. This command is also used to checkout branches, and you could happen to have a file with the same name as a branch. All is not lost, you will simply need to type:

git checkout -- filename

You can also do this with files from other branches, and such. man git-checkout has the details.

The rest of the Internet will tell you to use git reset --hard, but this resets alluncommitted changes you’ve made in your working copy. Type this with care.


Ref:  http://www.norbauer.com/notebooks/notes/git-revert-reset-a-single-file.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值