UNDOING STAGED CHANGES (BEFORE COMMITTING)

本文介绍如何使用Git撤销已暂存的文件更改,包括如何查看暂存区的状态、撤销暂存区的更改以及如何检查文件的已提交版本。

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

GOALS
  • Learn how to revert changes that have been staged
Change the file and stage the change01

Modify the hello.rb file to have a bad comment

FILE: hello.rb
# This is an unwanted but staged comment
name = ARGV.first || "World"

puts "Hello, #{name}!"

And then go ahead and stage it.

EXECUTE:
git add hello.rb
Check the Status02

Check the status of your unwanted change.

EXECUTE:
git status
OUTPUT:
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   hello.rb
#

The status output shows that the change has been staged and is ready to be committed.

Reset the Staging Area03

Fortunately the status output tells us exactly what we need to do to unstage the change.

EXECUTE:
git reset HEAD hello.rb
OUTPUT:
$ git reset HEAD hello.rb
Unstaged changes after reset:
M	hello.rb

The reset command resets the staging area to be whatever is in HEAD. This clears the staging area of the change we just staged.

The reset command (by default) doesn’t change the working directory. So the working directory still has the unwanted comment in it. We can use thecheckout command of the previous lab to remove the unwanted change from the working directory.

Checkout the Committed Version04
EXECUTE:
git checkout hello.rb
git status
OUTPUT:
$ git status
# On branch master
nothing to commit (working directory clean)

And our working directory is clean once again.

 

reference:http://gitimmersion.com/lab_15.html

转载于:https://www.cnblogs.com/joe-yang/archive/2012/04/26/2471094.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值