0

I followed the potential solution in

Detach (move) subdirectory into separate Git repository

My requirement is similar but only slightly different Hope someone could provide some easy solution.

Say I have the following repo:

node-browser-compat
├── ArrayBuffer
├── Audio
├── Blob
├── FormData
├── atob
├    ├──aaa.xyz 
├── btoa
├── location
└── navigator

I want to have a new repo abcd which has all of btoa and aaa.xyz in it while retaining the history of both btoa and aaa.xyz. Any help will be highly appreciated.

Note: If I already have an empty repo called abcd, instructions to moving btoa and aaa.xyz into abcd will also help.

Community
  • 1
  • 1
Ananth
  • 21
  • 8

2 Answers2

0

Maybe we have more than one solution, but I propose this solution: Since you didn't mention about branch, I suppose that you have only one branch.

Create a new empty Git repository named abcd, https://example.com/USERNAME/abcd.git

Clone node-browser-compat to abcd

git clone URL_of_node-browser-compat
git remote set-url origin https://example.com/USERNAME/abcd.git
git push -u origin master

Prune unnecessary folders/files:

git filter-branch --tree-filter 'rm -rf ArrayBuffer' --prune-empty HEAD
git filter-branch --tree-filter 'rm -rf Audio' --prune-empty HEAD
git filter-branch --tree-filter 'rm -rf Blob' --prune-empty HEAD
git filter-branch --tree-filter 'rm -rf FormData' --prune-empty HEAD
git filter-branch --tree-filter 'rm -rf location' --prune-empty HEAD
git filter-branch --tree-filter 'rm -rf navigator' --prune-empty HEAD

If directory/folder atob has more than 1 item aaa.xyz, let prune other folder/file in same way. Then push to repository abcd

git add -A .
git push

Reference

https://help.github.com/articles/changing-a-remote-s-url/

https://git-scm.com/docs/git-filter-branch

Vy Do
  • 46,709
  • 59
  • 215
  • 313
0

If you've got a longish history or a largeish worktree, this will be dramatically faster if you avoid unneeded churn.

Make a scratch ref-surgery repo on the fastest filesystem you can find, that's a tmpfs (on windows you'll want to install a "RAM disk", ImDisk has worked well for me).

scratch=`mktemp -dt`
git init --template='' $scratch
cd !$
echo /path/to/your/node-browser-compat/.git/objects >.git/objects/info/alternates

Make a nonce checkout, then set up all your branch refs:

git checkout $(git commit-tree $(git mktree <&-) <&-)
git fetch /path/to/your/node-browser-compat/ --no-tags +refs/heads/*:refs/heads/*

Now you can rewrite anything you want with a completely free hand -- if anything goes wrong, just abandon and rm -rf.

git filter-branch --index-filter '
        git read-tree --empty
        git reset -q $GIT_COMMIT atob/aaa.xyz btoa
' -- --all -- atob/aaa.xyz btoa

and you can clone or push any part of the result to wherever you want.

On a four year old midrange chip (3570K) this took about five minutes to do all ten years of git's history, rewriting 11924 commits, about a quarter of the total, that happened to alter the tests directory or builtin/var.c.

(edit: replaced origin with actual pathname, the remote name was a holdover from an earlier version)

jthill
  • 55,082
  • 5
  • 77
  • 137
  • `code` Rewrite ... (1/189) (0 seconds passed, remaining 0 predicted) /usr/lib/git-core/git-filter-branch: line 372: read-tree: command not found Usage: reset [options] [terminal] Options: -c set control characters -e ch erase character -I no initialization strings -i ch interrupt character -k ch kill character -m mapping map identifier to type -Q do not output control key settings -r display term on stderr -s output TERM set command -V print curses-version -w set window-size index filter failed: > git read-tree --empty `code` – Ananth Jan 23 '17 at 22:25
  • Thanks for your response Got above result at the git filter-branch... command – Ananth Jan 23 '17 at 22:35
  • Looks like a typo when you tried it, there's no way on earth it can't find `git read-tree`, and it's complaining about not being able to find `read-tree`, so I'm thinking transcription error of some sort. – jthill Jan 23 '17 at 22:37
  • You were right. I had copied up an extra > from a previous command. I retried by copying command from your suggestion directly and it worked. Thanks a lot. – Ananth Jan 23 '17 at 23:09