vim, git, aws and other three-letter words

Software Development Notes

git: move files with history to another repository

Git allows joining unrelated repositories via remotes, which, in turn, allows moving files and change history between them.

Some cases when this might be needed:

Preserving the history has an important effect: after we do the change, for example, extract a part of a bigger repository into a separate repository, we can continue moving changes between them (merge updates from the big repository to the small one and back).

Below, I am assuming the first case (extracting a part of the big repository). Other cases can be implemented with a similar technique.

Setup

Assume we have a source (upstream) repository and we want to extract a top-level /lib folder into a separate repository. First, create a destination repository:

mkdir dest-repo
cd dest-repo
git init

Add the main repository as a remote and check out the master branch:

git remote add -f upstream git@github.com:myaccount/main.git
git checkout -b upstream_master upstream/master
git subtree split --prefix=lib/ -b upstream_lib

What we do above is: add the source (upstream) repository as a remote, check out its master branch to the destination repository and then use git subtree split to create the upstream_lib branch containing only the /lib folder with all the change history.

Now we can merge the upstream files and history from the /lib folder into the target master branch:

git checkout master
git merge upstream_lib
git push origin HEAD

Update: a better version?   folded

Now we can create the target master branch from the upstream files and history of the /lib folder and push it to the destination remote:

git checkout -B master upstream_lib
git remote add origin git@github.com:myaccount/lib.git
git push origin master

Update from upstream

Pull the latest upstream master branch and re-split it into a new branch:

git checkout upstream_master
git pull
git subtree split --prefix=lib/ -b upstream_lib_YYYY.MM.DD

Merge the update:

git checkout master
git merge upstream_lib_YYYY.MM.DD
git push origin HEAD

Tags: git