my-notes

git_reflog

1 min read

git reflog

Safety net for recovering lost work.


πŸ“Š What is Reflog?

Reflog tracks every HEAD movement:

  • Commits
  • Checkouts
  • Resets
  • Rebases
  • Merges
graph LR
    A[Every HEAD change] --> B[Recorded in reflog]
    B --> C[Can recover lost commits]

πŸ“‹ View Reflog

Show Reflog

git reflog

Shows HEAD history.

Output:

abc1234 HEAD@{0}: commit: Message
def5678 HEAD@{1}: checkout: moving from main to feature
ghi9012 HEAD@{2}: commit: Previous message

Detailed View

git reflog show HEAD

Same as git reflog.


Branch Reflog

git reflog show main

Shows history for specific branch.


With Dates

git reflog --date=relative

Shows relative dates.


With Full Dates

git reflog --date=iso

Shows ISO format dates.


πŸ”„ Recover Lost Commits

After Hard Reset

git reset --hard HEAD~3
# Oops! Need those commits back!

Find Lost Commit

git reflog

Find the hash of lost commit.


Recover Commit

git reset --hard abc1234

Reset to the lost commit.


Create Branch from Lost Commit

git branch recovered abc1234

Creates branch at lost commit.


🌿 Recover Deleted Branch

After Deleting Branch

git branch -D feature-branch
# Need it back!

Find Last Commit

git reflog | grep feature-branch

Find where branch was.


Recreate Branch

git branch feature-branch abc1234

Recreates branch at that commit.


βͺ Recover from Bad Rebase

After Rebase Gone Wrong

git rebase main
# Made a mess!

Find Pre-rebase State

git reflog

Look for "rebase: starting" or "checkout".


Reset to Pre-rebase

git reset --hard HEAD@{5}

Returns to state 5 moves ago.


🧹 Manage Reflog

Expire Old Entries

git reflog expire --expire=90.days.ago --all

Removes entries older than 90 days.


Expire All Now

git reflog expire --expire=now --all

Removes all reflog entries.


Delete Specific Entry

git reflog delete HEAD@{2}

Deletes specific entry.


⏰ Reference by Time

By Relative Time

git show HEAD@{1.hour.ago}

Shows HEAD from 1 hour ago.


By Date

git show main@{2024-01-15}

Shows main branch on specific date.


By Yesterday

git diff main@{yesterday} main

Changes since yesterday.


πŸ’‘ Tips

Reflog is Local

Reflog only exists on your machine. Not shared with remote.

Default Retention

Entries expire after 90 days (30 for unreachable).

After gc

After git gc, unreachable commits may be deleted.



#git #reflog #recovery #advanced

Built with mdgarden