A place to cache linked articles (think custom and personal wayback machine)
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.md 4.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. title: Setting Up Git Identities
  2. url: https://www.micah.soy/posts/setting-up-git-identities/
  3. hash_url: 4218c8b3332d61d6702bb2bd73ea9944
  4. <p>Working on many projects across multiple identities can be difficult to manage. This is a procedure for leveraging git aliases to set an identity at the project level for any project with support for GPG-based commit signing.</p>
  5. <h3 id="first-remove-any-existing-global-identity">First, remove any existing global identity</h3>
  6. <div class="highlight"><pre><code class="language-shell" data-lang="shell">git config --global --unset user.name
  7. git config --global --unset user.email
  8. git config --global --unset user.signingkey
  9. </code></pre></div><h3 id="require-config-to-exist-in-order-to-make-commits">Require config to exist in order to make commits</h3>
  10. <p>Without the global user name and user email, git would use the system’s hostname and username to make commits. Tell git to throw an error instead, requiring you to specify an identity for every new project.</p>
  11. <div class="highlight"><pre><code class="language-shell" data-lang="shell">git config --global user.useConfigOnly true
  12. </code></pre></div><h3 id="for-each-identity-generate-gpg-keys">For each identity, generate GPG keys</h3>
  13. <p><img src="https://www.micah.soy/posts/setting-up-git-identities/keygen.png" alt="GPG key generation output"/></p>
  14. <p>Generate a GPG public/private key pair:</p>
  15. <p>Choose (1) RSA and RSA (default) key type. Choose key size of 4096 bits. Set the key to not expire (0) unless you want to repeat this step periodically. Finally, set your name and email address. Comment can be left blank.</p>
  16. <p>Once the key pair is generated we need to export the public key.</p>
  17. <h3 id="export-the-public-keys">Export the public keys</h3>
  18. <p><img src="https://www.micah.soy/posts/setting-up-git-identities/key-export.png" alt="GPG key export"/></p>
  19. <p><em>For each identity</em>, export the public key:</p>
  20. <div class="highlight"><pre><code class="language-shell" data-lang="shell">gpg --list-secret-keys --keyid-format LONG user@example.com
  21. </code></pre></div><p>where <code>user@example.com</code> is the email address of the identity you just created.</p>
  22. <p>This will output a <code>sec</code> ID in the format of <code>rsa4096/[serial]</code>. Copy the serial number, then run this command to output the public key:</p>
  23. <div class="highlight"><pre><code class="language-shell" data-lang="shell">gpg --armor --export <span>[</span>serial<span>]</span>
  24. </code></pre></div><p>Copy the public key block and add it to your Github or Gitlab settings. With the public key, Github and Gitlab can cryptographically verify your commits, placing a “Verified” label next to each.</p>
  25. <h3 id="set-global-git-config-identities">Set global git config identities</h3>
  26. <p>Now we need to create the identities in git’s global config. For example:</p>
  27. <div class="highlight"><pre><code class="language-shell" data-lang="shell">git config --global user.gitlab.name <span>"Your Name"</span>
  28. git config --global user.gitlab.email <span>"gitlab@example.com"</span>
  29. git config --global user.gitlab.signingkey 543166183AE7043A
  30. git config --global user.github.name <span>"Your Name"</span>
  31. git config --global user.github.email <span>"github@example.com"</span>
  32. git config --global user.github.signingkey BCF8B7A8C138D16B
  33. git config --global user.identity3.name <span>"Your Name"</span>
  34. git config --global user.identity3.email <span>"identity3@example.com"</span>
  35. git config --global user.identity3.signingkey 4F3FFC37B1A027BD
  36. git config --global user.identity4.name <span>"Your Name"</span>
  37. git config --global user.identity4.email <span>"identity4@example.com"</span>
  38. git config --global user.identity4.signingkey D921F8BA473CF1FC
  39. </code></pre></div><h3 id="create-git-alias">Create git alias</h3>
  40. <p>Setting a git alias will give us a new git command to use to set the identity at a project level. This really is just a script that sets a particular global identity to the local config.</p>
  41. <div class="highlight"><pre><code class="language-shell" data-lang="shell">git config --global alias.identity <span>'! git config user.name "$(git config user.$1.name)"; git config user.email "$(git config user.$1.email)"; git config user.signingkey "$(git config user.$1.signingkey)"; :'</span>
  42. </code></pre></div><h3 id="specify-git-identity">Specify git identity</h3>
  43. <p>For each project, specify the git identity to use:</p>
  44. <div class="highlight"><pre><code class="language-shell" data-lang="shell">$ cd /path/to/git/repo
  45. $ git config user.email <span># should be no response</span>
  46. $ git config user.github.email
  47. github@example.com
  48. $ git identity github
  49. $ git config user.email
  50. github@example.com
  51. </code></pre></div><p>That’s it! Now whenever you start a new project or work on an existing project, you can be confident that the correct name, email address, and GPG signing key are being used.</p>