https://github.com/opcode81/git-migrate-subdir
Describes a method for moving a directory in one git repository to a new path in another repository
https://github.com/opcode81/git-migrate-subdir
Last synced: 11 months ago
JSON representation
Describes a method for moving a directory in one git repository to a new path in another repository
- Host: GitHub
- URL: https://github.com/opcode81/git-migrate-subdir
- Owner: opcode81
- Created: 2019-01-05T12:17:36.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-07-10T10:27:36.000Z (almost 3 years ago)
- Last Synced: 2025-04-13T14:41:50.695Z (about 1 year ago)
- Language: Python
- Size: 1.95 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Moving a directory in one repository to a new path in another repository
This document explains how to move a subtree rooted at a given directory in one repository to a new path in another repository and ...
* maintain all the history,
* not require `--follow` for the history to work,
* not include any baggage, i.e. without superfluous commits that do not directly impinge upon the directory in question.
## Task
The following instructions assume that we want to move the contents of `./the/source/dir` in some source repository to `./new/dir` in some target repository.
Note: These two paths and the source repository URL``are the only variables that you will need to adapt in the instructions below.
## Steps to take in the source repository
From the source branch in the source repository, create a new branch that contains only the data to be moved:
git subtree split -P the/source/dir -b temp-branch
This will cause data previously at `./the/source/dir/` in the source branch to be in `./` in the newly created branch `temp-branch`.
Since we want the data to end up in `new/dir` eventually, we rewrite the new branch’s history to move it there:
git checkout temp-branch
git filter-branch --prune-empty -f --tree-filter '
git ls-tree --name-only -r $GIT_COMMIT | egrep -v "^new/dir/" | xargs -L1 dirname | xargs -I thedir mkdir -pv new/dir/thedir
git ls-tree --name-only -r $GIT_COMMIT | egrep -v "^new/dir/" | xargs -I thepath mv -v thepath new/dir/thepath
'
Note that there are four occurrences of `new/dir` in the above snippet. The **Python script** `moveToSubDir.py` serves to assist in the adaptation of the command and prints an adapted version for you.
## Steps to take in target repository
Add a remote for the source repository and merge the temporary branch we created.
git remote add –f source-repo
git merge source-repo/temp-branch --allow-unrelated-histories
Voila, you will have source-repo’s `./the/source/dir` at `./new/dir` in the target repository.