Skip to content

Git Configuration

This page documents general configuration for Git and it's related SSH settings. It also addresses setting up configuration for supporting different SSH keys for different repositories.

Global Configuration

In folder /home/{username}/.config/git, you have a global config file and different files for each Git server (e.g. GitHub, GitLab etc.). The global config file will have paths to the different files for each Git server.

Example Global config File

/home/{username}/.config/git/config
[includeIf "gitdir:~/code/repos/myothergitlab/"]
    path = ~/.config/git/gitconfig.myothergitlab

[includeIf "gitdir:~/code/repos/gitlab/"]
    path = ~/.config/git/gitconfig.gitlab

[includeIf "gitdir:~/code/repos/github/"]
    path = ~/.config/git/gitconfig.github

[includeIf "gitdir:~/code/repos/mylocalgitlab/"]
    path = ~/.config/git/gitconfig.mylocalgitlab

Example Repo Config File

From the above global config file, the paths point to config files for the different repositories. Below is an example of one.

/home/{username}/.config/git/gitconfig.gitlab
[init]
    defaultBranch = main
[user]
    name = yourgitusername
    email = yourgitusername@email.com
[credential]
    helper = /usr/lib/git-core/git-credential-libsecret

SSH Keys Configuration

/home/{username}/.ssh/config
AddKeysToAgent yes

# I think this is just for SSH connection to a Linux server
Host {Your internal linux server you want to connect to}
  HostName {Your internal linux server you want to connect to}
  User {Your internal linux username on server}

# Public Gitlab
Host gitlab.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/{your public key}

# Internal Gitlab at http://{IP Address}/
Host {IP Address}
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/{your public key}

# Internal Gitlab at https://hostname_domain/
Host hostname_domain
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/{your public key}

SSH Agent Setup

So that you don't have to enter SSH passwords every time with Git, you can enable ssh-agent.

A handy ssh-agent.service is included in openssh since the version 9.4p1-3, which can be enabled as a user unit.

systemctl --user start ssh-agent
systemctl --user enable ssh-agent

Note

Make sure to add the config setting AddKeysToAgent yes as showing in the above SSH Keys config file.

References

ArchWiki SSH Agents