git config --global --unset user.name
git config --global --unset user.email
git config --global --unset user.signingkey
git config --global user.useConfigOnly true
git config --global user.<id>.name "<name>"
git config --global user.<id>.email "<email>"
git config --global alias.identity '! git config user.name "$(git config user.$1.name)"; git config user.email "$(git config user.$1.email)"; :'
So given that I have created two users, e.g. personal and work I run: git identity work
in repos that need the work name/e-mail, and git identity personal
in the ones that are private.On ~/.gitconfig
[user]
name = personal
email = personal@example.com
[includeIF "gitdir:~/workspace/"]
path = ~/work.gitconfig
Then on ~/work.gitconfig [user]
name = workname
email = work@example.com
[1] https://git-scm.com/docs/git-config#_conditional_includesIf you don't use this in any nested envrc files, the settings don't carry over which means your git settings are not maintained. It is kind of an escape hatch as the parent files are not verified in the same way normal envrc files are, but I have found the trade off to be worth it
$ git whoami
GIT_COMMITTER_IDENT=Mel Smith <mel@example.org>
GIT_AUTHOR_IDENT=Mel Smith <mel@example.org>
`whoami` can be implemented via a simple alias: git config --global alias.whoami = "! git var -l | grep '^GIT_.*_IDENT'"But: does this edit a file that can appear in .gitignore or are you uploading this to affect all your collaborators?
Why `; :` at the end?
$ git config alias.foo '! echo "$1";'
$ git foo bar
bar
echo "$1";: bar: command not found
Whereas if I end with `; :` then it works as expected: $ git config alias.foo '! echo "$1"; :'
$ git foo bar
bar
It seems to execute the last argument (`bar`) as a command without the `:` at the end, and I don't have a `bar` command on my PATH so it angrily fails with exit code 127. If it instead executes `:` however, that will make it happily exit with 0.It seems to be a Git alias quirk, but I may be incorrect here.
What I have been doing by hand for some time is putting code for different customers in different directories and having a conditional in `~/.gitconfig` to determine what config applies there:
[includeIf "gitdir:~/projects-private/**"]
path = ./.gitconfig-private
[includeIf "gitdir:~/projects-client/**"]
path = ./.gitconfig-work
Then in .gitconfig-private or .gitconfig-work I have all the usual gitconfig settings that apply, for example the [user] section...Switching to the right directory thus automatically changes the settings.
However, your .gitconfig setup is (for me) way nicer as I already have things split up by GitHub organization, and now my identity can change without having to do anything at all.
So thanks for sharing that, had no idea it was possible.
I'd still think the cli could be useful in some situations. but I agree with others about how big this had to be because I used node.
various permutations of which were posted an hour ago and are currently higher than this comment, so I’m confused why multiple people asked to see them ~30½ minutes ago
#!/usr/bin/env zsh
case $1 in
foo)
git config user.name 'John Doe'
git config user.email john@example.com
git config user.signingKey 0xFFFFFFFF
;;
bar)
git config user.name 'Jane Doe'
git config user.email jane@example.com
git config user.signingKey 0xEEEEEEEE
;;
baz)
git config user.name 'My Dog'
git config user.email dog@example.com
git config user.signingKey 0xDDDDDDDD
;;
*)
echo "usage: git-user foo|bar|baz" >&2
exit 1
;;
esac
Done.Btw probably want to add signingKey support.
[user]
name = Gurjeet Singh
# Tell Git to _not_ guess my name and email based on `whoami` and `hostname`
useConfigOnly = true
With this in place, whenever I try to commit for the first time in a repository, Git prompts me *** Please tell me who you are.*
I then add an email address to the repo-local config based on whether it's work or personal project. git config user.email me@example.comFor my work repo I configure the repo to use my work identity. For my personal repos I use my personal identity.
I do not need to switch identities when using the same repo.
Doing this, you only setup your identity once per repo not every time, which is safer/less error prone. i.e.: you wont leak your work email on github by accident.
const store = require("data-store")({ path: process.cwd() + "/data.json" });
I think a `git-user-data.json` file in a centralized location makes far more sense.So I made this as a sugar for git config user.email whatever@gmail.com
Is a lot easier then what you came up with.