Skip to content

Git


title: Git: Version control and repository organization description: Using git for version control and organizing larger projects. published: true date: 2024-01-11T20:11:39.892Z tags: repository, git, control, version, organization, repo editor: markdown dateCreated: 2023-10-12T23:25:23.141Z


Git

git is a version control system for computer files, that helps keep track of changes mad by different users while keeping everything updated.

We use git to keep track of (almost) everything, including this wiki!

We also use the GitLab Hook (bot) on discord to keep track of this as well!

captura_desde_2024-01-11_17-03-25.png

Some of OLA's files live on other cloud services (e.g. Google Drive), and are updated on GitLab every now and then. This is because git has a rather steep learning curve, and many in the community do not really need to use it.

Here you'll find only the most relevant bits of git that we use.

Repositories

We use GitLab.com as a central host for OLA's repositories. Each person can have a perfect and full copy of the same repository, because git is a distributed version control system.

A GitLab project also has its own copy of the repository, but it is special because that one can be easily accessed by everyone, and used to share new changes (push) and download (pull) changes from others.

Submodules

A git repository can include other repositories as submodules, as shown below:

captura_desde_2024-01-11_17-04-54.png

In this case, git will only keep track of which particular version (i.e. which commit) of a repository it includes.

Creating a repo from a folder already in a git directory

Change directory to the original repository.

cd old/repo/path

Use subtree to a create a new branch tracking only the newt directory.

git subtree split -P code/newt -b newt

Create a new repository at an empty directory (e.g. new/repo/path/).

cd new/repo/path/
git init

Pull the newt branch from the original directory.

git pull old/repo/path newt

Add a new remote (e.g. on GitLab).

git remote add origin ssh://git@gitlab.com/pipettin-bot/pipettin-newt
git push -u origin master

Add a new remote (e.g. on GitLab, named origin) and push the new repo to the remote, on its master branch.

## Note that, on GitLab, you can use push to create a repo if it did not exist yet.
git push -u origin master

You may now want go back to the original repo and remove the files of the new repo, which still remain and are not automatically deleted:

cd old/repo/path
git rm -r code/newt

## Commit and push the changes.
git commit -m "remove original submodule files"
git push

Adding a repo as a submodule

Go back to your original repo:

cd new/repo/path/

Commit and push the changes:

## Note: you might need to delete or rename the original "code/newt" directory first.
git submodule add ssh://git@gitlab.com/pipettin-bot/pipettin-newt code/newt

## Commit and push the changes.
git commit -m "add newt as a submodule"
git push

Updating all submodules in a repo

Recursively so! :)

## From: https://stackoverflow.com/a/1032863
git submodule foreach git pull origin master

Note: you may want to setup every submodule with an HTTPs connection. In this way, you can update everything using one command, without needing an SSH key setup.

Where is the submodule's .git/config?

Your submodules will no longer contain a .git directory, but will contain a git file (with the same name) pointing to the parent repo's main directory.

The configs of your submodules live at the .git/modules directory of the parent repo.

cd path/to/main/repo

cd .git/modules/

cat your/submodule/directory/config

You can change the config details manually over there (e.g. changing SSH to HTTPs).

LFS

Use the SSH agent

If you are using SSH to pull or push changes with an encrypted SSH private key, the ssh agent may make things simpler for you.

This is specially usefull when paired with LFS, which may ask for you key's password several times.

To set it up only temporarily for a new terminal, run the following:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa # Adjust to match your key's path.

Cleanup

To list large files: https://stackoverflow.com/a/42544963/11524079

git rev-list --objects --all |
  git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' |
  sed -n 's/^blob //p' |
  sort --numeric-sort --key=2 |
  cut -c 1-12,41- |
  $(command -v gnumfmt || echo numfmt) --field=2 --to=iec-i --suffix=B --padding=7 --round=nearest

Follow the cleanup GitLab repo guide: https://docs.gitlab.com/ee/user/project/repository/reducing_the_repo_size_using_git.html#purge-files-from-repository-history

Critical

Always backup the commit-map file after runing each git filter-repo command.

Example filter-repo commands:

git filter-repo --invert-paths --path models/modelos_toolchanger/pipes_toolchanger/Documentacion_de_ToolChanger.docx
git filter-repo --invert-paths --path doc/models/modelos_toolchanger/pipes_toolchanger/Documentacion_de_ToolChanger.docx
git filter-repo --invert-paths --path "models/modelos_XYZS/thingiverse_belt_locks/modelos thingiverse/CR-10+Mod+-+X+axis+Tensioner.zip"
git filter-repo --invert-paths --path "models/modelos_XYZS/old_models/thingiverse_belt_locks/modelos thingiverse/CR-10+Mod+-+X+axis+Tensioner.zip"
git filter-repo --path-glob '*.stl' --invert-paths