Painless Git+SSH Setup
In this guide, we’ll walk you through the steps to generate an SSH key pair, add it to your Git account, and configure your local Git setup for seamless integration.
By the end, you’ll have a more efficient and secure Git environment that enhances your productivity and protects your code.
Why?
Setting up Git to use SSH can significantly enhance your workflow by providing a secure and convenient method for authenticating your repositories. Whether you’re a seasoned developer or new to version control, configuring Git with SSH keys streamlines the process of pushing and pulling code, eliminating the need to repeatedly enter your username and password.
How?
Create the .ssh
Directory and SSH config
mkdir -p ~/.ssh/keys/
touch ~/.ssh/config
New-Item -ItemType Directory -Path "$env:UserProfile\.ssh\keys"
New-Item -ItemType File -Path "$env:UserProfile\.ssh\config"
If everything went successfully, the .ssh
directory tree will look like:
- config
.ssh/keys
directory helps manage multiple SSH keys if needed.Generate an SSH Key Pair
ssh-keygen -t ed25519 -b 4096 -a 100 -C private-pc -f ~/.ssh/keys/githb
ssh-keygen.exe -t ed25519 -b 4096 -a 100 -C private-pc -f "$env:UserProfile\.ssh\keys\github"
<Enter>
twice.Now your .ssh
tree will look like:
- config
- github
- github.pub
Explanation
-t
: Type of key, hereed25519
.-b
: Number of bits, here 4096.-a
: Number of KDF rounds, here 100.-C
: Comment, defaults to<username>@<computer name>
.-f
: File location and name, here “github”. This creates two files:.ssh/keys/github
: Private key..ssh/keys/github.pub
: Public key.
ssh-keygen
here.Create the SSH Config File
|
|
ssh_config
here.Explanation
The key to the configuration is the first line: Host github.com
. This tells SSH to use the defined settings whenever you connect to github.com
:
ssh github.com
SSH will use the settings provided:
Hostname
: The address to connect to.User
: The user to authenticate as, usually “git”.IdentityFile
: Path to the private key,%d
is the user home directory.AddKeysToAgent
: Adds the key to the ssh-agent, usful if the SSH key is password protected.RequestTTY
: Set tono
as no shell is needed.SessionType
: Set tonone
since no commands are executed on the Git hosting provider.
Add Your Key to the Git Hosting Provider
The general flow is:
Copy the SSH Public Key to Your Clipboard
cat ~/.ssh/keys/github.pub
# Then select and copy the displayed contents
Alternative Way
.ssh
folder, open the file in a text editor, and copy it to your clipboard.Add the Public Key to Your User’s SSH Keys
Follow the instructions for your provider:
Verify SSH Access
Run:
ssh github.com
If you don’t get any errors, the setup is successful. 🥳
Configure Git to Use SSH Instead of HTTPS
git config --global url.ssh://git@bitbucket.org/.insteadOf https://bitbucket.org/
git config --global url.ssh://git@codeberg.org/.insteadOf https://codeberg.org/
git config --global url.ssh://git@github.com/.insteadOf https://github.com/
git config --global url.ssh://git@gitlab.com/.insteadOf https://gitlab.com/
--global
flag while inside a git repository to configure that repository only.Tips & Tricks
Set Private Key File Permissions
If you encounter:
Permissions for ‘private-key’ are too open.
Run the following:
chmod 400 ~/.ssh/keys/github
New-Variable -Name Key -Value "$env:UserProfile\.ssh\keys\github"
Icacls $Key /c /t /Inheritance:d
Icacls $Key /c /t /Grant ${env:UserName}:F
Icacls $Key /c /t /Remove:g Administrator "Authenticated Users" BUILTIN\Administrators BUILTIN Everyone System Users
Remove-Variable -Name Key
Migrating to a Different Git Hosting Provider
To switch providers, change the Hostname
and IdentityFile
in the SSH config to the new provider and add a new Host
block for it.
All existing repositories will point to the new provider, and new repositories will work as expected.
Migrate from Github to Codeberg Config Example
|
|