Git reflog
Git, with its powerful version control features, enables developers to traverse through
project history effortlessly. The git reflog
command serves as a valuable tool
for navigating this history, allowing users to review and recover previous states, branches,
and commits. We'll delve into various ways of using the
git reflog
command, exploring its capabilities and offering insights into how
it can enhance your Git workflow.
1. Basic git reflog
Usage:
The fundamental use of git reflog
is to display a log of changes to local
references (e.g., branches and HEAD). Execute the following command to view the reflog:
bashgit reflog
This command provides a chronological list of reference changes, including commit SHAs, actions, and commit messages.
2. Viewing Reflog for a Specific Branch:
If you are interested in the reflog for a specific branch (e.g., main
), use:
bashgit reflog main
Replace main
with the name of the branch you want to inspect.
3. Displaying Short Reflog Entries:
For a more concise view, you can use the --pretty=short
option:
bashgit reflog --pretty=short
This displays shorter entries, showing only the commit SHA and the action.
4. Filtering Reflog Entries by Date:
To filter reflog entries based on date, you can use the --since
and
--until
options. For example, to see entries within the last 7 days:
bashgit reflog --since="7 days ago"
5. Displaying Reflog Entries for All Branches:
By default, git reflog
shows entries only for the current branch. To display
entries for all branches, use the --all
option:
bashgit reflog --all
6. Recovering Lost Commits:
If you accidentally reset a branch or lost commits, git reflog
can help you
recover them. Find the commit SHA in the reflog and reset the branch:
bashgit reset --hard <commit-SHA>
Replace <commit-SHA>
with the commit SHA you want to recover.
7. Undoing a Reset Operation:
If you mistakenly perform a git reset
and want to undo it, use the reflog to
find the previous state and reset back:
bashgit reflog git reset --hard HEAD@{n}
Replace n
with the index of the previous state in the reflog.
8. Restoring Deleted Branches:
If you accidentally delete a branch, git reflog
can help you recover it. Find
the commit SHA in the reflog and recreate the branch:
bashgit reflog git branch <branch-name> <commit-SHA>
Replace <branch-name>
with the name of the deleted branch and
<commit-SHA>
with the commit SHA.
9. Inspecting Changes to HEAD:
To focus on changes to the HEAD reference, you can use:
bashgit reflog HEAD
This provides a detailed history of changes to the HEAD reference.
10. Deleting Stale Branches:
If you have stale branches that are no longer needed, you can use git reflog
to
identify them:
bashgit reflog --all | grep 'refs/remotes' | awk '{print $NF}' | sort | uniq
This command lists remote branches that may no longer be relevant.
11. Using git show
with Reflog Entries:
To view the changes introduced by a specific reflog entry, use git show
with the
commit SHA:
bashgit show <commit-SHA>
Replace <commit-SHA>
with the commit SHA from the reflog.
12. Deleting Stale Reflog Entries:
If you want to clean up old or unnecessary reflog entries, you can use:
bashgit reflog expire --expire=now --all git gc --prune=now
This removes expired reflog entries and performs garbage collection.