Most of the developers are using Git as their version control system and sometimes it comes in handy to have Git set up and configured for multiple accounts for example work and personal configs and accounts.
In this post, we will see why and how to set up multiple configurations for a seamless workflow.
Tables of contents:
- Git installed and basic understanding
The main use case for this kind of setup is when you have a single machine on which you develop and code on work and personal projects where you usually don't have the same Git accounts for both of them.
You can start by installing Git if you haven't already, after that we will be creating two files for each account/config, by default none of these files are created.
Let's start by checking them (run command in your home directory
cd ~ && ls -la | grep .gitconfig
The above command should output no files.
I will be assuming that you have the following file structure for work and personal projects, but you can adapt as needed.
/home/nizar/ └───personal-projects │ └───my-cool-project │ │ index.html │ │ ... └───work-projects │ └───my-professional-project │ │ index.html │ │ ...
Now let's set our global config, this will create a file for the personal account:
git config --global user.name "nizar" git config --global user.email "[email protected]"
Let's check the personal config:
git config -l
You should be able to see what we have just set above.
Now let's do a little demo to verify our personal configuration:
As you can see I've created a directory for my personal project and initialized a Git repository within a project folder, looking at the Git log we can my personal info being applied.
Now since this is sorted out, Let's configure our work/professional account, but before we start I want you to take a look at the root directory for a file called
We will be modifying this file and be creating a new similar one.
[user] name = nizar email = [email protected] [includeIf "gitdir:/home/nizar/work-projects/"] path = /home/nizar/.gitconfig-work
As you noticed we added an
IF statement, telling Git if it is in our
work-projects directory (which we will be creating next) use the
.gitconfig-work configuration instead of the global one.
.gitconfig-work file next to the old one:
The content will be similar in structure, adapt as needed:
[user] name = nizar-work-name email = [email protected]
Again, let's do a demo of this work account/configuration:
Note: user and host names are a bit different, it was a mistake on my part, but the image should represent the point without a problem.
As you can see, the work configuration is applied in this directory instead of the global personal configuration.
Applying the above steps, it's safe to say that we are done with our multiple Git accounts setups.
As always, I hope you learned something.
Found this useful? feel free to share it with your friends.
Join the newsletter from here to notify you of new posts and updates.
Like the post? consider buying us a coffee ❤️.