Git

How to Use git reflog – reflog vs. log

Git is a version control system used for managing and tracking development projects from local repositories to remote repositories. When multiple developers work on similar projects at the local repository, then they update the changes and move to the remote repositories.

Sometimes other project members need to view the whole log history of the working repository branches. For the specified purpose, the “$ git log” and “$ git reflog” commands can be utilized.

This guide will illustrate how to use the git reflog and elaborate on the difference between the reflog and log.

What is git reflog Command?

git reflog” is a Git command used when developers are required to track the log history of the Git local commits and exclude any commits that were pruned away from the current local repository. It includes reverting action logs, creating branches, committing changes, and deleting and switching the history log.

Now, let’s move toward the next section to implement the “git reflog” command!

How to Use git reflog?

The “git reflog” command is mainly used when developers need to view the repository’s local commits history, including commit changes, created branches, reverting actions log, switching and removing branches history log, and many more.

Follow the below-provided steps!

Step 1: Open Git Terminal
Search and open the “Git Bash” using the “Startup” menu:

Step 2: Navigate to Git Repository
Move to the Git local repository using the provided command:

$ cd "C:\Users\nazma\Git\demo2"

Step 3: Create New File
Next, execute the “touch” command to create a new file in the Git repository:

$ touch file1.txt

Step 4: Add File
Now, track the newly created file to the staging area by using the “git add” command:

$ git add file1.txt

Step 5: Commit Changes
After that, execute the “git commit” command with the “-m” option to commit changes to the Git repository:

$ git commit -m "file1.txt is added"

Step 6: Check Reference Log History
Finally, check the Git local commit history log of the Git repository using the provided command:

$ git reflog

The whole log history is displayed on the terminal, and the highlighted log refers to the recently committed changes:

Now, move toward the next section to view the difference between reflog and log commands.

Git reflog vs. log

The Git “reflog” command is utilized when users directly track the log history of the current working repository branch of the local commits and remove any commits that were pruned away from the repository. This command’s output will display the reverting action logs, creating new branches, committing changes, and removing and switching the history log. In contrast, The “git log” command offers the repository branch’s commit traversal history, including remote repository name, time, and provided credentials.

Conclusion

To use git reflog, first, navigate to the Git local repository and check the log history using the “$ git reflog” command. The “reflog” command is used to view the reverting action logs, create new branches, commit changes, and remove and switch the history log. In contrast, “log” commands show all of the commit changes at once. This guide illustrated how to use the git reflog and discussed the reflog vs. log.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.