WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Git · Intermediate · question 27 of 100

How do you use ’git reflog’, and when would you need it?

📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

In Git, the git reflog command is used to view a log of changes to the repository’s references, including the branch and HEAD pointers. The git reflog command is useful for recovering lost commits or branches, as well as for troubleshooting issues with the repository’s history.

Here’s how to use git reflog:

Open a Git repository in your terminal and run the following command:

    # View the Git reflog
    git reflog

This will display a list of the recent changes to the repository’s references, including the branch and HEAD pointers.

Identify the commit or branch that you want to recover. The git reflog output will include the commit hashes for each change, as well as the branch or HEAD pointer associated with the change.

Use the git checkout command to checkout the desired commit or branch.

    # Checkout a commit or branch
    git checkout <commit-hash or branch-name>

Alternatively, you can use the git reset command to reset the current branch to the desired commit or branch.

    # Reset the current branch to a commit or branch
    git reset --hard <commit-hash or branch-name>

Once you have recovered the desired commit or branch, you can continue working with the repository as usual.

The git reflog command is especially useful in situations where you have accidentally deleted a branch or committed changes that you did not intend to. By reviewing the git reflog output, you can identify the lost branch or commit and recover it easily.

In summary, the git reflog command is used to view a log of changes to the repository’s references, including the branch and HEAD pointers. It can be useful for recovering lost commits or branches, as well as for troubleshooting issues with the repository’s history.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Git interview — then scores it.
📞 Practice Git — free 15 min
📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

All 100 Git questions · All topics