1

I have code files from dozens of git repos in various subfolders under c:\code folder - 16Gb.

I want to migrate this folder to another computer. It's currently taking > 1 day to copy the entire folder to a USB drive, because it is around 650,000 small files.

Is there some script I can run to cleanup all of the repos in my c:\code folder?

Edit: all of the repos have a remote. I don't care about copying all branches. I only care about keeping the directory structure of the repos, i.e.

c:\code\github\NLog
c:\code\github\Swashbuckle.AspNetCore
c:\code\myclient\DevOpsProject1\solution1
c:\code\myclient\DevOpsProject1\solution2
c:\code\myclient\DevOpsProject2\solutionx
etc
Matt Frear
  • 52,283
  • 12
  • 78
  • 86
  • I have edited [my answer](https://stackoverflow.com/a/73383408/6309) to address your edited question. – VonC Aug 17 '22 at 19:52

3 Answers3

2

You can use git bundle to bundle each of the repositories full history into one file (per repository)

And you can zip the dozen of bundle into a giant tar file.

Result: only one (big) file to copy, and to untar.

You can then clone back your repositories from their respective bundle (cloning them from their bundle file).


I don't care about copying all branches. I only care about keeping the directory structure of the repos

The, an alternative approach is to simply tar cpvf code.tar code under C:\.
Copy the giant tar file to the target machine, and tar xpvf code.tar: the directory structure will be preserved.

A bit as in here:

find . -name "*.git" -type d -exec tar -czf {}.tar.z {} \; -exec rm {} \;

(Be careful with the -exec rm part: test it out first).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for the edit, it's a good approach. I have kicked off the `tar` command now, will see how long it takes. I was thinking of an alternative approach, with a `find` command which would find all git repos, and output the folder name and the remote origin url to a text file. And then on the other machine, I would do some other script which would git clone the repo back to the same folder. I don't need all of the compiled code and npm modules etc to be copied over. – Matt Frear Aug 17 '22 at 21:20
  • @MattFrear Good idea. I have edited the answer to include a possible command following that approach. – VonC Aug 17 '22 at 21:26
1

What I was originally looking for:

find . -name .git -type d -execdir git clean -dxf \;

This cleaned up my 16Gb of files across all repositories down to 800Mb. And then @VonC's answer (N.B remove v option from the below to be quieter)

tar cpvf code.tar code

and then on the destination machine:

tar xpvf code.tar

Whenever I would use git in the copied folders, I then kept getting errors:

fatal: detected dubious ownership in repository

So to fix that I had to "Take ownership" of the folder. I'm on Windows, which meant Properties of the code folder -> Security -> Advanced -> Owner -> Change. Even though "Local administrators" owned the folder, and I am a member of that group, I had to make myself the owner.

An additional hurdle was because I'm on a corporate machine I couldn't see the Security tab. Here's the fix for that.

Matt Frear
  • 52,283
  • 12
  • 78
  • 86
-2

You clone only lasted commit from your remote repository like

git clone --depth 1 https://url-of-your-repo

hyphens2
  • 166
  • 6
  • 15
  • The question was about local repositories and migrating them to another drive, not cloning from origin repository – lumpidu Aug 22 '22 at 20:59