Switching multiple roles and projects is a common challenge. Whether it’s managing personal coding ventures alongside work projects, or constantly switching between personal and professional repositories and emails, it can feel like a never-ending cycle.
Understanding Git Configuration
Remember setting up your global username and email with:
# You might have already set this up
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
These details reside in .gitconfig
, typically in your home directory : Windows: C:/Users/YourUsername/.gitconfig Unix System: ~/.gitconfig
Here’s a sample of what your .gitconfig might look like:
[user]
name = Anup Raj Rijal
email = [email protected]
That’s your default Git identity. But there’s more to it. We will talk more in depth about .gitconfig on upcoming content.
Step-by-Step Guide
1. Find Your Git Config File:
Your .gitconfig file is the key to your Git kingdom! On Unix-based systems, you’ll find it at ~/.gitconfig. For Windows, it’s usually at “C:/Users/YourUsername/.gitconfig”.
2. Organize Your Projects
Create a dedicated directory for your work, like “/work”, and organize your projects into subdirectories. This helps keep everything neat and tidy.
mkdir -p work/abc_company
You have the flexibility to create the directory in any location of your choice. Simply ensure to specify the directory path in the .gitconfig file provided below.
3. Configure Your Git Settings
Open your .gitconfig file in a text editor and add conditional configurations for each work directory:
For Unix Based System:
[user]
name = Anup Raj Rijal
email = [email protected]
[includeIf "gitdir:~/work/abc_company/"]
[user]
name = Your Name
email = [email protected]
For Windows:
[user]
name = Anup Raj Rijal
email = [email protected]
[includeIf "gitdir:C:/Users/YourUsername/work/abc_company/"]
[user]
name = Your Name
email = [email protected]
Replace “abc_company” with your project directory and [email protected] with the corresponding email address. This tells Git to use specific settings for each project.
4. Save your changes
Once you’ve made your updates, save the file. You’ve just mastered multiple email addresses in Git. Now you can initialize the git repo inside the “abc_company” folder, make a commit, and see the git log showing your company email.
Here is your updated .gitconfig file.
For Specific Repositories
If you want to change the email and name for a specific repository only, you can easily do so using Git’s conditional configuration. Here’s the command:
# First cd to your repository
git config --local user.name "Your Name"
git config --local user.email "[email protected]
This command sets the name and email configuration locally for the current repository only, overriding any global configurations. You can replace “Your Name” with the desired name and “[email protected]” with the desired email address.
Recap and What’s Next
You’ve done a great job by adding different email addresses to your Git workflow. Now, each project can really have its own personality, which makes it easy to keep track of and organize. In the next blog, we’ll explore using different emails alongside SSH keys to enhance security and streamline authentication. Stay tuned for our comprehensive guide! Your feedback is valuable as we journey towards mastering Git together.
Originally Published on Medium