美文网首页
git 进阶。stash和reflog使用,可恢复误删除、res

git 进阶。stash和reflog使用,可恢复误删除、res

作者: weiweilong | 来源:发表于2019-12-06 15:36 被阅读0次

stash: 储藏。

储藏机制典型情况---“中断工作流”。
使用场景:
1、在当前开发做修改后,不需要进行commit(可能是未开发完,有优先级更高的任务或bug),这时本地修改的代码就可以使用stash储藏。(也这种情况也可以使用基于当前修改新建分支解决)。
2、多个分支开发时,需要临时切换至其他分支,但是当前开发分支不做commit,可以使用stash储藏。

常用命令:
1、git stash save "描述" :保存当前修改至stash
save是git stash的默认语句,输入git stash也是默认执行git stash save。
2、git stash pop:取出最新的stash内容。
3、git stash list:查看所有stash列表
列表的形式是这样的:

stash@{0}: WIP on master: 65f0fe3 test commit

有多个stash 就会有多条。其中stash@{0}是用来取某一条stash的标识
4、git stash apply stash@{0}:取出某条stash中的内容
5、git stash show -p stash@{0}:查看某个stash中详细的修改信息

reflog引用日志

使用场景:在误删或者进行reset的误操作后,需要将代码恢复。可根据引用日志操作恢复。
引用日志会记录以下情况:

  • 复制
  • 推送
  • 执行新提交
  • 修改或创建分支
  • 重置操作
    一些更复杂的操作,比如 git filter-branch 都可以归结到简单的提交上,也会记录下来。根本上来说,任何修改引用或者更改分支头的git操作都会记录。

常用命令:
1、git reflog show :显示引用日志。
引用日志是这样的:

5b488e0 HEAD@{5}: commit: test 3
2dd0082 HEAD@{6}: checkout: moving from test to master
65ca119 HEAD@{7}: checkout: moving from master to test
2dd0082 HEAD@{8}: reset: moving to HEAD

HEAD@{0} 条目从0开始,我上面截取的是中间的一部分引用日志。
HEAD@{0}条目可作为log查看和reset的标识。可以针对某一 HEAD@{0} 条目进行 git log HEAD@{0} 或 git reset HEAD@{0} 操作。
2、git reflog 分支名:显示某个分支的引用日志
3、git log HEAD@{0}:查看此条目和此条目之前的提交日志。
4、git reset HEAD@{0}:针对某一条目进行reset 命令,reset 后可接不同的命令达到不通的效果,比如--soft --mixed --hard,具体可查看之前文章对reset的使用当误删或者进行reset的误操作后,可用此方法恢复。

模拟场景:
假如我们在执行 “git reset --hard 某次提交标识” 后(执行reset --hard会将本地的修改直接重置),发现将不需要重置的代码也重置了,再使用git log 查看提交日志时,发现之前的提交日志已经没了,回到了reset 的那次提交。这时就可以使用reflog 引用日志进行恢复。

具体的操作方法:
用以下引用日志说明

1、我先进行了三次测试的提交。提交完后使用 git reflog show 查看引用日志。
952108e (HEAD -> master) HEAD@{0}: commit: test Commit 03
5d0dd2d HEAD@{1}: commit: test commit 02
d5e842c HEAD@{2}: commit: test commit

使用 git log 查看日志

commit 952108e52816f07002b102e6b49b1de4bd922c68 (HEAD -> master)
Author: weiweilong <weiweilonggl@gmail.com>
Date:   Fri Dec 6 14:38:21 2019 +0800

    test Commit 03
    
    test Commit 03

commit 5d0dd2de10abfea78679763d0a8aac755eafa7b4
Author: weiweilong <weiweilonggl@gmail.com>
Date:   Fri Dec 6 14:37:23 2019 +0800

    test commit 02
    
    test commit 02

commit d5e842cb35285a7e27a1d08c2388b46c13cf54ff
Author: weiweilong <weiweilonggl@gmail.com>
Date:   Fri Dec 6 14:35:24 2019 +0800

    test commit
    
    test commit

在git log 和git reflog show 中都有提交日志。

2、使用命令 git reset --hard d5e842cb35285a7e27a1d08c2388b46c13cf54ff 重置到“test commit”这次提交。对应命令行命令:
➜  ztyf_min git:(master) git reflog show
➜  ztyf_min git:(master) git log
➜  ztyf_min git:(master) git reset --hard d5e842cb35285a7e27a1d08c2388b46c13cf54ff
HEAD is now at d5e842c test commit
➜  ztyf_min git:(master) 

