|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Set different user settings conditionally in Git |
| 4 | +categories: git |
| 5 | +--- |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +As you probably know, when using git you have to set your user name and email address before you can do any commits. |
| 12 | +Yoou can use `git config user.name "John Doe"` and `git config user.email "[email protected]"` to set these for a specific repository and `git config --global user.name "John Doe"` and `git config --global user.email "[email protected]"` to set these values for all repositories, but what if you want to use different values depending on the customer/folder? |
| 13 | + |
| 14 | +I use git both personally, using my own private Github email address, but also for work, where I mostly use my work e-mail address for commits to Azure DevOps. On top of that, I work for different customers where I usually end up with a dedicated user account in the customer's tenant. |
| 15 | + |
| 16 | +I want an easy way to sign my private commits using my private email address, work-related with my UPN and customer-specific with the user principal name of my account from that customer. |
| 17 | +Here's how I did that: |
| 18 | + |
| 19 | +## Use an easy folder structure |
| 20 | + |
| 21 | +These steps work best if you have different root directories for work related git repositories and private/public git repositories. |
| 22 | +In my case, I only use GitHub privately and Azure DevOps for work-related repo's, so that's easy. |
| 23 | + |
| 24 | +Therefore, my Git folder structure looks like this on a Windows laptop: |
| 25 | + |
| 26 | +- C:\Git |
| 27 | + - AzDevOps |
| 28 | + - CustomerA |
| 29 | + - Repo1 |
| 30 | + - Repo2 |
| 31 | + - CostomerB |
| 32 | + - Repo1 |
| 33 | + - GitHub |
| 34 | + - MarcoJanse |
| 35 | + - Repo1 |
| 36 | + - Repo2 |
| 37 | + - Repo3 |
| 38 | + |
| 39 | +## Set-up |
| 40 | + |
| 41 | +Because I primarily use non-work related git repositories, I use my GitHub user name and email address in my global config. |
| 42 | + |
| 43 | +### Basic commands |
| 44 | + |
| 45 | +Some useful commands for viewing and setting config |
| 46 | + |
| 47 | +- To view all config settings from a git repository |
| 48 | + - `git config --list` |
| 49 | +- To view settings defined in your global config |
| 50 | + - `git config --global --list` |
| 51 | +- To show where all the configuration is coming from in a git repository: |
| 52 | + - `git config --list --show-origin` |
| 53 | +- To set your global user name |
| 54 | + - `git config --global user.name John Doe` |
| 55 | +- To set your global email address |
| 56 | + - `git config --global user-email [email protected]` |
| 57 | +- To view your configured email adres from within a git repository |
| 58 | + - `git config --get user.email` |
| 59 | + |
| 60 | +### Creating the conditional includes file |
| 61 | + |
| 62 | +First, I create a file called `.gitconfig_work` and store this in my documents folder for safe keeping. |
| 63 | +You can name it whatever you want, but make sure it starts with a dot `(.)`. |
| 64 | +I created a Git folder in my OneDrive folder for this. |
| 65 | + |
| 66 | +In there, put the settings that apply only for your work-related repos. |
| 67 | + |
| 68 | +```bash |
| 69 | +[user] |
| 70 | + name = John Doe |
| 71 | + |
| 72 | +``` |
| 73 | + |
| 74 | +### Add the conditional include to your global git configuration |
| 75 | + |
| 76 | +Edit your global git config file via a code editor like VSCode. Mine is stored in `C:\Users\<username>\.gitconfig` |
| 77 | + |
| 78 | +Add the include at the end, to make sure the settings don't get overwritten again later in the same file. |
| 79 | + |
| 80 | +```bash |
| 81 | +[user] |
| 82 | + name = John Doe |
| 83 | + |
| 84 | + signingkey = FAKESIGNINGKEY001 |
| 85 | +[gpg] |
| 86 | + program = c:/Program Files (x86)/GnuPG/bin/gpg.exe |
| 87 | +[commit] |
| 88 | + gpgsign = true |
| 89 | +[includeIf "gitdir/i:C:/Git/AzDevOps/"] |
| 90 | + path = ~/OneDrive - CompanyName/Documents/Git/.gitconfig_work |
| 91 | +``` |
| 92 | + |
| 93 | +### Test the settings |
| 94 | + |
| 95 | +- Open a new shell and test your settings from within a git repository in AzDevOps folder: |
| 96 | + - `git config --get user.email` |
| 97 | + |
| 98 | +Depending on the folder and the level your're in, you should see different value. |
| 99 | + |
| 100 | +## Closing notes |
| 101 | + |
| 102 | +Hopefully this article helped you make git a little easier. |
0 commit comments