I have one repo. I need to move some files and directories to another repo.
I managed to extract directory using git filter-branch --subdirectory-filter config/deploy -- --all
.
I also need to move file config/deploy.rb
.
How to do this?
Join Stack Overflow to learn, share knowledge, and build your career.
I have one repo. I need to move some files and directories to another repo.
I managed to extract directory using git filter-branch --subdirectory-filter config/deploy -- --all
.
I also need to move file config/deploy.rb
.
How to do this?
Copy the patches.
As commenter Tim implied, git history technically cannot be moved from one repo to another. However, since you're rewriting history anyway, you obviously want to copy the file's history into your new repo.
I have some scripts that do this and work fine with the following caveats:
For brevity, I'm only listing the meat of the scripts:
# git-cp-history
OTHER_REPO_DIR="$1"
FILES="$@"
git-export-history "$FILES" | git-import-history "$OTHER_REPO_DIR"
# git-export-history
FIRST_COMMIT=`git log --format=%H --reverse -- "${FILES[@]}" | head -n1`
git format-patch --stdout "$FIRST_COMMIT"^..HEAD -- "${FILES[@]}"
# git-import-history
cd "$1"
git am
Since the scripts are named "git-*" and in my PATH, I use them like this:
git cp-history ../other-repo local/repo/file.txt
FIRST_COMMIT=0000000000000000000000000000000000000000; git format-patch --stdout "$FIRST_COMMIT"^..HEAD -- config/deploy.rb
returns fatal: bad revision '0000000000000000000000000000000000000000^..HEAD'
– Alex Tonkonozhenko
Oct 26 '16 at 14:17
git format-patch --stdout --root HEAD -- "${FILES[@]}"
(untested, HEAD probably optional). Read git help format-patch
for more details. The basic git format-patch | (cd ; git am)
process should work, but you may have to fiddle with the details for your specific case.
– willkil
Oct 26 '16 at 14:32
After git filter-branch
has completed, the branches contain only the filtered history. You can now just push the branches to a different repository:
git init --bare ../elsewhere
git push ../elsewhere master branch1 branch2 branch3
Now you have a bare repository with the filtered history from which you can clone new repositories.
If you are missing one file in the filtered history, you have to repeat the filtering so that it is not excluded.
filter-branch
– Alex Tonkonozhenko
Oct 26 '16 at 12:44
git filter-branch
, you may want to modify the commits in the copying process.) – torek Oct 26 '16 at 15:57