Multiple Git Accounts on One Computer

Search for a command to run...

No comments yet. Be the first to comment.
In this series, you can find useful Git commands, configuration and productivity tips.
There are times when creating an empty commit in Git is useful. A common scenario is the need to trigger a continuous integration pipeline or adding an important commit message that might have some effect. For example, GitVersion integrates with Git ...
This blog post is fourteenth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 14. This challenge was about optimizing an algorithm that generates exponentially larger and larger strings. I found ...

This blog post is thirteenth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 13. All solutions are in this GitHub repository. Solving Part One Given a list of x, y coordinates representing dots ...

This blog post is twelfth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described on Day 12. All solutions are in this GitHub repository. Solving Part One Given a set of connections between underground caves, ...

This blog post is eleventh in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 11. This day had many similarities to Day 9, and I solved the challenge using similar methods. All solutions are in thi...

This blog post is the tenth in the Advent of Code 2021 series and shows a JavaScript-based solution to the problem described in Day 10. All solutions are in this GitHub repository. Solving Part One Given a set of lines with opening and closing bracke...

You might need to use multiple Git accounts on a single computer. For example, you might have personal, work, and university accounts.
Most Git tutorials show you how to configure your global username and email which will be used when you make commits. Typically you set this right after installing Git and is your default user name and email.
git config --global user.name "YOUR NAME"
git config --global user.email "YOUR EMAIL"
You can set a custom user name and email for a specific Git repository by running this in the root of your Git repository:
git config user.name "YOUR NAME"
git config user.email "YOUR EMAIL"
In some cases, you might want to set a user name for a specific commit. You can configure a user name and email as part of the git commit command.
git -c "user.name=YOUR NAME" -c "user.email=YOUR EMAIL" commit -m"COMMIT MESSAGE"
I found an interesting solution on Stack Overflow for setting custom user names and emails based on which folder your Git repository is in on your computer.
You need to update the global ~/.gitconfig file. This file is specific to each user and is also the file where your global user and email are stored. Typically, on a Windows machine, this file is located at the root of your user folder (e.g. C:\Users\CristianaMan\.gitconfig). If you are unsure where this file is, you can run:
git config --list --show-origin
This command will show a list of all the configured Git properties and the corresponding file location.
Once you identified the file, notice it contains the following:
[user]
name = YOUR NAME
email = YOUR EMAIL
You can replace the user configuration with a snippet like the one in the example below which uses conditional includes. In this example, we would clone all personal repositories under a folder called personal and all work repositories under a folder called work.
In .gitconfig file:
[includeIf "gitdir:~/personal/"]
path = .gitconfig-personal
[includeIf "gitdir:~/work/"]
path = .gitconfig-work
Note: On Windows you might need to add the whole path (e.g.
[includeIf "gitdir:C:/Users/CristianaMan/Documents/personal"])
Create a .gitconfig-personal file in the same folder as your .gitconfig file and add
[user]
name = YOUR NAME
email = YOUR PERSONAL EMAIL
Create a .gitconfig-work file and add
[user]
name = YOUR NAME
email = YOUR WORK EMAIL
You can check if your user name and email are set correctly by cloning a project in the personal folder and in the root of that repository running git config --list --show-origin.