{"id":389886,"date":"2022-01-05T15:00:25","date_gmt":"2022-01-05T12:00:25","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-apply-git-commit-diffs-to-different-files-cloudsavvy-it\/"},"modified":"2022-01-05T15:00:25","modified_gmt":"2022-01-05T12:00:25","slug":"how-to-apply-git-commit-diffs-to-different-files-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-apply-git-commit-diffs-to-different-files-cloudsavvy-it\/","title":{"rendered":"#How To Apply Git Commit Diffs to Different Files \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a29395e8146a\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a29395e8146a\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-apply-git-commit-diffs-to-different-files-cloudsavvy-it\/#The_Problem\" >The Problem<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-apply-git-commit-diffs-to-different-files-cloudsavvy-it\/#Using_Git_Diffs_and_Patches\" >Using Git Diffs and Patches<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How To <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">App<\/a>ly Git Commit Diffs to Different Files \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-14290\" data-pagespeed-lazy-srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/4d72a7db.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/4d72a7db.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/4d72a7db.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1200\" height=\"675\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Git is a powerful version control system that allows for multiple branching versions of your project with automatic merging. That\u2019s great, but sometimes it breaks when files are renamed or aren\u2019t in the right place. Luckily, you can manually apply patches to different files.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"The_Problem\"><\/span>The Problem<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When using Git branches, it\u2019s often necessary to apply changes from one branch to another. A common use case of this is versioned branches\u2014if you have an old LTS release, you may want to apply the occasional patch to an old version. Or perhaps you are providing two builds of software targetting different toolchains or environments, and need to keep them in sync.<\/p>\n<p>If your branches have the same layout, you can use <code>git cherry-pick<\/code>, which can copy individual commits from one branch onto another. For example, pulling a commit from a feature branch onto <code>master<\/code>\u00a0before the entire thing is merged:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13233\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/3c4e20b0.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1200\" height=\"500\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p><strong>RELATED:<\/strong> <strong><em>What Does Git Cherry Pick Do, And When Should\u00a0You Use It?<\/em><\/strong><\/p>\n<p>However, software isn\u2019t always clean, and in the process of refactoring, you may come across cases where files have been moved or renamed. Git works off of directory paths, so this completely breaks <code>git cherry-pick<\/code>, as Git cannot apply changes to files that do not exist.<\/p>\n<p>Git has tools to fix this though, so when normal merging breaks down, you can manually patch files with updates using <code>git patch<\/code>.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Using_Git_Diffs_and_Patches\"><\/span>Using Git Diffs and Patches<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To showcase this, we\u2019ll create an empty repository with a single file: <code>Old.java<\/code>. A new branch, <code>old-version<\/code>, is made with this file, plus some changes. This file gets renamed with some later changes on <code>master<\/code>\u00a0branch to <code>New.java<\/code>, breaking compatibility between the branches.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15184\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/12\/bc38fd10.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1168\" height=\"500\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>And this is what the branch history looks like, shown in the GUI Git program Fork:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-15185\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/12\/30da9055.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"500\" height=\"199\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>In this example, we need to move the \u201cAdd more code\u201d commit onto the <code>old-version<\/code>\u00a0branch. To do that, you\u2019ll need to get the ID of that commit from the reference log:<\/p>\n<pre>git reflog<\/pre>\n<p>Then, run <code>format-patch<\/code>\u00a0with the ID and <code>-1<\/code>\u00a0flag, which will create a patch file:<\/p>\n<pre>git format-patch 82176b5 -1<\/pre>\n<p>This will generate a patch file in the current directory. You may want to move this to a different folder that is in the <code>.gitignore<\/code>\u00a0config, so it\u2019s not affected by changing branches.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15190\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/12\/4e415a7d.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"855\" height=\"538\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Then, checkout the <code>old-version<\/code>\u00a0branch, and apply the patch changes using the Linux <code>patch<\/code>\u00a0utility. Git has their own tools, <code>git apply<\/code>\u00a0and <code>git am<\/code>, which can handle it, but short of manually editing the patch file, they don\u2019t have options for changing the target file in cases of renames\/moves.<\/p>\n<p>You can pass the filename with the <code>-p1<\/code>\u00a0parameter, and pass the patch file with <code>-i<\/code>.<\/p>\n<pre>patch -l -p1 old -i Patches\/0001-Add-more-code.patch<\/pre>\n<p>This should edit the file and add in the changes, which you will need to commit. One downside of <code>patch<\/code>\u00a0compared to <code>git am<\/code>\u00a0is that is does not preserve commit info, though you can find that in the <code>.patch<\/code>\u00a0file if you want to keep the original message.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-15187\" data-pagespeed-lazy-src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/12\/ef417185.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"622\" height=\"227\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>However, merge conflicts can still happen if the target branch gets edited in the meantime, and in the event one does, you\u2019ll be stuck with manual editing, since <code>git merge<\/code>\u00a0cannot solve this for you.\u00a0<code>patch<\/code>\u00a0will output files containing the diffs, which you will need to manually fix.<\/p>\n<p>Alternatively, you could move the target file to the target directory Git expects, allowing Git to <code>cherry-pick<\/code>\u00a0and apply changes. Then, copy the file back to the original place, and delete the temporary file.<\/p>\n<p>Patch can also have issues with line feed endings (LF) vs carriage return line feed (CRLF) that can result from Windows editing. You may need to swap to LF in your text editor for the patch to apply correctly.<\/p>\n<\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/15183\/how-to-apply-git-commit-diffs-to-different-files\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How To Apply Git Commit Diffs to Different Files \u2013 CloudSavvy IT&#8221; Git is a powerful version control system that allows for multiple branching versions of your project with automatic merging. That\u2019s great, but sometimes it breaks when files are renamed or aren\u2019t in the right place. Luckily, you can manually apply patches to different&#8230;<\/p>\n","protected":false},"author":1,"featured_media":389887,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/09\/4d72a7db.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-389886","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/389886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=389886"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/389886\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/389887"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=389886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=389886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=389886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}