devz-docz

Aggregation of onboarding and general devz standards that I have gatherd over my career.

View on GitHub

Source Control / Git Repos

This page provides guidance on how to set up and manage your Git repos. For I, most of these will be kept in gitlab, so much of this advice will be geared specifically to managing gitlab repositories.

Setting Up Repositories

I try to manage my Git repositories with Terraform as much as possible. However, I haven’t had great luck creating repos with Terraform, so what I do is create the repo manually in gitlab, then import it into Terraform. For an example of how I’ve done that, and what Terraform code for gitlab repositories looks like, you can look in the legendary-waddle repo.

Repo Maintenance and Hygiene

Locally, you can add an alias to your ~/.gitconfig file. Example:

  [alias]
    fresh = "!git co main && git pull && git branch --merged | grep -v '\\*' | xargs -n 1 git branch -D"

Then instead of doing the typical git checkout main && git pull, you would only run git fresh.

gitlab Repo Settings

Splitting Out Code to a New Repository

If you need to split out code from one of your repositories into its own separate repo, you can follow these steps to make sure you’re preserving the commit history when doing so.

The filter-branch subcommand to git can be used to accomplish this, but the documentation strongly discourages its use. Instead, a third-party utility called git-filter-repo is recommended.

First, create a new repo using the gitlab UI as you would normally.

In your terminal, clone a copy of the original repo into a new folder:

git clone git@gitlab.com:OWNER/REPONAME.git NEWREPO && cd NEWREPO

Filter out commits that change the specified directory:

git-filter-repo --subdirectory-filter DIRNAME

The origin remote will be removed in this process. Add it back with the new repo URL, and push the filtered work tree:

git remote add origin git@gitlab.com:OWNER/NEWREPO.git
git push --set-upstream origin <main>

Note: This repo should be public and properly licensed. I has a :lock:decision record on what licenses should be applied to which sorts of projects.

If your project is managing its gitlab repositories with Terraform as I suggest, make sure you add the repo to the Terraform code and import it from gitlab. Speak to your project’s infrasec team if you need help with this.