Git - reflog czyli usunięte przez 14 dni trzymane

Autor podstrony: Krzysztof Zajączkowski

Stronę tą wyświetlono już: 461 razy

Zdarza się czasami, że usunięte rzeczy jednak są potrzebne. Nie ma jednak co rozpaczać pod warunkiem, że od dnia usunięcia nie minęło więcej niż 14 dni. Tak się bowiem składa, że git trzyma te informacje przez ten magiczny okres czasu. Polcenie reflog wyświetla listę wprowadzonych zmian (również tych związanych z "trwałym" usunięciem). W celu przećwiczenia takiego scenariusza utworzę nowy plik, dodam go i commit-uję:

 add "new file.txt"
git commit -m "add new commit to reset hard"
[detached HEAD 006f556] add new commit to reset hard
 1 file changed, 0 insertions(+), 0 deletions(-)    
 create mode 100644 new file.txt

tylko po to aby w następnej chwili go usunąć:

git reset --hard HEAD~1
HEAD is now at 9d2feaf add style for ul elements

Teraz spójrzmy co reflog przyniesie:

git reflog
9d2feaf (HEAD, 005_add_combining_branches_operations_types) HEAD@{0}: reset: moving to HEAD~1
006f556 HEAD@{1}: commit: add new commit to reset hard
9d2feaf (HEAD, 005_add_combining_branches_operations_types) HEAD@{2}: checkout: moving from 005_add_combining_branches_operations_types to 9d2feaf
9d2feaf (HEAD, 005_add_combining_branches_operations_types) HEAD@{3}: checkout: moving from 001_index_html to 005_add_combining_branches_operations_types
9d2feaf (HEAD, 005_add_combining_branches_operations_types) HEAD@{4}: checkout: moving from 9d2feafc30f5935b228c98291acaecc1459337f2 to 001_index_html
9d2feaf (HEAD, 005_add_combining_branches_operations_types) HEAD@{5}: checkout: moving from 005_add_combining_branches_operations_types to 9d2feaf
9d2feaf (HEAD, 005_add_combining_branches_operations_types) HEAD@{6}: Branch: renamed refs/heads/005_add_merging_operations_types to refs/heads/005_add_combining_branches_operations_types
9d2feaf (HEAD, 005_add_combining_branches_operations_types) HEAD@{8}: reset: moving to HEAD~1
c06f41d HEAD@{9}: commit: new todo things
9d2feaf (HEAD, 005_add_combining_branches_operations_types) HEAD@{10}: reset: moving to HEAD~1
cef9f37 HEAD@{11}: commit: new todo things
9d2feaf (HEAD, 005_add_combining_branches_operations_types) HEAD@{12}: reset: moving to HEAD~1
8ecb7c1 HEAD@{13}: commit: new todo things
9d2feaf (HEAD, 005_add_combining_branches_operations_types) HEAD@{14}: reset: moving to HEAD~1

a więc jednak jest światełko w tunelu! Oby nie okazało się nadjeżdżającym pociągiem! Dobra, to może czas odzyskać utracony drogocenny commit wykorzystując hasha usuniętego commit-a:

git reset --hard 006f556
HEAD is now at 006f556 add new commit to reset hard

To było prostsze aniżeli odebranie dzieciakowi lizaka (nie żebym kiedykolwiek próbował).

Propozycje książek