Git reflog

Advanced Git

Amanda Crawford-Adamo

Software and Data Engineer

What is Git Reflog?

  1. Local record of ALL reference updates in our repository
  2. Reflog is stored in on our local system under the .git/logs/refs/heads/ directory
  3. Records changes to branch tips and HEAD position
  4. Acts a safeguard for our Git operations
  5. Helps recover accidental changes or deletions

An example graph of git history changes

Advanced Git

Git Reflog versus Git Log

Feature Git Reflog Git Log
Purpose Shows reference updates and rewrites in local repo Shows commit history only
Scope Local repository only Local and remote repositories
Content All ref updates (commits, resets, merges, etc.) Only commits
Persistence Temporary (usually 90 days) Permanent (part of repository history)
Use Case Recovering lost commits, understanding recent actions Viewing project history, understanding feature development
Advanced Git

Reflog Commands

  • Displays the log data and HEAD activity
    git reflog
    git reflog show
    
  • Clean up old log or unreachable entries
    git reflog expire
    
Advanced Git

Reflog Output Structure

Breakdown of the reflog output text structure

Example of reflog output in terminal

Component Description
short-hash Abbreviated commit hash
ref Usually HEAD, but can be branch names
index Position in the reflog (0 is the most recent)
action Type of action (commit, reset, merge, etc)
descriptions Description about the action
1 https://hackernoon.com/time-to-rewrite-your-git-history-effectively-with-git-reflog
Advanced Git

Filtering: Since and Until

  • since = show entries from this point in time

    git reflog --since "time-qualifer"
    
  • until = show entries up to this point in time

    git reflog --until "time-qualifer"
    
Usage
git reflog --since="1 week ago"
git reflog --until="yesterday"
git reflog --until="2024-01-01"
Advanced Git

Recovering Deleted Branches

Scenario

  • Created a branch called etl-feature
  • Committed ETL feature changes to this branch
  • We accidentally deleted this branch
  • All code changes were lost

How can we restore the etl-feature branch?

Solution

  1. Identify the hash of the commit at the tip of deleted branch using git reflog
  2. Use git checkout to move HEAD to the commit hash of the deleted branch
  3. Create a new branch using the commit HEAD is currently pointed
git reflog
git checkout <hash>
git checkout -b <branch-name>
1 https://stackoverflow.com/questions/3640764/can-i-recover-a-branch-after-its-deletion-in-git
Advanced Git

Git Reset

  • Moves the HEAD to the specific commit object.
  • Depending on the reset type, the working and staging area is updated.
Reset Type Command Effect on Working Directory Effect on Staging Area
Soft git reset --soft <commit> No changes Changes remain staged
Mixed (Default) git reset --mixed <commit> No changes Changes are unstaged
Hard git reset --hard <commit> Changes are discarded Changes are discarded
Advanced Git

Finding A Loss Commit

Scenario

  • Tests started failing after commits and rebases
  • Unknown commit caused the failure
  • Need to revert to a passing state

How do we revert back to the previous ETL script changes?

Solution

  1. Find the deleted commit hash using Git Reflog
  2. Use git reset to revert to the commit with the passing tests
git reflog
git reset --soft HEAD@{1}
Advanced Git

Best Practices

  • Powerful for recovering lost code and commits
  • Use descriptive commit messages
  • Push to remote regularly
  • Be cautious with force pushes

Reflog is our local time machine when we make mistakes

An image of a clock that goes back in time

Advanced Git

Let's practice!

Advanced Git

Preparing Video For Download...