Setup Git SSH on your OS
the process varies depending on your OS, but the end goal is the same.
let’s get started!
Windows
the most straightforward way to install git on windows is via the official installer, which includes git bash (a MinGW-based terminal, I guess).
- download the installer from the official git website.
- run the executable. The default settings are generally fine, but pay attention to the “choosing the default editor used by git” step (change it from vim to vs code or notepad++ if you aren’t comfortable with vim ofc).
alternatively, if you use a package manager like winget, open PowerShell and run:
winget install --id Git.Git -e --source winget
after installing git, close and reopen your terminal so that the changes to your PATH are properly applied.
verify the installation:
git --version
Linux
git is available in the default repositories of virtually every linux distribution.
debian / ubuntu:
sudo apt update
sudo apt install git
arch btw (lol):
sudo pacman -S git
fedora:
sudo dnf install git
verify the installation by checking the version:
git --version
macOS
mac actually ships with a stripped-down version of git, but it’s often outdated and tied to the Apple Command Line Tools. It is highly recommended to install the latest version using Homebrew.
- open the Terminal.
- if you don’t have Homebrew installed, install it first.
- run the following command:
brew install git
if you don’t want to use homebrew, running git in the terminal for the first time will prompt you to install the Apple Command Line Tools, which includes a usable version of git:
xcode-select --install
verify the installation:
git --version
Termux (Android)
vor mobile development environments, termux makes installing git trivial.
ensure your packages are up to date:
pkg update && pkg upgrade
install the git package:
pkg install git
verify the installation:
git --version
initial config
once git is installed on your system of choice, you need to configure your identity. This info is baked into every commit you make.
open your terminal and run the following commands, replacing the strings with your actual name and the email tied to your GitHub/GitLab account:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
you should also set your default branch name to main to align with modern repository standards:
git config --global init.defaultBranch main
To verify your configuration was saved correctly, run:
git config --list
your environment is now ready for cloning, branching, and committing.
however, if you want to use SSH URLs such as:
[email protected]:username/repository.git
you still need to configure SSH authentication.
setting up SSH
SSH allows you to authenticate with Git hosting platforms without entering your username and password every time you push or pull.
instead of using an HTTPS URL:
https://github.com/username/repository.git
you can use an SSH URL:
[email protected]:username/repository.git
the general process is:
- check if you already have an SSH key.
- generate a new SSH key if necessary.
- start the SSH agent.
- add your private key to the SSH agent.
- add your public key to GitHub, GitLab, or another Git hosting provider.
- test the connection.
- configure your repository to use SSH.
the exact commands are slightly different depending on your operating system.
check for an existing SSH key
before creating a new key, check if you already have one.
Windows
open PowerShell or Git Bash and run:
ls ~/.ssh
in PowerShell, you can also use:
Get-ChildItem ~/.ssh
Linux and macOS
run:
ls -la ~/.ssh
Termux
run:
ls -la ~/.ssh
look for files with names similar to:
id_ed25519
id_ed25519.pub
or:
id_rsa
id_rsa.pub
the file without .pub is your private key.
the file ending in .pub is your public key.
never share your private key with anyone.
if you already have an id_ed25519 key that you want to use1, you can skip the key generation step.
generate an SSH key
the recommended key type for most modern systems is Ed25519.
run:
ssh-keygen -t ed25519 -C "[email protected]"
you will be asked where to save the key.
the default location is usually fine:
~/.ssh/id_ed25519
press Enter to use the default location.
you will then be asked to enter a passphrase.
using a passphrase is highly recommended because it protects your private key if someone gains access to the key file.
after completing the process, you should have:
~/.ssh/id_ed25519
~/.ssh/id_ed25519.pub
if Ed25519 is unavailable on your system, you can use RSA instead:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
however, Ed25519 should generally be preferred when supported.
start the SSH agent
the SSH agent stores your decrypted private key in memory so that you don’t need to enter your passphrase every time Git uses SSH.
Linux and macOS
start the agent:
eval "$(ssh-agent -s)"
you should see something similar to:
Agent pid 12345
then add your key:
ssh-add ~/.ssh/id_ed25519
Git Bash on Windows
run:
eval "$(ssh-agent -s)"
then:
ssh-add ~/.ssh/id_ed25519
PowerShell on Windows
Windows includes an OpenSSH client and SSH agent service.
first, check the service:
Get-Service ssh-agent
if the service is disabled, enable it:
Set-Service -Name ssh-agent -StartupType Automatic
start the service:
Start-Service ssh-agent
then add your key:
ssh-add $env:USERPROFILE\.ssh\id_ed25519
verify that the key was added:
ssh-add -l
Termux
start the SSH agent:
eval "$(ssh-agent -s)"
then add your key:
ssh-add ~/.ssh/id_ed25519
if you want to use SSH regularly in Termux, you can install the Termux API and configure your environment to start the agent automatically, but for basic Git usage the commands above are enough.
add your SSH key to GitHub
now that your key has been generated, you need to add the public key to your Git hosting account.
first, display your public key.
Windows PowerShell
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub
Git Bash, Linux, macOS, and Termux
cat ~/.ssh/id_ed25519.pub
you should see something similar to:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... [email protected]
copy the entire line.
do not copy the private key:
id_ed25519
only copy:
id_ed25519.pub
now open your GitHub account settings.
go to:
Settings / SSH and GPG keys/ New SSH key
give the key a recognizable title, such as:
My Windows PC
or:
My Linux Desktop
paste your public key into the key field and save it.
you can then test the connection from your terminal:
ssh -T [email protected]
the first time you connect, SSH may ask you to verify GitHub’s host fingerprint.
type:
yes
if the connection is successful, GitHub should respond with a message indicating that you have successfully authenticated.
GitHub does not provide shell access through this connection, so seeing a message that says shell access is not provided is normal.
add your SSH key to GitLab
the process is similar for GitLab.
first, copy your public key:
cat ~/.ssh/id_ed25519.pub
on PowerShell:
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub
then open your GitLab account settings.
go to:
Preferences /SSH Keys
paste your public key, give it a title, and add the key.
test the connection:
ssh -T [email protected]
if everything is configured correctly, GitLab should confirm that you have successfully authenticated.
test your SSH connection
after adding your key to your Git hosting provider, test the connection.
for GitHub:
ssh -T [email protected]
for GitLab:
ssh -T [email protected]
you can also run SSH in verbose mode if you need to debug a connection:
ssh -vT [email protected]
for even more detailed output:
ssh -vvvT [email protected]
this is particularly useful if SSH is not selecting the correct key or if the connection is being refused.
clone a repository using SSH
once SSH authentication works, you can clone repositories using their SSH URL.
for GitHub:
git clone [email protected]:username/repository.git
for GitLab:
git clone [email protected]:username/repository.git
you can find the SSH URL by opening the repository page and selecting the Code button, then choosing SSH.
change an existing repository from HTTPS to SSH
if you already cloned a repository using HTTPS, you don’t need to clone it again.
first, check the current remote:
git remote -v
you may see:
origin https://github.com/username/repository.git (fetch)
origin https://github.com/username/repository.git (push)
change the remote to SSH:
git remote set-url origin [email protected]:username/repository.git
verify the change:
git remote -v
you should now see:
origin [email protected]:username/repository.git (fetch)
origin [email protected]:username/repository.git (push)
you can now use:
git pull
git push
without authenticating through HTTPS.
using multiple SSH keys
if you use multiple GitHub accounts, GitHub and GitLab together, or separate keys for work and personal projects, you may want to configure multiple SSH keys.
for example:
~/.ssh/id_ed25519_personal
~/.ssh/id_ed25519_work
generate the keys with different filenames:
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_personal
and:
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_work
then create or edit your SSH configuration file:
~/.ssh/config
add:
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
you can now use different SSH host aliases when cloning repositories.
for your personal account:
git clone git@github-personal:username/repository.git
for your work account:
git clone git@github-work:organization/repository.git
this tells SSH exactly which key to use for each connection.
you can also change an existing repository’s remote:
git remote set-url origin git@github-work:organization/repository.git
fixing SSH permissions on Linux and macOS
SSH is strict about permissions on private keys.
if you get permission-related errors, make sure your .ssh directory is private:
chmod 700 ~/.ssh
then protect your private key:
chmod 600 ~/.ssh/id_ed25519
your public key can have less restrictive permissions:
chmod 644 ~/.ssh/id_ed25519.pub
you can also protect your SSH configuration:
chmod 600 ~/.ssh/config
SSH connection refused or timeout
sometimes SSH works perfectly but the connection to port 22 is blocked by your network, ISP, firewall, or hosting environment.
for example, you may see errors like:
ssh: connect to host github.com port 22: Connection timed out
or:
Connection refused
this does not necessarily mean your SSH key is incorrect.
GitHub also provides an SSH endpoint through port 443, which can be useful on networks where port 22 is blocked.
you can configure it in:
~/.ssh/config
add:
Host github.com
HostName ssh.github.com
User git
Port 443
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
now test the connection again:
ssh -T [email protected]
SSH will connect to GitHub’s SSH service through port 443 instead of port 22.
this is especially useful on restricted networks, corporate networks, or environments where outbound SSH connections are blocked.
troubleshooting
if ssh -T [email protected] fails, check whether your SSH key is loaded:
ssh-add -l
if no identities are available, add your key:
ssh-add ~/.ssh/id_ed25519
check that the public key you uploaded is actually the one belonging to your private key:
ssh-keygen -y -f ~/.ssh/id_ed25519
you can also inspect the SSH connection in verbose mode:
ssh -vT [email protected]
look for lines showing which identity file SSH is attempting to use.
you can also check your Git remote:
git remote -v
if your repository is still using HTTPS, change it to SSH:
git remote set-url origin [email protected]:username/repository.git
if you’re using multiple SSH keys, make sure your ~/.ssh/config file is correctly configured and that IdentitiesOnly yes is set for each host alias.
final checklist
at this point, your Git environment should be completely configured.
you should have:
- Git installed.
- Your Git username configured.
- Your Git email configured.
mainconfigured as the default branch for new repositories.- An Ed25519 SSH key generated.
- Your private key stored securely.
- Your public key added to your Git hosting provider.
- Your SSH key loaded into
ssh-agent. - Your SSH connection tested successfully.
- Your repositories configured to use SSH URLs.
you can verify the most important Git settings with:
git config --global --list
and verify your SSH keys with:
ssh-add -l
finally, test your Git hosting provider:
ssh -T [email protected]
if everything works, you are ready to create repositories, clone projects, commit changes, and push your code over SSH.
for a new repository, the typical workflow is now:
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin [email protected]:username/repository.git
git push -u origin main
and for an existing repository:
git clone [email protected]:username/repository.git
cd repository
git pull
git push
your Git environment is now ready for everyday development across Windows, Linux, macOS, and Termux.
(I made this guide for myself too lol)