UNDOING COMMITTED CHANGES

GOALS
  • Learn how to revert changes that have been committed to a local repository.
Undoing Commits01

Sometimes you realized that a change that you have already committed was not correct and you wish to undo that commit. There are several ways of handling that issue, and the way we are going to use in this lab is always safe.

Essentially we will undo the commit by creating a new commit that reverses the unwanted changes.

Change the file and commit it.02

Change the hello.rb file to the following.

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

puts "Hello, #{name}!"
EXECUTE:
git add hello.rb
git commit -m "Oops, we didn't want this commit"
Create a Reverting Commit03

To undo a committed change, we need to generate a commit that removes the changes introduced by our unwanted commit.

EXECUTE:
git revert HEAD

This will pop you into the editor. You can edit the default commit message or leave it as is. Save and close the file. You should see …

OUTPUT:
$ git revert HEAD --no-edit
[master 075015f] Revert "Oops, we didn't want this commit"
 1 files changed, 1 insertions(+), 1 deletions(-)

Since we were undoing the very last commit we made, we were able to useHEAD as the argument to revert. We can revert any arbitrary commit earlier in history by simply specifying its hash value.

Note: The --no-edit in the output can be ignored. It was necessary to generate the output without opening the editor.

Check the log04

Checking the log shows both the unwanted and the reverting commits in our repository.

EXECUTE:
git hist
OUTPUT:
$ git hist
* 075015f 2012-03-06 | Revert "Oops, we didn't want this commit" (HEAD, master) [Jim Weirich]
* 4773739 2012-03-06 | Oops, we didn't want this commit [Jim Weirich]
* e2a3502 2012-03-06 | Added a comment (v1) [Jim Weirich]
* d46b035 2012-03-06 | Added a default value (v1-beta) [Jim Weirich]
* fa23feb 2012-03-06 | Using ARGV [Jim Weirich]
* 4ac2080 2012-03-06 | First Commit [Jim Weirich]

This technique will work with any commit (although you may have to resolve conflicts). It is safe to use even on branches that are publicly shared on remote repositories.

 

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值