Different Github-user per project
If you want to use a different Github-user depending on the project you’re currently working on, the changes required are quite small and quickly applied. The main requirement to define a Github-user in project-scope is to correctly define the config-file in the .git-directory.
Updating the config file
This can be done either manually by directly modifying the file or by using the git-CLI. In the following example, I’ll show you how to use the CLI. Note that every project that uses git as its version management tool has such a directory as well as the config-file. What exactly it does is out of scope of this article, so let’s focus on the scoped user-config for now.
Before running the commands, please make sure you're in the root of the project.
// Note: the quotation marks have to be included!
git config --local user.name "user-name"
git config --local user.email "[email protected]"
The “--local”-flag makes the difference
The main difference when setting both username as well as email is to use the “--local”-flag. This will update the “config” in the hidden .git-directory.
On macOS, you can use the Keyboard-shortcut "Shift" + "Command" + "period". On Windows, simply select the checkbox to show all files in the Explorer.
// Example of some of the contents of the 'config'-file
// after running the above commands.
//
// The snippet below only shows a slice of a given
// config file and focus on the relevant changes.
[core]
repositoryformatversion = 0
filemode = true
[user]
name = user-name
email = [email protected]
// As you can see, "[user]" got added to the file.
// This now informs git regarding the user in
// this local clone of the repo.
Conclusion
And we’ve already reached the end of this article. As you can see, using a different user for each project is really only a small change required, but helps you avoiding to switch accounts every time you switch projects.