Skip to main content

Git: Move Subdirectory to New Repository

I recently published my Brief emulation mode for Emacs to the MELPA Emacs package repository. This is code I wrote well over twenty years ago and have been using daily since.

I hadn't published it before partly because it was languishing in a sub-directory of my main Emacs repo (and partly because I never had the time before). To become a package in its own right though, it had to have its own repo.

Splitting out a subdirectory to a new repo used to be a complex task in Git (usually involving complicated invocations of git filter-branch), but these days it's reasonably straightforward with the git subtree command (well at least as straightforward as anything else in Git 😀).

The other reason I like the subtree method is it's non-destructive - you can back out at any point and still get back to where you started, plus all the history is retained.

Here's how it works:

Step 1: Create a new branch in the main repo with just the sub-directory commits

$ cd <main-dir>
$ git subtree split -P <sub-dir> -b <new branch>

Step 2: Create new repo in the sub-directory

$ cd <sub-dir>
$ git init

Step3: Pull branch into new repo

$  git pull <main-dir> <branch>

Step 4: Create new remote repo

  • Using your upstream repo of choice.

Step 5: Push new repo to remote

$ git remote add origin <remote ref>
$ git push -u origin master

Step 6: Remove sub-directory from original repo

$ cd <main-dir>
$ git rm -r --cached <sub-dir>
$ git commit

(Optional) Step 7: Delete branch from original repo

$ git branch -d <branch>

Comments

Popular posts from this blog

My Work in new Top Trumps Birds of Prey Pack

The new Top Trumps "Birds of Prey" pack has my picture illustrating the Secretary Bird card 😀 Here's the original picture: From Flickr

On BBC Springwatch !

BBC Springwatch featured one of my Goldfinch pictures last night: Goldfinch on Springwatch Shame they spelt my name wrong though! See the original on Flickr.

Emacs and MacOS Catalina

Catalina introduced a lot of security changes and the most intrusive is probably all the popups asking to give permission for apps to access directories under Home, like Documents. Worse still, apps which weren't written to handle the new security measures might just fail silently with no clues for the user. A solution is to give apps like Emacs "Full Disk Access" under "Security & Privacy" in Preferences, to give unfettered access to your files and avoid all the popups and silent failures. Sounds good, but that doesn't actually work for Emacs because "Emacs" in the app bundle is actually a Ruby script which decides which flavour of Emacs executable to run. This never mattered before, but it does under Catalina because MacOS thinks the executable is /usr/bin/ruby . Conventional wisdom is therefore to give "Full Disk Access" to Ruby. While this does work, I've always been uncomfortable giving all Ruby scripts full access...