原帖收藏於IT老兵驛站,傳遞一個IT老兵在凋零前的光和氧。
git-reflog的用法總結。
前言
git-reflog是用來恢復本地錯誤操作很重要的一個命令,所以在這里對它進行一下整理。
正文
概要
管理reflog信息。
語法
git reflog <subcommand> <options>
具體的用法
git reflog [show] [log-options] [<ref>]
git reflog expire [--expire=<time>] [--expire-unreachable=<time>] [--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] [--verbose] [--all | <refs>…]
git reflog delete [--rewrite] [--updateref] [--dry-run | -n] [--verbose] ref@{specifier}… git reflog exists <ref>
Reference logs, or “reflogs”, record when the tips of branches and other references were updated in the local repository.
這句話怎么理解呢,記錄了“when”,時間?
翻譯:Reference logs(參考日志),或者叫做”reflogs”,記錄了分支的tips(提示信息?)或者其他參考在本地倉庫被更新的時間(when)。
問題來了,這個參考日志的作用是什么,和日志又有什么區別呢?
找到了這篇帖子:
git log shows the current HEAD and its ancestry. That is, it prints the commit HEAD points to, then its parent, its parent, and so on. It traverses back through the repo's ancestry, by recursively looking up each commit's parent.
(In practice, some commits have more than one parent. To see a more representative log, use a command like git log --oneline --graph --decorate.)
git reflog doesn't traverse HEAD's ancestry at all. The reflog is an ordered list of the commits that HEAD has pointed to: it's undo history for your repo. The reflog isn't part of the repo itself (it's stored separately to the commits themselves) and isn't included in pushes, fetches or clones; it's purely local.
Aside: understanding the reflog means you can't really lose data from your repo once it's been committed. If you accidentally reset to an older commit, or rebase wrongly, or any other operation that visually "removes" commits, you can use the reflog to see where you were before and git reset --hard back to that ref to restore your previous state. Remember, refs imply not just the commit but the entire history behind it.
上面就講的比較清楚了,總結一下:
git log
是顯示當前的HEAD
和它的祖先的,遞歸是沿着當前指針的父親,父親的父親,……,這樣的原則。
git reflog
根本不遍歷HEAD
的祖先。它是HEAD
所指向的一個順序的提交列表:它的undo
歷史。reflog
並不是repo
(倉庫)的一部分,它單獨存儲,而且不包含在pushes
,fetches
或者clones
里面,它純屬是本地的。
reflog
可以很好地幫助你恢復你誤操作的數據,例如你錯誤地reset
了一個舊的提交,或者rebase
,……,這個時候你可以使用reflog
去查看在誤操作之前的信息,並且使用git reset --hard
去恢復之前的狀態。
下面研究一下這個命令的具體用法。
先了解一下git
的版本表示方法:
HEAD@{2} means “where HEAD used to be two moves ago”, master@{one.week.ago}means “where master used to point to one week ago in this local repository”
HEAD@{2}
表示HEAD
指針在兩次移動之前的情況;而 master@{one.week.ago}
表示master
在本地倉庫一周之前的情況。
“show”子命令顯示所指定的參考的日志。
實例: 顯示HEAD
的reflog
。
$ git reflog show
ef64f10 (HEAD -> BlueLake_theme) HEAD@{0}: commit: 新增ethereum-programming-intr oduction
122e0ec (origin/BlueLake_theme) HEAD@{1}: commit: 移除了冗余的ethereum-rationale 文章
c17fbbb HEAD@{2}: commit: 新增git-change-server-password文章
1603d1a HEAD@{3}: pull: Merge made by the 'recursive' strategy.
0ce1e93 HEAD@{4}: commit: 新增了以太坊原理
c73503c HEAD@{5}: commit: 修改了-X-Frame-Options的關鍵字
6af02f6 HEAD@{6}: commit: 新增了git-tag的文章;修改了git其他的文章,規范了名字、 關鍵字
9087fbd HEAD@{7}: commit: 新增了gti-reset文章
039d95c HEAD@{8}: commit: 移除了沒用的目錄
ff72601 HEAD@{9}: commit: 修改成了next主題
ef64f10 (HEAD -> BlueLake_theme) HEAD@{0}: commit: 新增ethereum-programming-intr oduction
從上圖可以看到,幾乎所有的操作都記錄在其中,這個就像MySQL,隨時可以回滾。
“expire”子命令會刪除掉更老的reflog條目。
“delete”子命令從reflog中刪除一個條目。
“exists”子命令檢查一個ref是否有一個reflog。
這幾個命令就相對比較簡單了,以后再嘗試了。