{"id":11886,"date":"2024-09-01T00:23:45","date_gmt":"2024-08-31T22:23:45","guid":{"rendered":"https:\/\/monodes.com\/predaelli\/?p=11886"},"modified":"2024-09-01T00:23:46","modified_gmt":"2024-08-31T22:23:46","slug":"git-copy-a-file-or-directory-from-another-repository-preserving-the-history","status":"publish","type":"post","link":"https:\/\/monodes.com\/predaelli\/2024\/09\/01\/git-copy-a-file-or-directory-from-another-repository-preserving-the-history\/","title":{"rendered":"Git: Copy a file or directory from another repository preserving the history"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-style-default has-background is-layout-flow wp-block-quote-is-layout-flow\" style=\"background:linear-gradient(135deg,rgb(238,238,238) 0%,rgba(165,216,253,0.7) 100%)\">\n<p>How to copy a file or directory from another GIT repository while preserving its history?<\/p>\n\n\n\n<p>Internet is full of <a href=\"http:\/\/thread.gmane.org\/gmane.comp.version-control.git\/5126\/\" target=\"_blank\" rel=\"noreferrer noopener\">magic formulas<\/a> <a href=\"http:\/\/gbayer.com\/development\/moving-files-from-one-git-repository-to-another-preserving-history\/\" target=\"_blank\" rel=\"noreferrer noopener\">each one<\/a> <a href=\"http:\/\/stackoverflow.com\/questions\/1365541\/how-to-move-files-from-one-git-repo-to-another-not-a-clone-preserving-history\">more complex.<\/a><\/p>\n\n\n\n<p>Here I\u2019m proposing a much simpler and faster one that is to make a git format-patch for the entire history of the file or subdirectory that we want and then import it into the destination repository.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\" data-line=\"\">mkdir \/tmp\/mergepatchs\ncd ~\/repo\/org\nexport reposrc=myfile.c #or mydir\ngit format-patch -o \/tmp\/mergepatchs $(git log $reposrc|grep ^commit|tail -1|awk &#039;{print $2}&#039;)^..HEAD $reposrc\ncd ~\/repo\/dest\ngit am \/tmp\/mergepatchs\/*.patch<\/code><\/pre>\n\n\n\n<p>Simple and fast <img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/blog.neutrino.es\/wp-includes\/images\/smilies\/simple-smile.png?w=910\" alt=\":)\"\/><\/p>\n<cite>Source <a href=\"http:\/\/blog.neutrino.es\">synaptic fault<\/a> <a href=\"http:\/\/blog.neutrino.es\/2012\/git-copy-a-file-or-directory-from-another-repository-preserving-history\/\">http:\/\/blog.neutrino.es\/2012\/git-copy-a-file-or-directory-from-another-repository-preserving-history\/<\/a><\/cite><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"https:\/\/monodes.com\/predaelli\/2024\/09\/01\/git-copy-a-file-or-directory-from-another-repository-preserving-the-history\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"quote","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"federated","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[278],"tags":[51,405],"class_list":["post-11886","post","type-post","status-publish","format-quote","hentry","category-tricks","tag-git","tag-or","post_format-post-format-quote"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6daft-35I","jetpack-related-posts":[{"id":915,"url":"https:\/\/monodes.com\/predaelli\/2016\/01\/21\/find-and-restore-a-deleted-file-in-a-git-repository\/","url_meta":{"origin":11886,"position":0},"title":"Find and restore a deleted file in a Git repository","author":"Paolo Redaelli","date":"2016-01-21","format":false,"excerpt":"Use git log --diff-filter=D --summary to get all the commits which have deleted files and the files deleted;Use git checkout $commit~1 filename to restore the deleted file. Sorgente: Find and restore a deleted file in a Git repository - Stack Overflow","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":178,"url":"https:\/\/monodes.com\/predaelli\/2015\/05\/21\/linux-into-a-windows-domain-active-directory\/","url_meta":{"origin":11886,"position":1},"title":"Linux into a Windows Domain (Active Directory)","author":"Paolo Redaelli","date":"2015-05-21","format":false,"excerpt":"Once it was called LikeWise-open, now it's new name is powerbrokeropen.org and\u00a0 allows Active Directory authentication for Linux, UNIX, and Mac systems by joining them to Active Directory domains. It integrates very very well into a GNU\/Linux system, using Pluggable Authentication Modules (PAM) and Name Service Switch (NSS) and it\u2026","rel":"","context":"In &quot;Senza categoria&quot;","block_context":{"text":"Senza categoria","link":"https:\/\/monodes.com\/predaelli\/category\/senza-categoria\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11167,"url":"https:\/\/monodes.com\/predaelli\/2024\/01\/10\/lets-clone-simplemobiletools\/","url_meta":{"origin":11886,"position":2},"title":"Let&#8217;s clone SimpleMobileTools","author":"Paolo Redaelli","date":"2024-01-10","format":false,"excerpt":"Since SimpleMobileTools has been sold by its creator to a company notorious for buying apps from developers and repacking them with ads, putting a \"no-ads\" subscription to remove it for $15 a week, the time is ripe to clone its repositories! Join me, I already did it!: git clone https:\/\/github.com\/SimpleMobileTools\/Simple-App-Launcher\u2026","rel":"","context":"In &quot;Ethics&quot;","block_context":{"text":"Ethics","link":"https:\/\/monodes.com\/predaelli\/category\/ethics\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2024\/01\/may-the-source-be-with-you-0.webp?fit=750%2C1000&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2024\/01\/may-the-source-be-with-you-0.webp?fit=750%2C1000&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2024\/01\/may-the-source-be-with-you-0.webp?fit=750%2C1000&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2024\/01\/may-the-source-be-with-you-0.webp?fit=750%2C1000&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":11688,"url":"https:\/\/monodes.com\/predaelli\/2024\/06\/01\/never-use-git-pull-youtube\/","url_meta":{"origin":11886,"position":3},"title":"Never* use git pull &#8211; YouTube","author":"Paolo Redaelli","date":"2024-06-01","format":"video","excerpt":"https:\/\/www.youtube.com\/watch?v=xN1-2p06Urc Just a quick summary mostly for myself: How to use git pull --rebase to keep your team's commit history clean. Command for creating the 'git pr' alias (so you can copy-paste): git config --global alias.pr \"pull --rebase\" Always try git pull --rebase first. It if works, you're done! If\u2026","rel":"","context":"In &quot;Tricks&quot;","block_context":{"text":"Tricks","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/tricks\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/img.youtube.com\/vi\/xN1-2p06Urc\/0.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":10940,"url":"https:\/\/monodes.com\/predaelli\/2023\/11\/03\/versioning-data-in-postgres-testing-a-git-like-approach-specfy\/","url_meta":{"origin":11886,"position":4},"title":"Versioning data in Postgres? Testing a git like approach &#8211; Specfy","author":"Paolo Redaelli","date":"2023-11-03","format":false,"excerpt":"Versioning data in Postgres? Testing a git like approach - Specfy is fashinating but I think that most of the time these two proposed alternatives fit most of the needs: In-Table versioning, the Wordpress way of doing thing. Add a a column version (or modify date) and SELECT the maximum\u2026","rel":"","context":"In &quot;Tricks&quot;","block_context":{"text":"Tricks","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/tricks\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":15483,"url":"https:\/\/monodes.com\/predaelli\/2026\/04\/10\/the-git-commands-i-run-before-reading-any-code\/","url_meta":{"origin":11886,"position":5},"title":"The Git Commands I Run Before Reading Any Code","author":"Paolo Redaelli","date":"2026-04-10","format":"link","excerpt":"The Git Commands I Run Before Reading Any Code What Changes the Most Who Built This Where Do Bugs Cluster Is This Project Accelerating or Dying How Often Is the Team Firefighting The Git Commands I Run Before Reading Any Code Five git commands that tell you where a codebase\u2026","rel":"","context":"In &quot;Tricks&quot;","block_context":{"text":"Tricks","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/tricks\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/11886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/comments?post=11886"}],"version-history":[{"count":0,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/11886\/revisions"}],"wp:attachment":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/media?parent=11886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/categories?post=11886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/tags?post=11886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}