Git filter-branch -tree-filter "rm -rf " -prune-empty HEAD My solution was to use the tree-filter and to simply remove the unwanted files and directories from a temporary clone of the source repository, then pull from that clone into my target repository in 5 simple steps.
In my scenario I just want to merge parts of one repository into another and retain the history WITH full path name. The subdirectory-filter (or the shorter command git subtree) works good but did not work for me since they remove the directory name from the commit info. Note: You can use a merge instead of a pull, but pull works better.įinally, you probably want to clean up a bit by removing the remoteĬonnection to repository A git remote rm repo-A-branch git pull repo-A-branch master -allow-unrelated-histories Pull from this branch (containing only the directory you want to git remote add repo-A-branch /c/Working/GIT/myprojects # (repo-A-branch can be anything - it's just an arbitrary name)
git clone FOLDER_TO_KEEP is the name of the new repository you are copying to)Ĭreate a remote connection to repository A as a branch in repository Make a copy of repository B if you don’t have one already git clone git mv * FOLDER_TO_KEEPĬommit your changes and we’re ready to merge these files into the Move files into that directory git mv * eg. You may want to import these files into repository B within a directory not the root: FILE_TO_KEEP = pom.xml to keep only the pom.xml file from FOLDER_TO_KEEP Mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE || echo "Nothing to do"' -prune-empty -all 'git ls-files -s | grep $'\t'FILE_TO_KEEP$ | You don't want with the same name and commit.) git filter-branch -f -index-filter \ git filter-branch -subdirectory-filter subfolder1/subfolder2/FOLDER_TO_KEEP -allįor single file move only: go through what's left and removeĮverything except the desired file. git filter-branch -subdirectory-filter -all The result is the contents of directory 1 spewed out Go through your history and files, removing anything that is not inĭirectory 1. # -v (assuming myprojects is the repository you want to copy from)ĭelete the link to the original repository to avoid accidentally git clone -branch master -origin origin -progress \ Make a copy of repository A as the following steps make majorĬhanges to this copy which you should not push! git clone -branch -origin origin -progress \ It involves cloning the repository you want to move the file or folder from, moving that file or folder to the root, rewriting Git history, cloning the target repository and pulling the file or folder with history directly into this target repository. Having tried various approaches to move a file or folder from one Git repository to another, the only one which seems to work reliably is outlined below. Note that this involves merging the history into an existing repository, rather than simply creating a new standalone repository from part of another one ( as in an earlier question). Is there a better way to do this sort of thing in general? Or have I adopted the right approach? $ git merge p2 # -allow-unrelated-histories for git 2.9+īut that seems pretty convoluted. $ git commit -m "moved files to new subdirectory" > git mv $f deeply/buried/different/java/source/directory/B $ mkdir -p deeply/buried/different/java/source/directory/B $ git remote rm origin # so I don't accidentally overwrite the repo -) $ git filter-branch -subdirectory-filter deeply/buried/java/source/directory/A -all I did something like this: $ git clone project2 But in Git, each project is in its own repository, and today I was asked to move a subdirectory from project2 to project1. Obviously, it was pretty easy to move files from one to another with svn mv. Our Git repositories started out as parts of a single monster SVN repository where the individual projects each had their own tree like so: project1/branches