Igor's Blog

We've been moving all our code from Subversion to Git at work. As we've gone through this activity it made sense to split up some of the repositories to separate some of the source files more logically. Specifically what we wanted to do was move all of rarely updated library and utility type projects into a 'utils' repository while keeping the frequently updated projects in a 'main' repository. The catch was that we had to do this on a 'development' branch and we wanted to preserve the history of the projects that were being split into their own repository.

I tried to follow this article and the various comments left within it, but none of it seemed to work for me. That article naturally assumed that the master branch was being used. I've made some tweaks and came up with my own set of commands to do this that takes branches into account.

Objective: On branch 'dev', move directory 'LibProject1' from Git repository 'source-main' to 'source-utils' while preserving history of commits for that directory.
Assumption: Both source and target repositories have the same branch name set up. This is not a hard requirement and can be tweaked in the commands below but this is what I needed in my case.
 Git Details
Source Repository URL: [email protected]:user/source-main.git
Target Repository URL: [email protected]:user/source-utils.git
Branch: dev
Directory to move: LibProject1


Here is the first part, cloning the source repository and extracting just the directory you want to move...
 Git Commands
git clone [email protected]:user/source-main.git --branch dev --single-branch srcrepo
cd srcrepo
git remote rm origin
git filter-branch --subdirectory-filter LibProject1 -- --all
mkdir LibProject1
mv * LibProject1
git add .
git commit


At this point there is a Git repository that's been disconnected from the remote repository and contains just the directory that we're interested in moving.

We don't want to continue working in the source repository directory, so go up a directory...
 Git Commands
cd ..


Now comes the second part, cloning the target repository, creating a remote connection to the source as a branch and pulling in the contents of the branch before finally pushing the changes up to the target remote repository.
 Git Commands
git clone [email protected]:user/source-utils.git --branch dev --single-branch trgtrepo
cd trgtrepo
git remote add tmpbranch ../srcrepo
git pull tmpbranch dev
git remote rm tmpbranch
git push




That's pretty much it. Now the target repository has the contents and history of the directory you wanted moved over. The original directory can be deleted from the source repository at this stage, also the srcrepo and trgtrepo directories that were created during this process should now be removed as well.

-i


,
About — I'm an enthusiastic software engineer and consultant interested in many fields including J2EE, programming, electronics, 3D printing, video games, wood working and gardening.
See my Resume for more information.
The views expressed in this blog are my own and not those of my employer.
comments powered by Disqus
My other posts you may like...
Programming, DIY, Games, Hacks, Tech and more.
Follow me on...
Current and Past Projects
See my Resume

Subscribe


RSS Feed

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer

Recent Blog Posts

WebLogic package-name element classpath generator script

Using Jersey 2.x as a shared library on WebLogic 12.1.2

Google DFP with AdSense fallback is causing infinite scroll pages to go haywire

Workaround for Mobile Safari scrollTop() not updating during scroll

Skipping execution of Maven plugins that do not have a native skip option

Unmarshalling an XML fragment representing a JAXB object without XmlRootElement

Injecting a ContainerRequestContext into a Jersey entity provider class

Obfuscating PHP source code with Maven and YAK Pro PO

Making use of corner shelves with easy to make slide out draws

Octotree breaks GitLab Issue Boards

Recent Galleries

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

Good Friday Electronics fun Easter Bunny LED PCB Kit IBEABU-01.0

Top Categories

Blogs I follow

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth

Friends

RAWS Parts Online
Alpha Dimensions Hosting
Kristensen Photography
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Follow me on... 
     
...or subscribe for updates!

Don't show this again