EXPEDIA GROUP TECHNOLOGY — ENGINEERING

The easy way to get your Gits together

Decorative cartoon of cat with octopus tentacles surrounded by many filing cabinets
Image generated with OpenAI’s DALL•E 2

A grove of Gits doesn’t make you weird

Managing these multiple configurations efficiently is painful without a carefully-considered solution.

Why multiple Git accounts are hard to manage

  • Identity: Information about you attached to a commit inside a repo. This is managed by the Git application.
  • Authentication: Verifying that you are in fact you before you are allowed to interact with a repo. This is managed by your local computer and the service hosting your remote repos.

Your identity in Git

  • User name
  • Email address
  • GPG signing key
[merge]
ff = false
[pull]
ff = only
[core]
editor = vim
[commit]
gpgsign = true
[user]
name = Russell Brown
email = r***@***.com
signingkey = A0***9F

Conditional configuration

$HOME
+- git
+- work
+- oss
+- personal
[merge]
ff = false
[pull]
ff = only
[core]
editor = vim

# The trailing slashes below are required to match
# any subdirectory and not just the exact path

[includeIf "gitdir:~/git/work/"]
path = ~/.gitconfig.work
[includeIf "gitdir:~/git/oss/"]
path = ~/.gitconfig.oss
[includeIf "gitdir:~/git/personal/"]
path = ~/.gitconfig.personal
[user]
name = Russell Brown
email = r***@***.com
signingkey = A0***9F
[commit]
gpgsign = true
[merge]
ff = false
[pull]
ff = only
[core]
editor = vim
[user]
name = Russell Brown
email = r***@***.com
signingkey = A0***9F
[commit]
gpgsign = true
Diagram illustrating the relationship between config files and Git settings as described in the text and examples
How directory can be mapped to .gitconfig files per identity and used in the git command

Hosted Git

Authenticating to GitHub

  • The secret key is stored on your computer
  • Its corresponding public key is stored in the remote

Choosing an SSH key

Host personal
Hostname github.com
User rcbrown
Host *
IdentityFile ~/.ssh/id_rsa
Hostname github.com
User rcbrown
IdentityFile ~/.ssh/id_rsa
Host work
Hostname github.myemployer.com
User rbrown
Host personal
Hostname github.com
User rcbrown
Host *
IdentityFile ~/.ssh/id_rsa
Host work
Hostname github.myemployer.com
User rbrown
IdentityFile ~/.ssh/id_rsa_work
Host personal
Hostname github.com
User rcbrown
IdentityFile ~/.ssh/id_rsa_personal
Host *
IdentityFile ~/.ssh/id_rsa
Host work
Hostname github.myemployer.com
User rbrown
IdentityFile ~/.ssh/id_rsa_work
Host personal
Hostname github.com
User rcbrown
IdentityFile ~/.ssh/id_rsa_personal
Host oss
Hostname github.com
User open_rcbrown
IdentityFile ~/.ssh/id_rsa_oss
Host *
IdentityFile ~/.ssh/id_rsa

A red herring

  • GitHub makes it easy to copy the clone URL to the clipboard, but you will have to modify the hostname in the command line that you must actually run. This is easy to forget. It’s even more annoying if you are forking a repo and maintaining an upstream.
  • It’s a hierarchy parallel to your GitHub identity; adding a new account requires updating both .gitconfig and ~/.ssh/config.
  • Building on the last point, because separate directories are required to vary the Git identity configuration, the SSH server name must be set in a hostname that will be used only under the directory that provides the correct identity for that host. In other words, for this to work right, every repo needs both to have the right name and to be in the right place.
Diagram illustrating the relationship between config files and Git settings as described in the text and examples
Using directory to supply identity to git while using repo hostname to find the correct SSH key

Rescued by sshCommand

  • You can tell Git what SSH command to use in a config file by using the sshCommand setting.
  • Since you know how to tell Git to use specific configs per directory, you can configure an SSH command per directory.
  • You can construct an SSH command that uses a specific identity with the -i option.
[user]
name = Russell Brown
email = r***@***.com
signingkey = A0***9F
[commit]
gpgsign = true
[core]
sshCommand = "ssh -i ~/.ssh/id_rsa_work
Diagram illustrating the relationship between config files and Git settings as described in the text and examples
Unified configuration that determines both identity and SSH key from directory

Alternatives

Learn more about technology at Expedia Group

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store