I maintain a handful of git accounts at GitHub.com and on private git servers, and have repeated committed to a project using the wrong personality.
My early attempts to avoid this mistake involved scripts to set per-project git parameters, but I've found a more streamlined option.
The approach revolves around the file hierarchy in my home directory: Rather than dumping everything in a single ~/projects directory, they're now in ~/projects/personal, ~/projects/work, etc...
Whenever cloning a new project, or starting a new one, as long as I put it in the appropriate directory, git will chose the behaviors and identity appropriate for that project.
Here's how it works, with 'personal' and 'work' accounts at GitHub.com
1. Generate an SSH key for each account
ssh-keygen -t ed25519 -P '' -f ~/.ssh/work.github.com
ssh-keygen -t ed25519 -P '' -f ~/.ssh/personal.github.com
2. Add each public key to its respective GitHub account.
3. Create the project directories and .gitconfig files
mkdir -p ~/projects/{personal,work}
touch ~/projects/{personal,work}/.gitconfig
4. Set up a .gitconfig file in each directory
[user]
email = chris@work.domain
[core]
sshCommand = "ssh -i ~/.ssh/work.github.com"
5. Conditionally include project gitconfig from main gitconfig
[init]
defaultBranch = main
[user]
name = Chris Marget
[core]
excludesFile = ~/.gitignore
[includeIf "gitdir:~/projects/work/"]
path = ~/projects/work/.gitconfig
[includeIf "gitdir:~/projects/personal/"]
path = ~/projects/personal/.gitconfig