TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How I configure my Git identities

439 点作者 8organicbits6 个月前

30 条评论

powersnail6 个月前
I do something similar, but instead of `insteadOf`, I just clone the repo with `gh-work:org&#x2F;repo`, and in the git config:<p><pre><code> [includeIf &quot;hasconfig:remote.*.url:gh-work:**&#x2F;**&quot;] path = ~&#x2F;.gitconfig.d&#x2F;gh-work.inc </code></pre> So, any git repo cloned with the ssh identity defined under `gh-work` will take on the config of `gh-work.inc`, which includes the git identity, and also the same signing key as in the ssh config.<p>Essentially, the name `gh-work` becomes the distinguishing element in both my ssh identity and my git identity, and I find this easier to think about.
评论 #42235633 未加载
评论 #42239839 未加载
评论 #42251832 未加载
er453r6 个月前
One even-better approach IMHO<p>Just keep a <i>.gitconfig</i> in your <i>HOME</i> with aliases for your identities. Then just after initializing&#x2F;cloning the repo do <i>git config-company</i> or <i>git config-personal</i><p><pre><code> er453r@r7:~$ cat ~&#x2F;.gitconfig [user] useConfigOnly = true [alias] config-personal = !echo CONFIG-PERSONAL &amp;&amp; \ git config --local user.email &#x27;personal@email.com&#x27; &amp;&amp; \ git config --local user.name &#x27;personal&#x27; &amp;&amp; \ git config --local core.sshCommand &#x27;ssh -i ~&#x2F;.ssh&#x2F;id_rsa_personal&#x27; config-company = !echo OLD CONFIG-COMPANY &amp;&amp; \ git config --local user.email &#x27;official@comapny.io&#x27; &amp;&amp; \ git config --local user.name &#x27;Name Surname&#x27; &amp;&amp; \ git config --local core.sshCommand &#x27;ssh -i ~&#x2F;.ssh&#x2F;id_rsa_company&#x27;</code></pre>
评论 #42235320 未加载
montroser6 个月前
I used to work at a startup with a character who would set his identity to be random fairytale-sounding nonsense, changing every day. So his commits on Monday would be attributed to Mr. Bunnymann, and Tuesday would be Doctor Funtime, etc.<p>It was super unhelpful when trying to do version control forensics. But if I&#x27;m being generous, I think maybe he was trying to remind everyone that anyone can put anything in their identity config, and we shouldn&#x27;t trust whatever is in there for all that much.
评论 #42235905 未加载
评论 #42235722 未加载
评论 #42235318 未加载
评论 #42235404 未加载
评论 #42235863 未加载
est6 个月前
you don&#x27;t have to mess with ~&#x2F;.ssh&#x2F;config<p>Just put this in your ~&#x2F;.gitconfig (or ~&#x2F;.config&#x2F;git&#x2F;personal as in the article)<p><pre><code> [core] sshCommand = &#x2F;usr&#x2F;bin&#x2F;ssh -o IdentitiesOnly=yes -i ~&#x2F;.ssh&#x2F;IdentityFile2 -a </code></pre> This makes submodules easy without the `insteadOf`
评论 #42233932 未加载
评论 #42233893 未加载
bobek6 个月前
I&#x27;ve been using `includeIf` with directory for ages (<a href="https:&#x2F;&#x2F;www.bobek.cz&#x2F;til&#x2F;git-identities&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.bobek.cz&#x2F;til&#x2F;git-identities&#x2F;</a>), the `hasconfig:remote` is really neat. And it also works when cloning the repository.
elric6 个月前
The includeIf stuff is pretty neat. I currently keep the SSH complexity in ~&#x2F;.ssh, where I have several includes, one for each customer|project|identity. Things without unique hostnames, like github, get an alias assigned:<p><pre><code> Host customer-github Hostname github.com IdentityFile ~&#x2F;.ssh&#x2F;customer_rsa User git </code></pre> All I have to do is use the alias in any git clone command and I&#x27;m done.
评论 #42234205 未加载
guthriej6 个月前
Thank you for this! I have exactly the same problem and was waiting for the solution to present itself, which it now has.<p>Aside: I use NixOS with home-manager (on linux and mac), which makes this trivial [1]. Added the following lines to my home-manager config:<p><pre><code> programs.git = { enable = true; ... includes = [ { condition = &quot;hasconfig:remote.*.url:git@github.com:&lt;work&gt;&#x2F;**&quot;; contents = { user.email = &quot;&lt;work email&gt;&quot;; }; } ]; } </code></pre> [1]: <a href="https:&#x2F;&#x2F;nix-community.github.io&#x2F;home-manager&#x2F;options.xhtml#opt-programs.git.includes" rel="nofollow">https:&#x2F;&#x2F;nix-community.github.io&#x2F;home-manager&#x2F;options.xhtml#o...</a>
评论 #42235253 未加载
bilalq6 个月前
So glad I clicked on this link. I was already doing the `includeIf: &quot;gitdir&quot;` thing to separate work and personal stuff, but `hasconfig:remote` is a total game-changer.
评论 #42234006 未加载
lijok6 个月前
I always strongly advise consultants to use a separate machine for work, or at the very least a separate OS user.<p>You’re risking putting yourself in a whole lot of trouble by using a personal machine for work.
评论 #42235670 未加载
评论 #42235480 未加载
cquintana926 个月前
Shameless plug for a tool I developed in order to easily switch git identities based on projects: <a href="https:&#x2F;&#x2F;github.com&#x2F;cquintana92&#x2F;git-switch-user">https:&#x2F;&#x2F;github.com&#x2F;cquintana92&#x2F;git-switch-user</a><p>After configuring the identities you just need to run<p><pre><code> $ git su Personal $ git su Work </code></pre> And all the identity configuration (email, name, SSH key and optionally PGP key) will be set up into the repo&#x27;s .git&#x2F;config file.<p>Saved me a ton of time.
评论 #42234194 未加载
stared6 个月前
&gt; Note: I&#x27;ve had this post drafted for 3 YEARS!!! It&#x27;s finally time to publish it.<p>I suddenly felt a deep connection with the author. It is not only me.<p>I promise you, my dear drafts, that one day, I will set you free to see the world!
computerfriend6 个月前
This is a nice trick. But if you:<p>* use a dedicated work machine and<p>* also want to version control your dotfiles (including ~&#x2F;.config&#x2F;git&#x2F;) and<p>* don&#x27;t want to leak your work repository organisation via your dotfiles,<p>you can instead add something like<p><pre><code> [include] path = work.gitconfig </code></pre> which will override any settings above it and also fail gracefully&#x2F;silently if work.gitconfig does not exist.
评论 #42235896 未加载
metabeard6 个月前
I prefer to split work and personal repos into ~&#x2F;dev for work and ~&#x2F;src for personal.<p><pre><code> # file ~&#x2F;.gitconfig [includeIf &quot;gitdir:~&#x2F;src&#x2F;&quot;] path = &#x2F;Users&#x2F;metabeard&#x2F;.config&#x2F;git&#x2F;.gitconfig-personal [includeIf &quot;gitdir:~&#x2F;dev&#x2F;&quot;] path = &#x2F;Users&#x2F;metabeard&#x2F;.config&#x2F;git&#x2F;.gitconfig-work </code></pre> and<p><pre><code> # file .config&#x2F;git&#x2F;.gitconfig-personal and .config&#x2F;git&#x2F;.gitconfig-work # both are very similar with different email and signingkey [user] name = Meta Beard email = email@metabeard.me signingkey = ssh-rsa xxx== [gpg] format = ssh [gpg &quot;ssh&quot;] program = &quot;&#x2F;Applications&#x2F;1Password.app&#x2F;Contents&#x2F;MacOS&#x2F;op-ssh-sign&quot; [commit] gpgsign = true</code></pre>
评论 #42237861 未加载
cimnine6 个月前
I&#x27;ve written a short blogpost about how to link SSH Keys to Git identities: <a href="https:&#x2F;&#x2F;brainfood.xyz&#x2F;post&#x2F;20241030-use-a-specific-ssh-key-in-a-git-repo&#x2F;" rel="nofollow">https:&#x2F;&#x2F;brainfood.xyz&#x2F;post&#x2F;20241030-use-a-specific-ssh-key-i...</a>
zelphirkalt6 个月前
Why does the page redirect me periodically to <a href="https:&#x2F;&#x2F;www.benji.dog&#x2F;%F0%9F%8D%AA&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.benji.dog&#x2F;%F0%9F%8D%AA&#x2F;</a> and hide its content?
pestaa6 个月前
Is there a risk with not using different keys for work and personal?<p>The private bits are all in the same place: if one is compromised, so are the rest.
评论 #42234166 未加载
评论 #42234154 未加载
评论 #42234720 未加载
评论 #42234223 未加载
bentinata6 个月前
I use `insteadOf` instead of ssh alias because my workplace use GitLab orgs. So instead of typing the full URL like:<p><pre><code> git clone gitlab.com&#x2F;acme-corp&#x2F;project-name </code></pre> I could use:<p><pre><code> git clone work:project-name </code></pre> But this kinda broke `includeIf` since it store the `insteadOf` remote url directly. I then had to convert existing repositories to use the `insteadOf` url.<p>I wrote a little bit about it here: <a href="https:&#x2F;&#x2F;bentinata.com&#x2F;log&#x2F;git-insteadof-includeif" rel="nofollow">https:&#x2F;&#x2F;bentinata.com&#x2F;log&#x2F;git-insteadof-includeif</a>
andrei-akopian6 个月前
&gt; Note: I&#x27;ve had this post drafted for 3 YEARS!!! It&#x27;s finally time to publish it.<p>Did you say that just so we could imagine the world where you published it earlier?<p>Thanks anyway, and nice site!
codazoda6 个月前
I love that Rek drew the image on your about page. I knew I recognized their work as soon as I saw it.<p>Based on that and your 1.44MB Club, you might find Neat CSS interesting. :P<p><a href="https:&#x2F;&#x2F;neat.joeldare.com" rel="nofollow">https:&#x2F;&#x2F;neat.joeldare.com</a><p>My Neat CSS websites will almost always fit on a floppy and I have a case of old floppies right here in my closet. The Neat CSS home page is only about 12k. Things get bigger when you start adding images, of course.
zelphirkalt6 个月前
Somewhat annoying, that the configuration is spread out across 2 config files of 2 separate programs (git and ssh) and that the `insteadOf` `url` must match the `Host` in `.ssh&#x2F;config`, which leaves a lot of room for mistakes and is fiddly, but at least this method works well!<p>Edit: Hmm it seems to ask me over and over again for my password for a key at every pull or push. Maybe this method somehow disables memorizing the SSH identity?
asadjb6 个月前
I hated configuring multiple Hosts&#x2F;aliases in my ~&#x2F;.ssh&#x2F;config for Github&#x2F;Bitbucket when dealing with different keys for different clients.<p>I ended up creating a &quot;SSH environment&quot; manager 4 years ago to help with this: <a href="https:&#x2F;&#x2F;github.com&#x2F;theonejb&#x2F;sshenv">https:&#x2F;&#x2F;github.com&#x2F;theonejb&#x2F;sshenv</a><p>It&#x27;s worked wonderfully for me since then, and it&#x27;s something I use almost daily.
ku1ik6 个月前
This post is a great reference of what’s possible with git config wrt includes&#x2F;remotes, and I’m sure I’ll be getting back to it.<p>One thing though: what’s the point of using separate keys for work&#x2F;personal&#x2F;github&#x2F;gitlab? I fail to see a practical and security advantage over using one key (per workstation).
评论 #42234192 未加载
评论 #42234406 未加载
评论 #42234196 未加载
ab71e56 个月前
Great tips, did not know about the `hashconfig:remote` option.<p>Currently dealing with a difficult setup where we have subrepos (so just using an `~&#x2F;.ssh&#x2F;config` alias for github.com:org does not work), some dependencies downloaded with CMake CPM, and working in a vscode devcontainer.
jmb996 个月前
As cool as this is, how many peoples&#x27; employers allow them to do either personal work from the work computers, or work work from their personal computers? My company is quite strict on both.
评论 #42234231 未加载
评论 #42235688 未加载
r0rshrk6 个月前
This works well enough for me with minimal configuration<p><a href="https:&#x2F;&#x2F;github.com&#x2F;TheYkk&#x2F;git-switcher">https:&#x2F;&#x2F;github.com&#x2F;TheYkk&#x2F;git-switcher</a>
dmos626 个月前
Why have multiple git identities in the same machine? I use a single different key with each machine and that&#x27;s it.
评论 #42234792 未加载
评论 #42234641 未加载
layer86 个月前
Be careful what client you use, though. JGit unfortunately still doesn’t support <i>includeIf</i>.
vishnugupta6 个月前
I really liked the website, the layout, typography, icons etc. Really well done!
评论 #42233939 未加载
cabirum6 个月前
user.useconfigonly=true can also help
quectophoton6 个月前
Eh, if I need a different identity[1], I just use a different identity[2] (ideally different machine or host when feasible).<p>I&#x27;m not a fan of mixing identities, like for example mixing a personal identity[2] with a work identity[1].<p>[1]: Git.<p>[2]: OS user.