Git & GitHub - Git Reflog
How to use git reflog to manage references
Git reflog is a powerful feature that records updates to the tip of branches and other references. This guide covers how to use git reflog to recover lost commits, undo changes, and manage your repository's references effectively.
Key Points:
- Git reflog records changes to the tip of branches and references, allowing you to recover lost commits.
- You can use reflog to undo changes and restore previous states of your repository.
- Reflog entries are stored locally and can be viewed, reset, and deleted as needed.
Viewing the Reflog
Listing Reflog Entries
Use the git reflog
command to view the history of changes to your references:
# List all reflog entries
$ git reflog
Understanding Reflog Entries
Each reflog entry includes the commit hash, reference name, and a message describing the action:
# Example reflog entry
abc1234 HEAD@{0}: commit: Fixed a bug
def5678 HEAD@{1}: checkout: moving from main to feature-branch
ghi9012 HEAD@{2}: commit (amend): Updated commit message
Recovering Lost Commits
Using Reflog to Find a Lost Commit
If you have lost a commit, you can use reflog to find and recover it:
# List reflog entries to find the lost commit
$ git reflog
# Checkout the lost commit using its hash
$ git checkout abc1234
Resetting to a Previous State
You can use reflog to reset your branch to a previous state:
# Reset the current branch to a previous state
$ git reset --hard HEAD@{2}
Undoing Changes
Undoing a Commit
If you want to undo a commit but keep the changes, use the git reset
command:
# Undo the last commit but keep the changes
$ git reset --soft HEAD~1
Reverting to a Specific Commit
You can use reflog to revert your branch to a specific commit:
# Revert the branch to a specific commit
$ git revert abc1234
Managing Reflog Entries
Deleting Specific Reflog Entries
To delete specific reflog entries, use the git reflog delete
command followed by the reference:
# Delete a specific reflog entry
$ git reflog delete HEAD@{1}
Expiring Old Reflog Entries
Git automatically expires reflog entries after 90 days. You can manually expire old entries using the git reflog expire
command:
# Expire reflog entries older than 30 days
$ git reflog expire --expire=30.days refs/heads/main
Cleaning Up Reflog Entries
To clean up reflog entries and optimize your repository, use the git gc
command:
# Run garbage collection to clean up reflog entries
$ git gc
Best Practices
Follow these best practices when using git reflog:
- Regularly Review Reflog: Regularly review your reflog entries to keep track of changes and recover lost commits if necessary.
- Clean Up Reflog Entries: Periodically clean up old reflog entries to optimize your repository.
- Use Reflog for Recovery: Utilize reflog to recover from mistakes and undo changes safely.
Summary
This guide covered how to use git reflog to manage references, including viewing reflog entries, recovering lost commits, undoing changes, managing reflog entries, and best practices. Git reflog is a powerful tool that helps you keep track of changes and manage your repository effectively.