From: W. Trevor King Date: Thu, 15 Mar 2012 18:59:55 +0000 (-0400) Subject: Add note on merging a single file from a foreign repo using Git. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=28a44079cc14e106d0419f682dd0765dc8e5a72d;p=blog.git Add note on merging a single file from a foreign repo using Git. --- diff --git a/posts/Git/notes.org b/posts/Git/notes.org index 702b158..ab5a547 100644 --- a/posts/Git/notes.org +++ b/posts/Git/notes.org @@ -486,6 +486,36 @@ Discarding all other history : $ git filter-branch --subdirectory-filter foodir -- --all +** Merge a single file (with history) into another repository + +Merge the file =myfile= (with history) from =REPO_A= into =REPO_B=. + +Create a filtered version of =REPO_A=: + + : $ git clone REPO_A work-a + : $ cd work-a + : $ SPELL='git ls-tree -r --name-only --full-tree "$GIT_COMMIT" | grep -v "[.]git/\|myfile" | grep -v "^[.]git\$" | tr "\n" "\0" | xargs -0 git rm --cached -r --ignore-unmatch' + : $ git filter-branch --prune-empty --index-filter "$SPELL" -- --all + +Alter the =grep= stuff as you need. The goal is to remove anything +you want to keep from the listing produced by =SPELL=. + +Merge it into =REPO_B=: + + : $ cd .. + : $ git clone REPO_B work-b + : $ git remote add myfile-repo ../work-a + : $ git pull myfile-repo master + : $ git remote rm myfile-repo + +Clean up: + + : $ cd .. + : $ rm -rf work-a REPO_B + : $ mv work-b REPO_B + +Thanks to [[http://gbayer.com/development/moving-files-from-one-git-repository-to-another-preserving-history/][Greg Bayer]] and [[http://stackoverflow.com/questions/7375528/how-to-extract-one-file-with-commit-history-from-a-git-repo-with-index-filter][Peter Hillerström]]. + ** Cherry pick a commit from another repository First, give the remote repository a nickname (optional)