- ऐसे environment में जहाँ personal और work repositories दोनों
~/workspaceमें रखे जाते हैं, folder location के बजाय remote URL के आधार पर Git identity को अलग करना ज़्यादा सटीक होता है - Git का
includeIfgitdirके ज़रिए path-based settings लोड कर सकता है, लेकिन जब एक ही working directory में कई accounts की repositories मिली-जुली हों, तो path-based branching की सीमा सामने आ जाती है hasconfig:remote.*.url:condition इस्तेमाल करने पर GitHub, GitLab, SourceHut या किसी खास GitHub organization जैसे remote URL pattern के हिसाब से अलग config files include की जा सकती हैं- SSH keys को
~/.ssh/configमेंHost,Hostname,User,IdentityFileके साथ अलग से manage करना चाहिए, और उसीgithub.comपर भी organization-specific keys इस्तेमाल करनी हों तो Host alias की जरूरत होती है url.<base>.insteadOfको साथ में set करने पर आप रोज़मर्रा की तरहgit@github.com:orgname/projectइस्तेमाल कर सकते हैं, लेकिन internally यहgh-work:orgnameमें replace होकर सही SSH setting से होकर जाता है
remote URL के आधार पर Git settings अलग करना
- मौजूदा
includeIfexamplesgitdir:~/code/**,gitdir:~/work/**की तरह local directory path के अनुसार अलग config files include करते हैं~/codeके नीचे~/.config/git/personal, और~/workके नीचे~/.config/git/workलोड किया जा सकता है- include की गई files में आम तौर पर Git identity और signing key जैसे
user.name,user.email,user.signingkeyहोते हैं
- अगर सारा code
~/workspaceके नीचे रखा जाए, तो personal,work-1,work-2repositories एक ही path structure में मिल सकती हैं, इसलिए सिर्फ path-based conditions से मनचाहा अलगाव करना मुश्किल हो जाता है - Git का
hasconfig:remote.*.url:इस्तेमाल करने पर current repository में कोई खास remote URL होने पर ही config file include की जा सकती हैgit@github.com:*/**से match हो तो~/.config/git/config-ghgit@github.com:orgname/**से match हो तो~/.config/git/config-gh-orggit@gitlab.com:*/**से match हो तो~/.config/git/config-glgit@git.sr.ht:*/**से match हो तो~/.config/git/config-srht
- Git आख़िरी matched setting को include करता है, इसलिए condition order महत्वपूर्ण है
github.com:orgname/**condition को generalgithub.com:*/**condition से नीचे होना चाहिए, ताकि organization-specific setting general GitHub setting से overwrite न हो
- नतीजतन, जिन repositories में
github.com:orgname/**remote है वेconfig-gh-orgइस्तेमाल करती हैं, और बाकी GitHub repositories general GitHub setting इस्तेमाल करती हैं
SSH keys और insteadOf से organization-specific access information मिलाना
- Git identity से अलग, remote पर
pull·pushकरने के लिए SSH key settings की जरूरत होती है~/.ssh/configमेंgitlab.comके लिए~/.ssh/gitlab.id_ed25519github.comके लिए~/.ssh/github.id_ed25519की तरहIdentityFilespecify किया जा सकता है
ssh-agentsettings के हिसाब से हरHostकेIdentityFileके नीचेIdentitiesOnly yesजोड़ना अच्छा हो सकता है- एक ही
Hostnameयानीgithub.comपर organization के हिसाब से अलग keys इस्तेमाल करनी हों, तोHostvalue अलग रखनी होगी- configuration
Host gh-work,Hostname github.com,User git,IdentityFile ~/.ssh/work.id_ed25519के रूप में होती है
- configuration
- Git settings में
url "gh-work:orgname"औरinsteadOf = git@github.com:orgnameइस्तेमाल करने पर Git URL अपने-आप replace किया जा सकता है- user
git clone git@github.com:orgname/projectकी तरह input देता है - Git
github.com:orgnameहिस्से कोgh-work:orgnameमें बदलकर~/.ssh/configकीgh-worksetting इस्तेमाल करता है
- user
- यह तरीका SSH and multiple Git credentials से लिया गया trick है, और साथ में reference किए गए लेख ये हैं
1 टिप्पणियां
Hacker News की राय
insteadOfके बजाय repository कोgh-work:org/repoके रूप में clone करें, और Git config मेंincludeIf "hasconfig:remote.*.url:gh-work:**/**"रखेंऐसा करने पर
gh-workके तहत परिभाषित SSH identity से clone की गई repositories अपने-आपgh-work.incconfig ले लेंगी, और उसमें Git identity तथा SSH config जैसी signing key शामिल होगीआखिरकार
gh-workनाम ही SSH identity और Git identity को अलग करने का आधार बन जाता है, इसलिए यह समझना आसान हैincludeIfcase-sensitive है, और priority में आखिरी config जीतती हैयह सही चल रहा है या नहीं देखने के लिए
git remote get-url originऔरgit config --get user.emailचलाया जा सकता हैमेरे हिसाब से बेहतर तरीका है कि
HOMEकी.gitconfigमें identity-specific aliases रखें, और repository initialize या clone करने के तुरंत बादgit config-companyयाgit config-personalचलाएँuser.useConfigOnly = trueचालू करके, alias में local repository केuser.email,user.name,core.sshCommandको क्रमशः personal/work SSH keys पर set कर देंलेख के तरीके का फायदा यह लगता है कि organization से clone करने पर यह सीधे काम करता है
पहले एक startup में कोई व्यक्ति रोज़ किसी परीकथा जैसे मनमाने नाम से अपनी identity बदलता था
सोमवार के commits Mr. Bunnymann, मंगलवार के commits Doctor Funtime जैसे होते थे, इसलिए version control forensics करते समय बहुत परेशानी होती थी
नरमी से देखें तो शायद वह यह याद दिलाना चाहता था कि कोई भी identity setting में कुछ भी डाल सकता है, इसलिए उस value पर बहुत भरोसा नहीं करना चाहिए
फिर भी details पूछने या style और expertise का अंदाजा लगाने के लिए यह जानना मददगार होता है कि किसने किया
commits पर GPG signature अनिवार्य करके और allowed GPG identities register करके author/committer metadata के बजाय signature से वास्तविक author पहचाना जा सकता है
बेशक “simple” और GPG signature हमेशा साथ-साथ अच्छे से नहीं चलते
अगर आप यह भरोसा नहीं कर सकते कि employee अपने commits को सही तरह identify करेगा, तो मेरे हिसाब से उसे निकाल देना चाहिए
~/.ssh/configछेड़ने की जरूरत नहीं;~/.gitconfigया लेख की तरह~/.config/git/personalमेंcore.sshCommand = /usr/bin/ssh -o IdentitiesOnly=yes -i ~/.ssh/IdentityFile2 -aडाल देंइससे
insteadOfके बिना भी submodules आसान हो जाते हैंमैं काफी समय से directory-based
includeIfइस्तेमाल कर रहा था (https://www.bobek.cz/til/git-identities/), लेकिनhasconfig:remoteवाकई बहुत साफ-सुथरा हैयह repository clone करते समय भी काम करता है
includeIfकाफी अच्छा हैफिलहाल SSH complexity को
~/.sshमें रखता हूँ, और हर customer/project/identity के लिए एक-एक include रखता हूँGitHub जैसी जगहों के लिए, जहाँ unique hostname नहीं होता,
customer-githubजैसा host alias लगाता हूँ, औरHostName github.com,IdentityFile ~/.ssh/customer_rsa,User gitकी तरह config करता हूँइसके बाद
git cloneमें बस वही alias इस्तेमाल करना होता हैमुझे भी यही समस्या थी, और अब लगता है समाधान मिल गया
NixOS और home-manager को Linux और Mac पर इस्तेमाल करने से यह setup आसान हो जाता है
programs.git.includesमेंcondition = "hasconfig:remote.*.url:git@github.com:/**"औरuser.emailsetting डाल देंसंदर्भ: https://nix-community.github.io/home-manager/options.xhtml#opt-programs.git.includes
.gitconfigमें लिखने से यह कम आसान लगता हैcondition और setting तो लेख जैसी ही हैं, लेकिन इसमें build/template step और अजीब syntax वाली नई programming language सीखना भी जुड़ जाता है
मैं पहले से
includeIf: "gitdir"से काम और personal settings अलग कर रहा था, लेकिनhasconfig:remoteपूरी तरह game changer हैconsultants को मैं हमेशा strongly recommend करता हूँ कि काम के लिए अलग machine इस्तेमाल करें, या कम-से-कम अलग OS user रखें
personal machine को काम में इस्तेमाल करने से बड़ी मुश्किल में पड़ने का risk होता है
remote-first company में खुद laptop arrange करना और हर 2–3 साल में नई device खरीदने का खर्च मिलना, लेकिन फिर भी वह personal laptop होना—ऐसा भी हो सकता है, या आप temporary contractor भी हो सकते हैं
किस situation में और क्यों समस्या होती है, इसे ज्यादा specific बताना चाहिए
risk सचमुच है, लेकिन अगर उसे list नहीं कर पा रहे, तो यह education से ज्यादा FUD फैलाने जैसा हो जाता है
project के हिसाब से Git identity आसानी से बदलने के लिए बनाया गया tool: https://github.com/cquintana92/git-switch-user
identities set करने के बाद
$ git su Personalया$ git su Workचलाने पर email, name, SSH key, और optional रूप से PGP key तक repository की.git/configमें set हो जाती हैइससे काफी समय बचा
12 साल पुराना tool है, लेकिन अभी भी actively maintained है