Updated 2023-03-10: The regular git-pre-commit-format hook script would not work with submodules properly. I have fixed this in my version, and it is uploaded to my repository. I have updated the link to point to this version, which is still 100% based on the original from barisione.

Setting up your git environment in Linux may seem straight-forward and not a big deal; and while getting git installed and running is super easy, there are some tricks that will certainly make your life easier as a developer!

Install git

Every package manager for Linux that I am aware of is going to have a git package you can install. Depending on the package maintainers this will usually be a fairly recent version of git (v2.36.1 is the latest release as of this writing), but anything 2.26 or newer should be just fine.

# APT Package Manager (Debian/Ubuntu/etc.)
sudo apt install git

# YUM Package Manager (RedHat/Fedora/CentOS/etc.)
sudo yum install git

# APK Package Manager (Alpine)
apk add git
Code language: Bash (bash)

Basic git Configuration

First off, you need to setup your basic git configuration, such as your name and your email address. This can be done on a per repository basis or globally. I typically have my work email setup globally and then configure a different email address for other repositories as required (i.e., my open source projects on GitHub).

# Global Configuration
git config --global user.name "Your Name"
git config --global user.email "your_email@work.com"

# Per Repository Configuration
git config user.email "your_email@personal.com"

# Or your GitHub no-reply email address
git config user.email "YourID+username@users.noreply.github.com"
Code language: Bash (bash)

Storage Folders and Hooks

I prefer to keep all my git repositories in one location, typically in my home directory somewhere like ~/git. When working in Windows (under WSL) I make sure that I clone my repositories inside the WSL environment, otherwise when working with the source (building, editing, etc.) there can be performance losses in the interaction between WSL and Windows.

One of the first things I do when setting up my git environment is make sure I am setup with all the hooks I will need. As a C/C++ developer primarily, there really are only two that are a must have for me – my pre-commit format hook and my Conventional Commits hook (with Commitizen). To make setup of these easier, I wrote a script to easily install these hooks, which you can find here.

mkdir -p ~/git/_globalhooks && cd ~/git/_globalhooks
wget https://raw.githubusercontent.com/jhaws1982/git-hook-installer/master/git-hook-installer.sh

chmod a+x git-hook-installer.sh
Code language: Bash (bash)

Auto Code-formatting Before Committing

This is very useful to enforce a particular style for your repository.

First, make sure clang-format is installed:

sudo apt install clang-format
Code language: Bash (bash)

Pull the necessary scripts into your _globalhooks directory:

wget https://raw.githubusercontent.com/barisione/clang-format-hooks/master/apply-format
wget https://raw.githubusercontent.com/jhaws1982/git-hook-installer/master/git-pre-commit-format

chmod a+x apply-format
chmod a+x git-pre-commit-format
Code language: Bash (bash)

Finally, install the hook:

./git-hook-installer.sh git-pre-commit-format pre-commit <path-to-repository>
Code language: Bash (bash)

Now, every time you commit, your source code will be checked for proper formatting and fixed automatically if you approve.

Conventional Commit Enforcement

By setting up a git hook to walk you through writing a proper commit log, you can make sure that your history is very readable! Commitizen makes this super easy and you can customize the template as you see fit. I prefer to follow Conventional Commits, and the setup for that is described here:

# Install npm, commitizen, and template for Conventional Commits
sudo apt install npm
npm install -g commitizen
npm install -g cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc

# Create the git hook
cat >> ~/git/_globalhooks/commitizen-commit-msg
#!/bin/bash
exec < /dev/tty && node_modules/.bin/cz --hook || true
^EOF
Code language: Bash (bash)

Finally, install the hook:

./git-hook-installer.sh commitizen-commit-msg prepare-commit-msg <path-to-repository>
Code language: Bash (bash)

Global Hooks

With the hooks installed in the ~/git/_globalhooks directory, you can easily copy the hooks to a new machine. This enables you to get setup rather quickly. You can also use these hooks globally for all repositories if you so choose. I usually don’t because most repos don’t follow Conventional Commits or the same formatting rules. To apply hooks globally, specify the global flag to the script when installing the commit. This will set up the hook in a “global” folder of your choosing (I would choose ~/git/_globalhooks). It also adds the necessary configuration line to your git config to specify the path as the hookspath.

./git-hook-installer.sh -g git-pre-commit-format pre-commit ~/git/_globalhooks/

git config --list
    core.hookspath=$HOME/git/_globalhooks/
Code language: Bash (bash)

Hopefully some of the tips I have found will help you out as you setup your environment from scratch or just update it with new settings!

Last modified: March 29, 2023

Author

Comments

Write a Reply or Comment

Your email address will not be published.