Skip to content

Instantly share code, notes, and snippets.

@jackblk
Last active February 10, 2024 17:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackblk/269fed351c9e6202b4c9ad09c314a3d3 to your computer and use it in GitHub Desktop.
Save jackblk/269fed351c9e6202b4c9ad09c314a3d3 to your computer and use it in GitHub Desktop.
Git automatically config for work email and personal email
[includeIf "gitdir/i:github/"]
path = .gitconfig-github
[includeIf "gitdir/i:work/"]
path = .gitconfig-work'
[user]
name = John Smith
email = john@personal.com
[user]
name = John Smith
email = john@work.com

Intro

This guide will help git to automatically use work email for any repo in work folder, and personal email for any repo in github folder.

Setup

Edit global git config file

Edit ~/.gitconfig by text editor or just vi ~/.gitconfig if you know how to use vim.

  • Add these lines:
[includeIf "gitdir/i:github/"]
  path = .gitconfig-github
[includeIf "gitdir/i:work/"]
  path = .gitconfig-work'
  • Remove email key in [user] section.

Optional:

  • Remove name key, if you want to config your name differently.
  • Remove /i to make it case sensitive. Example: [includeIf "gitdir:github/"]

Create personal & work config

Create a file at ~/.gitconfig-github: touch ~/.gitconfig-github.

Edit the file: vi ~/.gitconfig-github

[user]
  email = john@personal.com
  name = John Smith

Remove the name key if you already set it up in global config.

Do the same for work config file ~/.gitconfig-work.

Now all repo under folder work will use work email, all repo under folder github will use personal email.

References

Git tip

Git config documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment