{"id":2725,"date":"2024-04-29T14:51:55","date_gmt":"2024-04-29T14:51:55","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=2725"},"modified":"2024-07-01T15:18:58","modified_gmt":"2024-07-01T15:18:58","slug":"git-commit-logs","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2024\/04\/29\/git-commit-logs\/","title":{"rendered":"Git: Commit logs"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-1-today-1024x1024.jpeg\" alt=\"\" class=\"wp-image-2726\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-1-today-1024x1024.jpeg 1024w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-1-today-150x150@2x.jpeg 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-1-today-150x150.jpeg 150w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-1-today.jpeg 1536w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-1-today-300x300@2x.jpeg 600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Every project has a story, and in the world of software development, that story is often told through commit logs. For those unfamiliar, Git is a version control system used for tracking changes in code. Commit logs are the detailed record of those changes, providing a crucial window into a project&#8217;s history.<\/p>\n\n\n\n<p>In this blog, we&#8217;ll delve into the world of Git commit logs, exploring how they work and how you can maintain them effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What&#8217;s in a Commit Log?<\/strong><\/h2>\n\n\n\n<p>When you make changes to your code and commit them using Git, you&#8217;re essentially creating a snapshot of your project at that specific point in time. Each commit includes several key pieces of information:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Commit Hash:<\/strong> A unique identifier for the commit, like a fingerprint for your code.<\/li>\n\n\n\n<li><strong>Author:<\/strong> Who made the changes (usually includes name and email address).<\/li>\n\n\n\n<li><strong>Date:<\/strong> When the changes were committed.<\/li>\n\n\n\n<li><strong>Commit Message:<\/strong> A brief description of the changes made.<\/li>\n<\/ul>\n\n\n\n<p>This commit message is the heart of a good commit log. It should be clear, concise, and informative, explaining what was changed and why.<\/p>\n\n\n\n<p><strong>Maintaining Clear Commit Logs<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Here are some tips for keeping your commit logs clean and informative:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Write Descriptive Messages:<\/strong> Avoid vague messages like &#8220;Fixed stuff.&#8221; Instead, explain what was fixed and how it benefits the project.<\/li>\n\n\n\n<li><strong>Focus on Functionality:<\/strong> The message should describe the changes made to the code&#8217;s functionality, not the specific lines of code edited.<\/li>\n\n\n\n<li><strong>Use Proper Tense:<\/strong> Commit messages are written in the past tense, as they represent a completed action.<\/li>\n\n\n\n<li><strong>Keep it Consistent:<\/strong> Develop a convention for formatting your commit messages, ensuring a uniform look and feel throughout the log.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Benefits of Clean Commit Logs<\/strong><\/h3>\n\n\n\n<p>Clear commit logs provide a multitude of benefits for developers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Improved Collaboration:<\/strong> Team members can easily understand the project&#8217;s history and the rationale behind changes.<\/li>\n\n\n\n<li><strong>Easier Debugging:<\/strong> When issues arise, clear commit messages help pinpoint the exact change that might have introduced the problem.<\/li>\n\n\n\n<li><strong>Project Tracking:<\/strong> Commit logs act as a roadmap, allowing developers to track the evolution of the project over time.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Tools for Effective Log Management<\/strong><\/h3>\n\n\n\n<p>While Git itself provides the <code>git log<\/code> command for viewing commit history, several helpful tools can enhance log management:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Git Graphical Interfaces:<\/strong> Many Git GUI clients offer a visual representation of commit history, making it easier to navigate and understand.<\/li>\n\n\n\n<li><strong>Commit Message Linters:<\/strong> These tools enforce best practices for writing commit messages, ensuring consistency and clarity.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Modifying a commit log?<\/h3>\n\n\n\n<div class=\"wp-block-snow-monkey-blocks-flex smb-flex is-layout-flex wp-container-snow-monkey-blocks-flex-is-layout-01b195ca wp-block-snow-monkey-blocks-flex-is-layout-flex is-vertical\">\n<figure class=\"wp-block-image size-large is-style-default\"><img decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-5-today-1024x1024.jpeg\" alt=\"\" class=\"wp-image-2728\" style=\"aspect-ratio:3\/2;object-fit:contain\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-5-today-1024x1024.jpeg 1024w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-5-today-150x150@2x.jpeg 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-5-today-150x150.jpeg 150w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-5-today.jpeg 1536w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-5-today-300x300@2x.jpeg 600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n\n<p>Ever made a commit message that could use some improvement? Thankfully, Git offers ways to modify existing commit messages. Here&#8217;s a breakdown of the steps depending on the commit you want to change:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Recent Commit (not yet pushed):<\/strong><\/h3>\n\n\n\n<p>This is the simplest scenario. Use the <code>git commit --amend<\/code> command:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Navigate to your Git repository using your terminal.<\/li>\n\n\n\n<li>Run the command <code>git commit --amend<\/code>. This opens your default text editor with the current commit message.<\/li>\n\n\n\n<li>Edit the message to your liking, ensuring clarity and conciseness.<\/li>\n\n\n\n<li>Save and close the editor. Git will create a new commit with the updated message, replacing the previous one.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Older Commit (or Pushed Commit):<\/strong><\/h3>\n\n\n\n<p>Here, things get a bit more involved. We&#8217;ll use interactive rebasing:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Navigate to your Git repository.<\/li>\n\n\n\n<li>Run the command <code>git rebase -i HEAD~n<\/code>, where <code>n<\/code> is the number of commits you want to go back and edit (usually around 3-5 is manageable). This opens a list of the specified commits in your editor.<\/li>\n\n\n\n<li>Locate the commit with the message you want to change. In front of the commit line, replace <code>pick<\/code> with <code>reword<\/code>.<\/li>\n\n\n\n<li>Save and close the editor. Git will walk you through each commit on the list you selected for rebasing.<\/li>\n\n\n\n<li>When prompted for the specific commit you want to modify, edit the commit message using your preferred editor.<\/li>\n\n\n\n<li>After modifying the message, save and close the editor.<\/li>\n\n\n\n<li>Git will automatically create a new commit with the updated message.<\/li>\n\n\n\n<li>When you are ready you need to push the changes<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code has-black-color has-pale-cyan-blue-background-color has-text-color has-background has-link-color wp-elements-7d94a88d201de920dc6c1388e3217ef7\"><code>git push --force origin &lt;your branch><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Reverting to a last commit?<\/h3>\n\n\n\n<p>There are two main approaches to revert to the last <code>nth<\/code> <code>commit<\/code> that was <em>pushed<\/em>, depending on your situation and comfort level with rewriting Git history:<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Option 1: Using Git Revert (Recommended for Un-pushed or Private Branches)<\/strong><\/h4>\n\n\n\n<p>This method is ideal if you haven&#8217;t pushed your commits to a remote repository (like GitHub) or if you&#8217;re working on a private branch. It creates a new commit that effectively undoes the changes introduced in the last nth commit(s).<\/p>\n\n\n\n<p>Here&#8217;s how to do it:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Navigate to your Git repository using your terminal.<\/li>\n\n\n\n<li>Run the command <code>git revert HEAD~n<\/code>, where <code>n<\/code> is the number of commits you want to revert.<\/li>\n<\/ol>\n\n\n\n<p><strong>Example:<\/strong> To revert the last 2 commits: <code>git revert HEAD~2<\/code><\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li>This will create a new commit with a message indicating it&#8217;s a revert. You can edit this message if needed.<\/li>\n\n\n\n<li>Once you&#8217;re happy with the revert commit, stage and commit it using:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-link-color wp-elements-21e204056b346a7b9d422523e8785041\"><code>git add .\ngit commit -m \"Revert of the last n commits (your explanation)\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Option 2: Using Git Reset &#8211;hard (Advanced Users, Caution Advised)<\/strong><\/h3>\n\n\n\n<p>This approach is more aggressive and rewrites Git history. It&#8217;s recommended for experienced users and only if you haven&#8217;t pushed your commits to a shared repository. <\/p>\n\n\n\n<figure class=\"wp-block-pullquote has-black-color has-luminous-vivid-amber-background-color has-text-color has-background has-link-color has-medium-font-size wp-elements-a10aaab9eae7e68e5aa03baa19a969c3\"><blockquote><p><strong>Using this on a pushed branch can cause issues for collaborators.<\/strong><\/p><cite>git-documentation<\/cite><\/blockquote><\/figure>\n\n\n\n<p>Here&#8217;s how to do it (use with caution):<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Navigate to your Git repository using your terminal.<\/li>\n\n\n\n<li>Run the command <code>git log<\/code> to identify the commit hash of the commit you want to go back to (the good state before the last nth commits).<\/li>\n\n\n\n<li>Use the command <code>git reset --hard &lt;commit_hash&gt;<\/code>, replacing <code>&lt;commit_hash&gt;<\/code> with the actual hash you identified in step 2.<\/li>\n<\/ol>\n\n\n\n<p><strong>Warning:<\/strong> This detached your HEAD from the current branch, meaning you&#8217;re no longer pointing to the latest commit history.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li>To fix this and create a new branch pointing to the commit you reset to, run:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>git checkout -b new_branch_name &lt;commit_hash&gt;\n<\/code><\/pre>\n\n\n\n<p><strong>Remember:<\/strong> Always double-check before using <code>git reset --hard<\/code> and ensure you understand the implications of rewriting history.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-4-today-1024x1024.jpeg\" alt=\"\" class=\"wp-image-2729\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-4-today-1024x1024.jpeg 1024w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-4-today-150x150@2x.jpeg 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-4-today-150x150.jpeg 150w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-4-today.jpeg 1536w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2024\/04\/image-4-today-300x300@2x.jpeg 600w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Additional Notes:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>These commands only affect your local repository. If you&#8217;ve already pushed the commits you want to revert, you&#8217;ll need to push the new revert commit(s) as well.<\/li>\n\n\n\n<li>Consider the impact on your team if you&#8217;re working on a shared branch. Communicate any changes to the commit history clearly.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Every project has a story, and in the world of software development, that story is often told through commit logs. For those unfamiliar, Git is a version control system used for tracking changes in code. Commit logs are the detailed record of those changes, providing a crucial window into a project&#8217;s history. In this blog, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2727,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2725","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=2725"}],"version-history":[{"count":2,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2725\/revisions"}],"predecessor-version":[{"id":2751,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2725\/revisions\/2751"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/2727"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=2725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=2725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=2725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}