Skip to content

Git

For version control git is used at d-centralize. The additional git-lfs extension for versioning large files is also useful. Install both as:

Terminal window
sudo apt-get install git git-lfs

When performing git commands, always recurse submodules is a time saver:

Terminal window
git config --global submodule.recurse true

Setting up an SSH key

  1. You will have to register an SSH key for using GitLab with Git. You can generate one using the following command.

    Terminal window
    ssh-keygen -t rsa
  2. Copy your key from the pub file. You can view the pub file using the following command.

    Terminal window
    cat ~/.ssh/id_rsa.pub
  3. Login to dcentralized gitlab and sign in with your d-centralize account.

  4. Click on your user avatar and then go to Settings --> SSH Keys Public Keys. Then paste your public key and click Add key{.interpreted-text role=“guilabel”}.

Commit signing

It is recommended to also use the SSH key for commit signing to ensure that GitLab is able to verify that it is you committing your code. This can be done by following the steps in this guide.

  1. Configure Git to use SSH for commit signing

    Terminal window
    git config --global gpg.format ssh
  2. Specify which SSH key should be used as the signing key

    Terminal window
    git config --global user.signingkey ~/.ssh/id_rsa
  3. Tell Git to sign your commits automatically

    Terminal window
    git config --global commit.gpgsign true

If setup was successful, you’ll see this “verified” box in your commits:

image

gitignore

Most projects have this file in their root folder. It prevents adding files that probably should not be added that might get generated while running the project, like log files.

Files that are OS or editor specific are best kept in a global excludes file.

Tricks

Git aliases

An improved git log that provides you more visual branch context is very useful. Additionally, git iclean is interactive and lethal. To see what exactly these do and what the difference is between the two, see the Git clean docs. In case you’d like to use the aliases, add the following to your ~/.gitconfig:

[alias]
iclean = clean -idf
nuke = clean -idfx
lg = log --graph --abbrev-commit --decorate --date=relative --all

Git add

Remember, Git is a content tracker, not a file tracker. Therefore, it is possible to only commit a specific part of a file which could be useful when you have some changes that you don’t want to commit. To go through all unstaged changes and select individual hunks to commit, simply use:

Terminal window
git add -p

Git newline

Sometimes on Windows you might get errors regarding new lines in the codebase. This is due to the difference between the windows and Linux newlines. To fix it, run this code before cloning the repository:

Terminal window
git config --global core.autocrlf false

Pre-commit hook

How often did you push an MR only to find out the linter failed because of a small mistake. Using a pre-commit hook, you’ll find out before the commit. Just add the linters of your choice to this file .git/hooks/pre-commit in each of your git projects:

#!/usr/bin/env sh
set -e # exit on failures
pipenv run flake8 &
pipenv run black . &
pipenv run yamllint . &
wait

Make it executable afterwards:

Terminal window
chmod +x .git/hooks/pre-commit