再使用git reflog show 和 git log查看日志

d5e842c (HEAD -> master) HEAD@{0}: reset: moving to d5e842cb35285a7e27a1d08c2388b46c13cf54ff
952108e HEAD@{1}: commit: test Commit 03
5d0dd2d HEAD@{2}: commit: test commit 02
d5e842c (HEAD -> master) HEAD@{3}: commit: test commit

git log

commit d5e842cb35285a7e27a1d08c2388b46c13cf54ff (HEAD -> master)
Author: weiweilong <weiweilonggl@gmail.com>
Date:   Fri Dec 6 14:35:24 2019 +0800

    test commit
    
    test commit

可以看到log中只有“test commit”的提交记录了,但是reflog中所有的操作都被记录了。假如需要恢复到这次reset前的内容,执行git reset HEAD@{1}即可

3、执行:git reset HEAD@{1}

执行完再使用git reflog show 和 git log查看日志

952108e (HEAD -> master) HEAD@{0}: reset: moving to HEAD@{1}
d5e842c HEAD@{1}: reset: moving to d5e842cb35285a7e27a1d08c2388b46c13cf54ff
952108e (HEAD -> master) HEAD@{2}: commit: test Commit 03
5d0dd2d HEAD@{3}: commit: test commit 02
d5e842c HEAD@{4}: commit: test commit

git log

commit 952108e52816f07002b102e6b49b1de4bd922c68 (HEAD -> master)
Author: weiweilong <weiweilonggl@gmail.com>
Date:   Fri Dec 6 14:38:21 2019 +0800

    test Commit 03
    
    test Commit 03

commit 5d0dd2de10abfea78679763d0a8aac755eafa7b4
Author: weiweilong <weiweilonggl@gmail.com>
Date:   Fri Dec 6 14:37:23 2019 +0800

    test commit 02
    
    test commit 02

commit d5e842cb35285a7e27a1d08c2388b46c13cf54ff
Author: weiweilong <weiweilonggl@gmail.com>
Date:   Fri Dec 6 14:35:24 2019 +0800

    test commit
    
    test commit

到此可以看到原来reset的log已经回来了。但是对应本地的文件还并没有修改回来。
使用git status查看状态。

➜  ztyf_min git:(master) ✗ git status
On branch master
Your branch is ahead of 'origin/master' by 7 commits.
  (use "git push" to publish your local commits)

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:   pages/Home/MyCar/MyCar.js

no changes added to commit (use "git add" and/or "git commit -a")

可以看到pages/Home/MyCar/MyCar.js 文件就是我们需要恢复的文件,对文件进行checkout 就可以把文件修改恢复到提交前的状态了。
一般在项目中使用时都是借用工具进行discard changes。

其实恢复文件git提供了一个git fsck命令,fsck(file system check)文件系统检测。但是一般情况用不到,只有在误删除并且把引用日志也删除的情况才用到git fsck。本文就不做解释了。

相关文章

  • git 进阶。stash和reflog使用,可恢复误删除、res

    stash: 储藏。 储藏机制典型情况---“中断工作流”。使用场景:1、在当前开发做修改后,不需要进行commi...

  • 删除git stash后如何恢复

    恢复误删除的git stash记录 git fsck --unreachable(找删除后的悬空对象)1.首先使用...

  • git

    配置 git stash 分支 git log查看日志 git reflog查看commits push后撤销pu...

  • Git stash 技巧

    Git stash常用技巧 git stash save git stash list git stash app...

  • stash 操作暂存区

    涉及命令:git stash、stash list、git stash apply、git stash drop ...

  • git进阶 之 reflog

    示例 提交 git三大对象类型: 数据对象(Blob Object) / 树对象(Tree Object) / 提...

  • git常用命令

    git log git reflog git reset git log git reflog git check...

  • git stash

    git stashgit stash pop(最新的暂存) 如果不想使用最新的暂存,可以 git stash li...

  • git 找回丢失的commit

    今天是rebase变基时不小心把提交弄没了使用git reflog找回了。具体方法。使用git reflog列出最...

  • Git

    git stash和git stash pop git stash 可用来暂存当前正在进行的工作, 比如想pull...

网友评论

      本文标题:git 进阶。stash和reflog使用,可恢复误删除、res

      本文链接:https://www.haomeiwen.com/subject/logsgctx.html