TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Switch Git Users CLI

140 pointsby geongeorgekover 4 years ago

15 comments

varlover 4 years ago
I found this user management strategy somewhere, and it&#x27;s been working great for me:<p><pre><code> 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.&lt;id&gt;.name &quot;&lt;name&gt;&quot; git config --global user.&lt;id&gt;.email &quot;&lt;email&gt;&quot; git config --global alias.identity &#x27;! git config user.name &quot;$(git config user.$1.name)&quot;; git config user.email &quot;$(git config user.$1.email)&quot;; :&#x27; </code></pre> So given that I have created two users, e.g. personal and work I run:<p><pre><code> git identity work </code></pre> in repos that need the work name&#x2F;e-mail, and<p><pre><code> git identity personal </code></pre> in the ones that are private.
评论 #25070829 未加载
评论 #25071233 未加载
评论 #25072775 未加载
评论 #25074289 未加载
评论 #25072918 未加载
评论 #25071028 未加载
评论 #25072891 未加载
pavloover 4 years ago
This is a great looking tool.<p>What I have been doing by hand for some time is putting code for different customers in different directories and having a conditional in `~&#x2F;.gitconfig` to determine what config applies there:<p><pre><code> [includeIf &quot;gitdir:~&#x2F;projects-private&#x2F;**&quot;] path = .&#x2F;.gitconfig-private [includeIf &quot;gitdir:~&#x2F;projects-client&#x2F;**&quot;] path = .&#x2F;.gitconfig-work </code></pre> Then in .gitconfig-private or .gitconfig-work I have all the usual gitconfig settings that apply, for example the [user] section...<p>Switching to the right directory thus automatically changes the settings.
评论 #25069896 未加载
评论 #25070342 未加载
评论 #25071467 未加载
Foxboronover 4 years ago
A 1000 line YARN lock file for something that would be roughly 20 lines of bash is amazing and terrifying at the same time.
评论 #25070658 未加载
评论 #25070988 未加载
评论 #25070644 未加载
评论 #25071652 未加载
oefrhaover 4 years ago
Without using include in gitconfig:<p><pre><code> #!&#x2F;usr&#x2F;bin&#x2F;env zsh case $1 in foo) git config user.name &#x27;John Doe&#x27; git config user.email john@example.com git config user.signingKey 0xFFFFFFFF ;; bar) git config user.name &#x27;Jane Doe&#x27; git config user.email jane@example.com git config user.signingKey 0xEEEEEEEE ;; baz) git config user.name &#x27;My Dog&#x27; git config user.email dog@example.com git config user.signingKey 0xDDDDDDDD ;; *) echo &quot;usage: git-user foo|bar|baz&quot; &gt;&amp;2 exit 1 ;; esac </code></pre> Done.<p>Btw probably want to add signingKey support.
archseerover 4 years ago
git has a similar feature built in: <a href="https:&#x2F;&#x2F;deepsource.io&#x2F;blog&#x2F;managing-different-git-profiles&#x2F;" rel="nofollow">https:&#x2F;&#x2F;deepsource.io&#x2F;blog&#x2F;managing-different-git-profiles&#x2F;</a>
评论 #25071484 未加载
mr-karanover 4 years ago
I currently use [karn](<a href="https:&#x2F;&#x2F;github.com&#x2F;prydonius&#x2F;karn" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;prydonius&#x2F;karn</a>) to manage multiple `git` identities. Works pretty nice.
评论 #25074231 未加载
gurjeetover 4 years ago
I rely on Git&#x27;s `user.useConfigOnly` option to force myself to set my email per repository. This is what my `user` section in ~&#x2F;.gitconfig looks like.<p><pre><code> [user] name = Gurjeet Singh # Tell Git to _not_ guess my name and email based on `whoami` and `hostname` useConfigOnly = true </code></pre> With this in place, whenever I try to commit for the first time in a repository, Git prompts me<p><pre><code> *** Please tell me who you are.* </code></pre> I then add an email address to the repo-local config based on whether it&#x27;s work or personal project.<p><pre><code> git config user.email me@example.com</code></pre>
miguelmotaover 4 years ago
This package installed 47.4MiB of node_modules. Yikes.
enriqutoover 4 years ago
What&#x27;s the point of writing this simple tool in js? I&#x27;d like to use it but I don&#x27;t have npm everywhere (and I don&#x27;t want to grab a huge tree of dependencies just for editing a couple of lines on a text file...)
评论 #25070815 未加载
评论 #25071970 未加载
29athrowawayover 4 years ago
You can have a per-project git config, which to me is a more convenient solution.<p>For my work repo I configure the repo to use my work identity. For my personal repos I use my personal identity.<p>I do not need to switch identities when using the same repo.<p>Doing this, you only setup your identity once per repo not every time, which is safer&#x2F;less error prone. i.e.: you wont leak your work email on github by accident.
KuiNover 4 years ago
Have seen more than a few attempts to solve this problem. Here&#x27;s my version[0] and there&#x27;s many more on GitHub. Wonder if there&#x27;s an opportunity to unify our efforts &amp; get something into git contrib?<p>[0] - <a href="https:&#x2F;&#x2F;github.com&#x2F;bobbo&#x2F;git-profile" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bobbo&#x2F;git-profile</a>
SparkyMcUnicornover 4 years ago
This line makes it not very useful, because I have to re-add accounts for each project.<p><pre><code> const store = require(&quot;data-store&quot;)({ path: process.cwd() + &quot;&#x2F;data.json&quot; }); </code></pre> I think a `git-user-data.json` file in a centralized location makes far more sense.
geongeorgekover 4 years ago
Author here! I&#x27;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.<p>So I made this as a sugar for git config user.email whatever@gmail.com
评论 #25069821 未加载
评论 #25070761 未加载
zachripover 4 years ago
Looks neat, but I agree with others about using conditional includes instead. Perhaps this tool could build on that?
Icyphoxover 4 years ago
Could&#x27;ve been written in a 30–40 line shell script, heh.