Jalibu
Jalibu

Reputation: 21

How to clone/connect a remote GIT repository without initial loading files and history?

I want to prepare a development VM for our project that contains all required stuff for the developers (such as IDEs, Browsers, Tools, Bookmarks, GIT repositories, etc)

As this is an already long ongoing project, the size of the repositories grew up to > 3 GB and as we're serving micro services, not every developer needs to have all of them.

To keep the initial VM file size small, I want to know how I could "prepare" the VM with configured git repositories (so that the developers could easily get their code by "git fetch && git checkout xy") without downloding all the code in advance.

Upvotes: 1

Views: 163

Answers (3)

sergej
sergej

Reputation: 17999

You can shallow clone the repo and skip checking out files:

git clone --depth=1 --no-checkout <url/repo.git>

Test

  1. Normal clone

    git clone https://github.com/Microsoft/vscode.git
    du -h --max-depth=0 vscode/.git
    

    Result: 162M

  2. Shallow clone

    git clone --depth 1 --no-checkout https://github.com/Microsoft/vscode.git
    du -h --max-depth=0 vscode/.git
    

    Result: 11M

Upvotes: 0

ElpieKay
ElpieKay

Reputation: 30888

  1. Make a mirror clone of the repository under $MIRROR. $MIRROR could be /opt/ for example. git clone <remote_repo> --mirror -- /opt/mirror_repo. $MIRROR could also be located in a mounted device.

  2. Every user makes a clone by using /opt/mirror_repo as a reference. cd /home/userA/;git clone <remote_repo> --reference=/opt/mirror_repo, cd /home/userB/;git clone <remote_repo> --reference=/opt/mirror_repo.

  3. Train the developers to use git worktree to checkout different revisions to different working trees instead of making new clones. If they insist on new clones, tell them to always add --reference=/opt/mirror_repo.

  4. Run a scheduled task to update /opt/mirror_repo regularly, at 00:00 everyday for example. git --git-dir=/opt/mirror_repo fetch.

A reference repo can reduce network and local storage costs.

Upvotes: 0

Yogesh_D
Yogesh_D

Reputation: 18809

You can just create all the repo folders and setup the remote. Here's an example:

C:\Users\yogesh\Desktop>md test123
C:\Users\yogesh\Desktop>cd test123
C:\Users\yogesh\Desktop\test123>git init
Initialized empty Git repository in C:/Users/yogesh/Desktop/test123/.git/
C:\Users\yogesh\Desktop\test123>git remote add origin https://github.com/org1/repo1.git

Do this for all the repos. This should just create an empty directory with a git repo configured to point to actual remote repos. Once a dev needs to setup the repo they can just do:

git fetch
git checkout <branch>

Upvotes: 2

Related Questions