I work on a single device and use it for both my office work and side/pet/personal projects. Default git credentials for the device is obviously the one provided by my office. Now, this scenario has happened to me a lot of times -
- My office work is done
- I am super excited about a personal project idea
- So I set up a remote git repository
- Set up the project boilerplate locally
git remote add,
git commit& PUSH!!!
And it is then when I realize I have pushed code to my personal git with wrong (office) credentials. And I start all over again.
Do let me know if you have ever faced a similar situation like mine in comments!
So, now what is the solution? How do I have separate git config on a single machine? The answer is pretty simple. You need to have two separate Git config files. But, how ? Read on...
*** Before starting make sure you have git version 2.13 or higher installed in your system. ***
$ git --version git version 2.14.2
Now with the prerequisites set, let's begin -
.gitconfigresides in your home directory. For Windows users, it will be in the path
C:\Users\[YOUR_NAME]. Go to that directory.
- Create two separate folders like My_Office_Workspace & My_Personal_Workspace.
- Create two separate
.gitconfigfiles, one for office and another for personal usage in the path
C:\Users\[YOUR_NAME]. Let's name them
The contents of
# This is Git's per-user configuration file. [user] name = YOUR_OFFICE_GIT_USERNAME email = firstname.lastname@example.org
The contents of
# This is Git's per-user configuration file. [user] name = YOUR_PERSONAL_GIT_USERNAME email = email@example.com
- After that's done, open up the
.gitconfigfile, and make the necessary changes. It should look like -
[includeIf "gitdir:~/My_Office_Workspace/"] path = .gitconfig-office [includeIf "gitdir:~/My_Personal_Workspace/"] path = .gitconfig-personal
Configuration done! Now let's verify.
- Goto My_Office_Workspace and clone a project from your office GitHub, BitBucket or GitLab. Let's call the project My_Office_Project and do the following -
$ cd My_Office_Workspace/ $ git clone https://github.my_office.com/VeryImportant/My_Office_Project.git $ cd My_Office_Project/ $ git config user.name YOUR_OFFICE_GIT_USERNAME
- You can also do the same for your personal projects under My_Perosonal_Workspace, but you will see you the output for the last command
git config user.nameis YOUR_PERSONAL_GIT_USERNAME.
Woohoo, it's done! No more messing up. 😃
If your projects are in a different drive on Windows, for eg. D or F, then you can configure the
.gitconfig like this
[includeIf "gitdir:D:/My_Office_Workspace/"] path = .gitconfig-office [includeIf "gitdir:F:/My_Personal_Workspace/"] path = .gitconfig-personal
Liked this post? Share it ↩️ or maybe give a heart 🧡.