GitHub’s documentation for setting up Git on a new machine is scattered across many pages. This guide collects all the necessary steps in one place to make the process easier.
git config --global user.name "Mona Lisa"
git config --global user.email "YOUR_EMAIL"
Enter ls -al ~/.ssh
to see if existing SSH keys are present.
If there are, follow this guide.
Otherwise, follow the steps below.
Create a new SSH key with:
ssh-keygen -t ed25519 -C "your_email@example.com"
At the prompt, type a secure passphrase.
- Start the ssh-agent in the background
eval "$(ssh-agent -s)"
> Agent pid 59566
-
Check to see if your
~/.ssh/config
file exists in the default location. -
If it doesn't exist, create the file:
touch ~/.ssh/config
- Open your
~/.ssh/config
file, then modify the file to contain the following lines. If your SSH key file has a different name or path than the example code, modify the filename or path to match your current setup.
Host github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
- Add your SSH private key to the ssh-agent and store your passphrase in the keychain.
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
- Copy the SSH public key to your clipboard:
pbcopy < ~/.ssh/id_ed25519.pub
gpg --list-secret-keys --keyid-format=long
If there are already keys follow this guide. Otherwise generate new keys with the steps below.
- Install GPG command line tools:
brew install gnupg
- Generate a GPG key
gpg --full-generate-key
This will prompt you to configure the kind, size and length of the key, just press Enter
for using defaults.
After that, it will prompt you to enter your user ID and user email (same email as in Github account).
-
Type a secure passphrase
-
Copy the GPG key ID
-
Use the
gpg --list-secret-keys --keyid-format=long
command to list the long form of the GPG keys and copy the ID (number after slash). In this example, the GPG key ID is3AA5C34371567BD2
:
$ gpg --list-secret-keys --keyid-format=long
/Users/hubot/.gnupg/secring.gpg
------------------------------------
sec 4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
uid Hubot <hubot@example.com>
ssb 4096R/4BB6D45482678BE3 2016-03-10
- Paste the text below, substituting in the GPG key ID you'd like to use. In this example, the GPG key ID is
3AA5C34371567BD2
:
gpg --armor --export 3AA5C34371567BD2
-
Copy your GPG key, beginning with
-----BEGIN PGP PUBLIC KEY BLOCK------
and ending with-----END PGP PUBLIC KEY BLOCK-----
.
- Set your primary GPG signing key. In this example, the GPG key ID is
3AA5C34371567BD2
:
git config --global user.signingkey 4BB6D45482678BE3
- Optionally, to configure Git to sign all commits and tags by default, enter the following command:
git config --global commit.gpgsign true
git config --global tag.gpgSign true
- If you configured the GPG keys with a passphrase (recommended), install
pinentry-mac
and add it to your GNUPG configuration:
brew install pinentry-mac
echo "pinentry-program $(which pinentry-mac)" >> ~/.gnupg/gpg-agent.conf
This will prompt you to enter the passphrase the first time you make a signed commit. If you check the box "Add to keychain" it will never prompt again.