git reset 回退commit提交的版本
-先模拟提交过程,一共提交四次,每次都向文本里写一个新数
# 向文本中写入0 $ echo 0 >a.t $ git add . #第一次提交到仓库 $ git commit -m '0' # 向文本中写入1 $ echo 1 >a.t $ git add .; #第二次提交到仓库 $ git commit -m '1' # 向文本中写入2 $ echo 2 >a.t $ git add . #第三次提交到仓库 $ git commit -m '2' # 向文本中写入3 $ echo 3 >a.t $ git add . #第四次提交到仓库 $ git commit -m '3' # 显示提交过日志 $ git log
 
上图,(HEAD -> master) 代表当前所在提交版本的位置
查看文本内容:

HEAD 说明:
HEAD 表示当前版本
HEAD^ 上一个版本
HEAD^^ 上上一个版本
HEAD^^^ 上上上一个版本
以此类推...
可以使用 ~数字表示
HEAD~0 表示当前版本
HEAD~1 上一个版本
HEAD^2 上上一个版本
HEAD^3 上上上一个版本
以此类推...
------------------------------------------- 分割线 --------------------------------------------------------------
测试:每次回退一个版本
第一次执行 reset 回退
ubuntu@ubuntuE470:~/test$ git reset HEAD^ 重置后取消暂存的变更: M a.t ubuntu@ubuntuE470:~/test$ cat a.t 3 ubuntu@ubuntuE470:~/test$ git log
 -
第二次执行 reset 回退
ubuntu@ubuntuE470:~/test$ git reset HEAD^ 重置后取消暂存的变更: M a.t ubuntu@ubuntuE470:~/test$ cat a.t 3 ubuntu@ubuntuE470:~/test$ git log
 -
第三次执行 reset 回退
ubuntu@ubuntuE470:~/test$ git reset HEAD^ 重置后取消暂存的变更: M a.t ubuntu@ubuntuE470:~/test$ cat a.t 3 ubuntu@ubuntuE470:~/test$ git log
 -
总结:上面是每次回退一个版本,版本每次都在回退,但是内容一直是最后一次 commit 的 3
如果使用 --hard 参数,内容也会跟着变化。
------------------------------------------- 分割线 --------------------------------------------------------------
测试: 直接回退到某次提交,用 --hard参数 覆盖当前内容(把 add 也撤销了)
$ git reset --hard d6cbefe0da132 HEAD 现在位于 d6cbefe 1 $ cat a.t 1 $ git log
 -
注意:用 --hard 后 a.t 文本内容变成1 而原来的内容3被隐藏。彻底回退到某个版本,本地的源码也会变为上一个版本的内容
如果还想找回 3 的内容,那就再输入 3 对应的 commit id 即可找回 内容3 对应的ID: c19972
------------------------------------------- 分割线 --------------------------------------------------------------
测试: 直接回退到某次提交,用 --soft 只回退 commit 内容,不回退文本内容
当前仓库状态

直接回退到指定 commit id :
$ cat a.t 3 $ git reset --soft f796337abab2 $ cat a.t 3
 -
git reset --soft f796337abab2 和 git reset f796337abab2 效果是一样的
回退到某个版本,只回退了commit的信息,不会恢复到index file一级。
如果还要提交,直接commit即可。
$ echo 4 >a.t $ git add . $ git commit -m '4' [master a8016c2] 4 1 file changed, 1 insertion(+), 1 deletion(-) $ git log
 -
-
参考:
https://www.runoob.com/git/git-reset.html
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号