Conditional git configs
In this blog post we will learn how to automatically change git configs based on the directory you are in.
When you initially setup git, you are asked to setup your name and email like this:
When you execute the above commands, git stores the name and email in a file called ~/.gitconfig
. This is a global git config file and it is used for all the git repositories on your system.
If you have to use multiple git accounts, say one work account and one personal account. You have to manually update the config locally for each repository by executing:
This can be a bit cumbersome if you have to do this for multiple repositories. And you might forget to change the config and commit with the wrong credentials.
This is where conditional git configs come in. You can set up git to automatically change the config based on the directory you are in.
How to set up conditional git configs
If you have a habit of managing your dotfiles and keep .gitconfig
in your dotfiles repository, you don't want to accidentally commit your work email and other details to your public dotfiles repository.
So we will add the configs to a separate file ~/.gitprofile
which will be included in the main .gitconfig
file.
Make sure you DON'T commit the ~/.gitprofile
file to your public dotfiles repository.
Create a ~/.gitprofile
file
Edit your ~/.gitconfig
file and add the following lines:
This will tell git to include the ~/.gitprofile
file in the main .gitconfig
file. If you have the ~/.gitprofile
file, it will be included in the main .gitconfig
file.
If you don't have the ~/.gitprofile
file, git will ignore it.
Here's an example of how the gitconfig
will look like https://github.com/sagarPakhrin/.dotfiles/blob/master/git/.gitconfig
Now in our gitprofile file let's add a default profile.
Configure work profile
Now let's say you keep all your work repositories in a directory called ~/work
.
Let's create a work profile inside the work directory.
Edit the ~/work/.gitconfig
file and add the following lines:
Now we have to tell git to use the ~/work/.gitconfig
file when we are in the ~/work
directory.
Edit the ~/.gitprofile
file and add the following lines:
So your final ~/.gitprofile
file will look something like this:
Now when you setup a new repository in the ~/work
directory, git will automatically use the ~/work/.gitconfig
file and use the work email and name.
Conclusion
You can automatically switch between different git profiles based on the directory you are in using git conditional config.