集册 沉浸式学 Git 撤销本地更改

撤销本地更改

欢马劈雪     最近更新时间:2020-08-04 05:37:59

90

目的

学习如何还原工作目录中的更改。

检出 Master

在处理之前确认你在 master 中的最新提交上。

$ git checkout master

更改 hello.rb

有时候你修改了本地工作目录中的文件,且想要还原已经提交的内容。checkout 命令可以用来处理这种情况。

更改 hello.rb 让其具有错误的注释。

# This is a bad comment.  We want to revert it.
name = ARGV.first || "World"

puts "Hello, #{name}!"

检查状态

首先,检查工作目录的状态。

$ git status
$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   hello.rb
#
no changes added to commit (use "git add" and/or "git commit -a")

我们看到 hello.rb 已被修改,但还没有暂存。

还原工作目录中的更改

使用 checkout 命令来检出 hello.rb 在仓库中的版本。

$ git checkout hello.rb
$ git status
$ cat hello.rb
展开阅读全文