I find it much easier to use direnv and set GIT_AUTHOR_EMAIL in each of ~/work/.envrc and ~/personal/.envrc No need to reconfigure every repo this way.
Just a note of caution, when this feature was added tools based on libgit2 did not understand it. I just checked now, libgit2 eventually added support (though it took 5 months since I filed the ticket). But if you're using any tools that have a 3-year-old version of libgit2 they might not understand it.
I do the same. Also remember the following keyword if you use nested .envrc files: source_up
If 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
Nice! I was thinking of something like this recently, but decided instead to completely divorce personal and work projects. It’s nice to close the work laptop and go open the personal laptop knowing it’s impossible to cross the streams, as well as for the ritual.
I'm not entirely sure, but `:` means "true" in bash, and if I omit it, something like this happens:
$ 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.
Huh I had no idea $1/$2/etc worked as-is in an alias. The advice I learned years ago for dealing with any alias that needs to do something custom with parameters is to write it like
git config alias.foo '!f() { actual command goes here }; f'
as that will pass all the args to the shell function. But if git is already setting it up so the args work then suffixing the alias with ";:" seems simpler.
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:
The CLI tool looks very great indeed and handy when you keep all the projects in the same top-level directory, or need to be able to change identity while in the same repository.
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 think a tool that has a similar UX as your is handy if a person don't care or want to memorise the gitconfig documentation to figure this out. Which, I think, is true even for most of the developers. I have found this one out by a coincidence myself.
This is the approach that works very well for me. Especially since I keep my repos cloned into a directory structure inspired by "go get" using https://github.com/grdl/git-get
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
I asked because none of the posted permutations offer the same feature set. Specifically, the nice thing about this utility (and reason for the dependencies) is interactive adding and selecting of identities. If this can replicated in bash, I am legitimately interested in seeing it, 20 lines or not.
Implement the `git identity` portion listed earlier with a `identity-list` utilizing `git config --get-regexp`. If you need interactivity you can tie it together with fzf and some awk.
!!!
which not only covers the identity scenario but the whole configuration of git, which can be indeed very different from repo to repo, from company to company
What's the point of writing this simple tool in js? I'd like to use it but I don't have npm everywhere (and I don't want to grab a huge tree of dependencies just for editing a couple of lines on a text file...)
I suspect it was written in JS because that was the author’s language of choice. There are lots of ways to manage git identities and this seems like a decent utility for web devs who already have node installed and don’t care to manually edit config files.
You can have a per-project git config, which to me is a more convenient solution.
For 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.
Have seen more than a few attempts to solve this problem. Here's my version[0] and there's many more on GitHub. Wonder if there's an opportunity to unify our efforts & get something into git contrib?
Author here!
I'm a freelancer working for multiple agencies at the moment. And One of the requirements was that I use the agency email for all commits.
So I made this as a sugar for
git config user.email whatever@gmail.com
I find that creating a separate user account on my system for each client works much better for this kind of thing than trying to configure every application with multiple identities.