如何让暂存区的文件恢复到和HEAD一样以及如何让工作区的文件恢复到和暂存区一样

如何让暂存区的文件恢复到和HEAD一样以及如何让工作区的文件恢复到和暂存区一样

十月 21, 2019

如何让暂存区的文件恢复到和HEAD一样以及如何让工作区的文件恢复到和暂存区一样

如何让暂存区的文件恢复到和HEAD一样

  • git reset HEAD
$ git status #将文件修改好加入暂存区,查看当前状态
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: a
modified: abc
$ git reset HEAD #根据提示 使用reset HEAD命令将所有暂存区文件都还原到HEAD
Unstaged changes after reset:
M a
M abc
$ git status #OK,还原成功
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: a
modified: abc

no changes added to commit (use "git add" and/or "git commit -a")
$ git diff --cached #还可以通过这种方式查看暂存区有没有文件和HEAD存在差异,没有返回证明无差异

如何取消暂存区部分文件的修改

  • git reset HEAD —[filename1,filename2,…]
$ git status #查看当前目录状态 有三个已经加入暂存区的文件
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: abc
modified: add
modified: commit1

$ git reset HEAD -- abc commit1 #使用reset命令只还原abc和commit1文件
Unstaged changes after reset:
M abc
M commit1
$ git status #再次查看目录状态 abc和commit1文件已经从暂存区中被取消
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: add

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: abc
modified: commit1

如何让工作区的文件恢复到和暂存区一样

  • git checkout — [filename]
$ vi a
$ git add a #假设修改了a文件 并将a加入暂存区
$ vi a #然后觉得a不太好,又修改了内容
$ git diff #修改后查看了工作区和暂存区的差异 发现工作区的修改还不如暂存区
diff --git a/a b/a
index 79ca77c..e63ef0c 100644
--- a/a
+++ b/a
@@ -1 +1 @@
-Hello,I'm a!!!
+Hello,I'm a!!!.....
$ git status #查看一下当前状态
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: a

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: a
$ git checkout -- a #按照上边的提示 忽略对a文件的修改
$ git status #再次查看状态 工作区的修改已经被抹掉
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: a