{"id":1598,"date":"2022-01-21T11:14:09","date_gmt":"2022-01-21T10:14:09","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1598"},"modified":"2022-01-21T11:14:09","modified_gmt":"2022-01-21T10:14:09","slug":"git-reflog-vs-log-differences","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2022\/01\/21\/git-reflog-vs-log-differences\/","title":{"rendered":"Git reflog vs. log differences"},"content":{"rendered":"\n<p>The biggest difference between Git reflog vs. Git log is that the<em> <strong>log<\/strong><\/em> is a public accounting of the repository&#8217;s commit history while the <strong><em>reflog<\/em><\/strong> is a <span class=\"has-inline-color has-vivid-red-color\">private<\/span>, workspace-specific accounting of the repo&#8217;s local commits.<\/p>\n\n\n\n<p>The Git log is part of the Git repository and is replicated after a push, fetch or pull. In contrast, the Git reflog is not part of the replicated repo. A developer can&#8217;t examine a local repository&#8217;s reflog without having physical access to the computer where it is located.<\/p>\n\n\n\n<p><em><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Use the\u00a0<\/strong><\/span><strong><span class=\"has-inline-color has-vivid-red-color\">git log<\/span><\/strong><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\u00a0command to view the log and use the\u00a0<\/strong><\/span><strong><span class=\"has-inline-color has-vivid-red-color\">git reflog\u00a0<\/span><\/strong><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>command to view the reflog (<\/strong><\/span><strong><span class=\"has-inline-color has-black-color\">It means only your personal commit<\/span><\/strong><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>).<\/strong><\/span><\/em><\/p>\n\n\n\n<p>In more technical terms, the reflog is a file found in\u00a0<em><strong>.git\\logs\\refs\\heads<\/strong><\/em>\u00a0that tracks the history of local commits for a given branch and excludes any commits that were potentially pruned away through Git Garbage collection routines. In contrast, the Git log provides a branch&#8217;s historical traversal of commits that starts from the most recent commit and ends at the very first commit of the entire branch history.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-red-color\">Git reflog vs. log similarities<\/span><\/h4>\n\n\n\n<p>One of the reasons for the Git reflog vs. log confusion is the fact that the two components will often show an identical history, especially when a developer completes a number of local commits without a fetch or a pull.<\/p>\n\n\n\n<p>Take a look at the following example, which starts by\u00a0<em><strong>creating a Git repository<\/strong><\/em>\u00a0and then performs three commits. Notice how the\u00a0git reflog\u00a0and\u00a0log\u00a0commands print out the exact same information:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git init\r\nInitialized empty Git repository in C:\/workspaces\/temp\/.git\/\r\n\r\n$ touch home.html\r\n$ git add .\r\n$ git commit -m \"First commit\"\r\n\r\n$ echo \"Small Change\" >> home.html\r\n$ git commit -a -m \"Second commit\"\r\n\r\n$ echo \"Another Change\" >> home.html\r\n$ git commit -a -m \"Third commit\"<\/code><\/pre>\n\n\n\n<p>Let\u00b4s examine now both the log and reflog, you&#8217;ll notice how remarkably similar they look as they report the exact same commit history:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git log --pretty=oneline\r\n7abb321 (HEAD -> master) Third commit\r\ne1051e5 Second commit\r\n95d919c First commit\r\n\r\n$ git reflog\r\n7abb321 (HEAD -> master) HEAD@{0}: commit: Third commit\r\ne1051e5 HEAD@{1}: commit: Second commit\r\n95d919c HEAD@{2}: commit (initial): First commit<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-red-color\">How the Git reflog and log differ<\/span><\/h4>\n\n\n\n<p>Based on the similar output above, it&#8217;s possible to conclude incorrectly that the Git log and reflog are essentially the same. To demonstrate how the two can differ, see what happens after a developer\u00a0<strong>hard resets<\/strong>, or changes the last Git commit message.<\/p>\n\n\n\n<p>Here we change the last Git commit message:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git commit --amend -m \"3rd commit message\"<\/code><\/pre>\n\n\n\n<p>After a developer issues the command, reflog&#8217;s\u00a0<em>private<\/em>\u00a0listing of local commits reveals four entries. Every local commit issued against the repository is included in the output of the\u00a0git reflog\u00a0command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git reflog\r\n88846c2 (HEAD -> master) HEAD@{0}: commit (amend): 3rd commit message\r\n7abb321 HEAD@{1}: commit: Third commit\r\ne1051e5 HEAD@{2}: commit: Second commit\r\n95d919c HEAD@{3}: commit (initial): First commit<\/code><\/pre>\n\n\n\n<p>However, the\u00a0git log\u00a0command, which shows a\u00a0<em>public<\/em>\u00a0view of the repository, doesn&#8217;t reveal any information about the\u00a0<strong>amended commit message<\/strong>. In this situation, we can now see how the Git log and reflog differ:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git log --pretty=oneline\r\n88846c (HEAD -> master) 3rd commit message\r\ne1051e Second commit\r\n95d919 First commit<\/code><\/pre>\n\n\n\n<p>A reset example provides an even more pronounced Git relog vs. log difference<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git reset --hard 95d919c<\/code><\/pre>\n\n\n\n<p>After a hard reset takes the repository back to the first commit, the reflog will show the history of all five commits. The log will show only one and won&#8217;t have any reference to the amend, the reset or even the second and third commits. Here is the output of the\u00a0<em>git reflog<\/em>\u00a0command after the reset:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git reflog\r\n95d919c (HEAD -> master) HEAD@{0}: reset: moving to 95d919c\r\n88846c2 HEAD@{1}: commit (amend): 3rd commit message\r\n7abb321 HEAD@{2}: commit: Third commit\r\ne1051e5 HEAD@{3}: commit: Second commit\r\n95d919c (HEAD -> master) HEAD@{4}: commit (initial): First commit<\/code><\/pre>\n\n\n\n<p>In contrast, the <em>Git log<\/em> only shows one commit on the master branch:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git log --pretty=oneline\r\n95d919c (HEAD -> master) First commit<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-red-color\">Clones, logs and reflogs<\/span><\/h4>\n\n\n\n<p>After a\u00a0repository is cloned , the log will output a full history of the repository, while the reflog mentions nothing other than the clone ( that\u00b4s normal, because reflog only mention the action\/change we made localy). Look at the following Git log and reflog example after a developer has cloned a GitHub repository:<\/p>\n\n\n\n<p>After the clone, the reflog is quite sparse:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git reflog\r\n46e70ce (HEAD -> master, origin\/master, origin\/HEAD) HEAD@{0}: clone<\/code><\/pre>\n\n\n\n<p>However, the\u00a0git log\u00a0command will show the entire history of the <strong>cloned Github\u00a0repository&#8217;s commits<\/strong>, which can span years and include hundreds of entries.<\/p>\n\n\n\n<p>example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git log --pretty=oneline\r\n46e70c (HEAD -> master, origin\/master, origin\/HEAD) Add Spring Configuration Example Tutorial\r\nad128a Update pom.xml\r\na04781 Update pom.xml\r\nd816ba Update pom.xml\r\nfb73eb Delete DemoApplicationTests.java\r\ne14004 Create game.html\r\n66a9e7 more boot\r\n38a86d Merge branch 'master'\r\n17c3ca Added Score class with static variables\r\nd08cd7 Update WebController.java\r\nca01b6 added spring-ioc-example\r\n0bc3b9 adding a few\r\nf395f5 Update .gitignore \n.\n.\n.<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>PS: we can also view the <em>reflog<\/em> via the\u00a0<em><strong>git log<\/strong><\/em>\u00a0command with the\u00a0<strong><em>&#8211;walk-reflogs<\/em><\/strong>\u00a0options parameter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git log --pretty=oneline --walk-reflogs master\r\n95d919c (HEAD -> master) master@{0}: reset: moving to 95d919c\r\n88846c2 master@{1}: commit (amend): 3rd commit message\r\n7abb321 master@{2}: commit: Third commit\r\ne1051e5 master@{3}: commit: Second commit\r\n95d919c (HEAD -> master) master@{4}: commit (initial): First commit<\/code><\/pre>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Conclusion<\/h4>\n\n\n\n<p>Think about the <em><strong>reflog <\/strong><\/em>as a record of lapidary local commits, and the <em><strong>log<\/strong><\/em> as the shiny, clean history a repository publicly presents to onlookers.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5>Reference:<\/h5>\n\n\n\n<p><a href=\"https:\/\/www.theserverside.com\/\">https:\/\/www.theserverside.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The biggest difference between Git reflog vs. Git log is that the log is a public accounting of the repository&#8217;s commit history while the reflog is a private, workspace-specific accounting of the repo&#8217;s local commits. The Git log is part of the Git repository and is replicated after a push, fetch or pull. In contrast, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1599,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[60],"tags":[459,462,458,456,455,457,461,460],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1598"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1598"}],"version-history":[{"count":1,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1598\/revisions"}],"predecessor-version":[{"id":1600,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1598\/revisions\/1600"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1599"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}