{"id":1893,"date":"2024-09-18T09:52:30","date_gmt":"2024-09-17T21:52:30","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=1893"},"modified":"2024-09-24T15:50:56","modified_gmt":"2024-09-24T03:50:56","slug":"squashing-commits-with-git-merge","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=1893","title":{"rendered":"Squashing Commits with git merge"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>In Git, squashing commits is a powerful technique that combines multiple commits into a single, more concise commit. This can simplify your project's history and make it easier to review changes. One common method to achieve this is using the <code>git merge --squash<\/code> command.<\/p>\n<h2>Understanding git merge --squash<\/h2>\n<p>When you use <code>git merge --squash<\/code>, Git merges the changes from the source branch into the target branch, but instead of creating a new commit for each merged change, it creates a temporary commit that contains all the changes combined. This temporary commit is not automatically recorded in the history.<\/p>\n<h2>Steps to Squash Commits<\/h2>\n<ol>\n<li>\n<p>Switch to the target branch:<\/p>\n<pre><code class=\"language-shell\">git checkout &lt;target-branch&gt;<\/code><\/pre>\n<\/li>\n<li>\n<p>Merge the source branch:<\/p>\n<pre><code class=\"language-shell\">git merge --squash &lt;source-branch&gt;<\/code><\/pre>\n<\/li>\n<li>\n<p>Review the merged changes:<\/p>\n<pre><code class=\"language-shell\">git diff<\/code><\/pre>\n<\/li>\n<li>\n<p>Create the final commit:<\/p>\n<pre><code class=\"language-shell\">git commit -m \"Squashed commits from <source-branch>\"<\/code><\/pre>\n<\/li>\n<\/ol>\n<p><strong>Example<\/strong><\/p>\n<p>Let's say you have a feature branch named <code>feature-branch<\/code> and you want to merge its changes into the <code>main<\/code> branch. Here's how you would use <code>git merge --squash<\/code>:<\/p>\n<pre><code class=\"language-shell\">git checkout main\ngit merge --squash feature-branch\ngit commit -m &quot;Merged feature changes&quot;<\/code><\/pre>\n<h2>Benefits of Squashing Commits<\/h2>\n<ul>\n<li><strong>Cleaner history:<\/strong> Reduces the number of commits, making it easier to review changes.<\/li>\n<li><strong>Improved readability:<\/strong> A concise commit history can be easier to understand and navigate.<\/li>\n<li><strong>Simplified code review:<\/strong> Fewer commits to review can streamline the code review process.<\/li>\n<\/ul>\n<h2>When to Squash Commits<\/h2>\n<ul>\n<li><strong>Small, related changes:<\/strong> If you've made a series of small, related changes, squashing them into a single commit can provide a better overview.<\/li>\n<li><strong>Experimental or temporary changes:<\/strong> If you've made changes that were experimental or temporary, squashing them can clean up the history.<\/li>\n<li><strong>Before creating a pull request:<\/strong> Squashing commits before submitting a pull request can help keep the review process focused.<\/li>\n<\/ul>\n<h2>Caution:<\/h2>\n<p>While squashing commits can be beneficial, it's important to use it judiciously. If you need to track individual commits for debugging or auditing purposes, consider merging normally instead of squashing.<\/p>\n<h2>Conclusion<\/h2>\n<p><code>git merge --squash<\/code> is a valuable tool for maintaining a clean and organized Git history. By understanding how to use it effectively, you can streamline your development workflow and improve the readability of your project's changes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In Git, squashing commits is a powerful technique that combines multiple commits into a single, more concise commit. This can simplify your project&#8217;s history and make it easier to review changes. One common method to achieve this is using the git merge &#8211;squash command. Understanding git merge &#8211;squash When you use git merge &#8211;squash, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1893"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1893"}],"version-history":[{"count":2,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1893\/revisions"}],"predecessor-version":[{"id":1895,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1893\/revisions\/1895"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}