git reset 的三个参数
–soft 版本号 – hard 版本号 --mixed 版本号
测试之前 ,先创建文件夹 完成初始化操作
mkdir t1
git init
先创建 五个文件
touch {1..5}.c
一次性放到暂存区,然后 分别提交(五次),产生五次日志记录
git add .
git commit -m "1" 1.c
git commit -m "2" 2.c
git commit -m "3" 3.c
git commit -m "4" 4.c
git commit -m "5" 5.c
git status 查看暂存区状态
$ git status
On branch master
nothing to commit, working tree clean
修改 1.c 2.c
vim 1.c vim 2.c
并创建 6.c 7.c
touch {6..7}.c
git status (6.c 7.c 未被提交,所以未被追踪) ( 1.c 2.c 被修改 所以在工作区)
$ 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: 1.c
modified: 2.c
Untracked files:
(use "git add <file>..." to include in what will be committed)
6.c
7.c
no changes added to commit (use "git add" and/or "git commit -a")
为了测试命令,要让 工作区 暂存区 本地库都要有文件
将2.c 和7.c 添加到暂存区
此时 工作区(1,6.c) 暂存区(2,7.c) 本地库(3,4,5.c) 都有文件
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: 2.c
new file: 7.c
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: 1.c
Untracked files:
(use "git add <file>..." to include in what will be committed)
6.c
为了多次利用该测试文件夹的文件 注明: git 的所有信息也会拷贝,提交记录等等也全部一致
cp -r t1 t2
cp -r t1 t3
cp -r t1 t4
cp -r t1 t5
测试 git reset --soft 版本号 ,进入 t 2
文章目录
查看提交日志
$ git reflog
b49282b (HEAD -> master) HEAD@{0}: commit: 55555555
5fa1fc5 HEAD@{1}: commit: 4444444
6977b98 HEAD@{2}: commit: 3333333
e98a5e7 HEAD@{3}: commit: 222222222
89870a3 HEAD@{4}: commit (initial): 11111
恢复到 5的版本号
git reset --soft b49282b
查看暂存区 : 结果无变化 原因 :5.c 文件之后没有任何文件提交到本地库
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: 2.c
new file: 7.c
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: 1.c
Untracked files:
(use "git add <file>..." to include in what will be committed)
6.c
恢复到2的版本号 $ git reset --soft e98a5e7
查看暂存区
$ git reset --soft e98a5e7
User@DESKTOP-DGQLAAQ MINGW64 /d/ws/t3 (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: 2.c
new file: 3.c
new file: 4.c
new file: 5.c
new file: 7.c
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: 1.c
Untracked files:
(use "git add <file>..." to include in what will be committed)
6.c
可见 git reset --soft 版本号 执行后 将2.c 之后提交到本地库的 3,4,5 都移动到暂存区了
作用为 :
回退到指定版本,并将 这个版本提交之后的所有其他版本从本地库移动到暂存区
也就是清空本地库(版本库)撤销 commit 操作
工作区:修改了已被git跟踪的1.xt,不会重置它的文件内容。6.xt 是新文件,它的状态也不会改变,还是未跟踪状态
暂存区:暂存区中的2.bxt修改状态和7.txt的新文件状态保留
版本库:因为3.txt、4.txt、 5.txt 是之后当前版本才加入版本库的,git重置commit后自动将它们放到了暂存区
测试 git reset --mixed 版本号 ,进入 t 3
文章目录
进入一个新的文件夹
查看 状态 和 日志
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: 2.c
new file: 7.c
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: 1.c
Untracked files:
(use "git add <file>..." to include in what will be committed)
6.c
User@DESKTOP-DGQLAAQ MINGW64 /d/ws/t4 (master)
$ git reflog
b49282b (HEAD -> master) HEAD@{0}: commit: 55555555
5fa1fc5 HEAD@{1}: commit: 4444444
6977b98 HEAD@{2}: commit: 3333333
e98a5e7 HEAD@{3}: commit: 222222222
89870a3 HEAD@{4}: commit (initial): 11111
执行 git reset --soft b49282b 重置到5.c的状态
查看状态
$ git reset --mixed b49282b
Unstaged changes after reset:
M 1.c
M 2.c
User@DESKTOP-DGQLAAQ MINGW64 /d/ws/t4 (master)
$ 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: 1.c
modified: 2.c
Untracked files:
(use "git add <file>..." to include in what will be committed)
6.c
7.c
no changes added to commit (use "git add" and/or "git commit -a")
发现 暂存区中的(2,7.c 移动到了工作区 ) ,本地库的文件没有移动
执行$ git reset --mixed e98a5e7 重置到 2.c的状态
$ git reset --mixed e98a5e7
Unstaged changes after reset:
M 1.c
M 2.c
User@DESKTOP-DGQLAAQ MINGW64 /d/ws/t4 (master)
$ 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: 1.c
modified: 2.c
Untracked files:
(use "git add <file>..." to include in what will be committed)
3.c
4.c
5.c
6.c
7.c
no changes added to commit (use "git add" and/or "git commit -a")
发现 3,4,5.c 文件从本地库移动到了工作区
暂存区中的(2,7.c 移动到了工作区 )
执行 $ git reset --mixed 6977b98 重置到 3号状态
文章目录
$ git reset --mixed 6977b98
Unstaged changes after reset:
M 1.c
M 2.c
User@DESKTOP-DGQLAAQ MINGW64 /d/ws/t4 (master)
$ 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: 1.c
modified: 2.c
Untracked files:
(use "git add <file>..." to include in what will be committed)
4.c
5.c
6.c
7.c
no changes added to commit (use "git add" and/or "git commit -a")
发现只有4,5从本地库 移动到了 工作区
暂存区中的(2,7.c 移动到了工作区 )
总结:和git reset --soft一样 撤销的只是指定版本号之后的所有提交
但不仅会撤销版本区,还会撤销暂存区。
进入新文件 执行 git reset --hard版本号
查看状态,和日志
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: 2.c
new file: 7.c
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: 1.c
Untracked files:
(use "git add <file>..." to include in what will be committed)
6.c
User@DESKTOP-DGQLAAQ MINGW64 /d/ws/t5 (master)
$ git reflog
b49282b (HEAD -> master) HEAD@{0}: commit: 55555555
5fa1fc5 HEAD@{1}: commit: 4444444
6977b98 HEAD@{2}: commit: 3333333
e98a5e7 HEAD@{3}: commit: 222222222
89870a3 HEAD@{4}: commit (initial): 11111
执行 $ git reset --hard b49282b 重置 5.c状态
$ git reset --hard b49282b
HEAD is now at b49282b 55555555
User@DESKTOP-DGQLAAQ MINGW64 /d/ws/t5 (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
6.c
nothing added to commit but untracked files present (use "git add" to track)
发现本地中只有一个6.c文件(未被追踪)
继续执行
ll -a
$ ll
total 0
-rw-r--r-- 1 User 197121 0 Jun 13 21:22 1.c
-rw-r--r-- 1 User 197121 0 Jun 13 21:22 2.c
-rw-r--r-- 1 User 197121 0 Jun 13 18:43 3.c
-rw-r--r-- 1 User 197121 0 Jun 13 18:43 4.c
-rw-r--r-- 1 User 197121 0 Jun 13 18:43 5.c
-rw-r--r-- 1 User 197121 0 Jun 13 18:43 6.c
发现 暂存区中7.c被删除了
再进入一个新文件,这次将 版本reset到 2.c
$ git reset --hard e98a5e7
HEAD is now at e98a5e7 222222222
User@DESKTOP-DGQLAAQ MINGW64 /d/ws/t6 (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
6.c
nothing added to commit but untracked files present (use "git add" to track)
User@DESKTOP-DGQLAAQ MINGW64 /d/ws/t6 (master)
$ ll
total 0
-rw-r--r-- 1 User 197121 0 Jun 13 21:42 1.c
-rw-r--r-- 1 User 197121 0 Jun 13 21:42 2.c
-rw-r--r-- 1 User 197121 0 Jun 13 21:41 6.c
发现 暂存区中依旧只有未被追踪的 6.c (这里的 1.c 2.c 重置修改,放在本地库中)
而再执行 ll 查看文件
发现文件 3,4,5,7.c均被删除了。
总结 : 使用 git reset --hard 版本号 会将后续版本号跟踪的文件删除(例如3,4,5,7.c) . 同时会再 --mixed 的基础上 ,又重置 工作区文件副本的修改(也就是 1.c 2.c )
track)
User@DESKTOP-DGQLAAQ MINGW64 /d/ws/t6 (master)
$ ll
total 0
-rw-r–r-- 1 User 197121 0 Jun 13 21:42 1.c
-rw-r–r-- 1 User 197121 0 Jun 13 21:42 2.c
-rw-r–r-- 1 User 197121 0 Jun 13 21:41 6.c
发现 暂存区中依旧只有未被追踪的 6.c (这里的 1.c 2.c 重置修改,放在本地库中)
而再执行 ll 查看文件
发现文件 3,4,5,7.c均被删除了。
总结 : 使用 git reset --hard 版本号 会将后续版本号跟踪的文件删除(例如3,4,5,7.c) . 同时会再 --mixed 的基础上 ,又重置 工作区文件副本的修改(也就是 1.c 2.c )