目录
对比文件差异
一、暂存区和HEAD文件差异查看
1、指令
git diff --staged
#或者
git diff --cached
2、演示
#我们修改index.html文件,并将修改提交到暂存区
vi index.html
#提交到暂存区
git add index.html
#查看暂存区与HEAD的文件区别,使用下面的命令
localhost:gitLearning LC$ git diff --cached
diff --git a/index.html b/index.html
index 6ad4c68..4fbef7a 100644
--- a/index.html
+++ b/index.html
@@ -29,7 +29,7 @@
<div class="accordion"><h1>Basic Commands</h1></div>
<div class="panel">
<ol>
- <li></li>
+ <li>config</li>
<li></li>
<li></li>
<li></li>
localhost:gitLearning LC$
二、工作区和暂存区文件差异查看
1、指令
git diff
2、演示
#修改style.css文件后保存退出
localhost:gitLearning LC$ vim styles/style.css
#查看工作区和暂存区文件区别,使用下面的命令
localhost:gitLearning LC$ git diff
diff --git a/styles/style.css b/styles/style.css
index 4c6bc45..ba3150d 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -5,7 +5,7 @@ body{
}
body a{
- color: white;
+ color: black;
}
header{
localhost:gitLearning LC$
三、如果想查看某个具体文件的差异可以使用
localhost:gitLearning LC$ git diff --cached -- index.html
localhost:gitLearning LC$ git diff -- styles/style.css
四、对比任意两次commit的差异
#对比两个分支的不同
git diff temp master
#对比两次commit的不同
git diff hash1 hash2
#查看具体文件的不同
git diff hash1 hash2 -- index.html
恢复暂存区、工作区
一、恢复暂存区
1、指令
git restore --staged <file>
2、演示
#我们先看下Git的状态
localhost:gitLearning LC$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: readme
modified: styles/style.css
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
localhost:gitLearning LC$
#通过提示,我们可以使用命令git restore --staged <file> 命令来实现
localhost:gitLearning LC$ git restore --staged .
localhost:gitLearning LC$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
modified: readme
modified: styles/style.css
no changes added to commit (use "git add" and/or "git commit -a")
localhost:gitLearning LC$
#也可以使用下面的命令来恢复
#所有文件
localhost:gitLearning LC$ git reset HEAD
#或者指令特定的文件
localhost:gitLearning LC$ git reset HEAD readme
二、恢复工作区
1、指令
git restore <file>
2、演示
localhost:gitLearning LC$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
modified: readme
modified: styles/style.css
localhost:gitLearning LC$
#根据提示,我们使用git restore 命令
localhost:gitLearning LC$ git restore index.html
localhost:gitLearning LC$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme
modified: styles/style.css
localhost:gitLearning LC$
重置HEAD指向
1、指令
#该指令可以重置HEAD的指向,并清空暂存区和工作区
git reset
2、演示
#看下commit记录
localhost:gitLearning LC$ git log --oneline
206309d (HEAD -> temp) detached test
fe9b9f9 Add index.html
8119c2d Add readme
f7eed8c Initial
#使用 git reset命令来重置HEAD指向
localhost:gitLearning LC$ git reset --hard fe9b9f9
HEAD is now at fe9b9f9 Add index.html
#重置后的状态,丢弃了一个commit,并且无法找回
localhost:gitLearning LC$ git log --oneline
fe9b9f9 (HEAD -> temp) Add index.html
8119c2d Add readme
f7eed8c Initial
localhost:gitLearning LC$
#还可以使用下面的指令
#将该次commit取消,并将变更的内容保留在暂存区
git reset --soft fe9b9f9
#将该次commit取消,并将变更的内容保留在工作区,或状态为Untracked files:
git reset fe9b9f9
3、演示2
#同时清空暂存区和工作区
git restore --staged .
git restore .
#方法二:
git reset --hard HEAD
删除文件的方法
1、指令
git rm fileName
2、演示
localhost:gitLearning LC$ ls -l
total 16
drwxr-xr-x 3 LC staff 96 1 13 14:25 images
-rw-r--r-- 1 LC staff 1303 1 13 17:03 index.html
drwxr-xr-x 3 LC staff 96 1 13 14:25 js
-rw-r--r-- 1 LC staff 51 1 13 15:59 readme
localhost:gitLearning LC$ git rm readme
rm 'readme'
localhost:gitLearning LC$ git status
On branch temp
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: readme
localhost:gitLearning LC$
行者常至,为者常成!