5 ms·
Show HN: Switch Git Users CLI
- geongeorgek 6y agoAuthor 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
- Foxboron 6y agohttps://git-scm.com/docs/git-config#_conditional_includes https://git-scm.com/docs/git-config#_conditional_includes Is a lot easier then what you came up with.
- geongeorgek 6y agoThanks. This is perfect but I also like the convenience of a quick switcher for any folder.
- JimDabell 6y agoI 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.
- edoceo 6y agoMore isolation, less accidents. I use this way too.
- Foxboron 6y agoA 1000 line YARN lock file for something that would be roughly 20 lines of bash is amazing and terrifying at the same time.
- gnagatomo 6y agoCan you show us your take on the bash solution?
- cdubzzz 6y agoLet’s see the 20 lines of bash that replicate all the features of this utility. Should fit in an HN comment nicely.
- boogies 6y ago> roughly 20 lines of bash 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
- cdubzzz 6y agoI 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.
- Foxboron 6y agoImplement 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.
- archseer 6y agogit has a similar feature built in: https://deepsource.io/blog/managing-different-git-profiles/ https://deepsource.io/blog/managing-different-git-profiles/
- andreineculau 6y ago!!! 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
- pavlo 6y agoThis is a great looking tool. 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.
- diggan 6y agoThe 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.
- geongeorgek 6y agoThanks for sharing this. This was what I was missing. Should've done proper research before building. 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.
- pavlo 6y agoI 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.
- gru 6y agoThis 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 https://github.com/grdl/git-get
- oefrha 6y agoWithout using include in gitconfig: #!/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.
- zachrip 6y agoLooks neat, but I agree with others about using conditional includes instead. Perhaps this tool could build on that?
- Icyphox 6y agoCould've been written in a 30–40 line shell script, heh.
- enriquto 6y agoWhat'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...)
- cdubzzz 6y agoI 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.
- enriquto 6y agoOf course, but my point is that these kind of tools are of general interest, and most programmers are not web developers.
- benmller313 6y agoJavascript is a general interest language imo.
- maple3142 6y agoI think one of the benefit of using js to write cli tool is that you can install them very easily on Windows.
- varl 6y agoI found this user management strategy somewhere, and it's been working great for me: 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.
- 0xEFF 6y agoI 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.
- cbcoutinho 6y agoI 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
- augusto-moura 6y agoGit has conditional includes based on path[1]. My configuration is like: 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_includes https://git-scm.com/docs/git-config#_conditional_includes
- grenoire 6y agoWoah, this is neat!
- SparkyMcUnicorn 6y agoThis line makes it not very useful, because I have to re-add accounts for each project. 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.
- deleted 6y ago[deleted]
- KuiN 6y agoHave 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? [0] - https://github.com/bobbo/git-profile https://github.com/bobbo/git-profile
- mr-karan 6y agoI currently use [karn](https://github.com/prydonius/karn https://github.com/prydonius/karn) to manage multiple `git` identities. Works pretty nice.
- captn3m0 6y ago+1 for karn. Works nicely with any directory hierarchy.
- miguelmota 6y agoThis package installed 47.4MiB of node_modules. Yikes.
- 29athrowaway 6y agoYou 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.
- deleted 6y ago[deleted]
- gurjeet 6y agoI rely on Git's `user.useConfigOnly` option to force myself to set my email per repository. This is what my `user` section in ~/.gitconfig looks like. [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.com