How to setup git and GitHub on macOS?
A step by step guide to setup git and GitHub on Mac
A step by step guide to setup git and github on Mac
1.Prerequisite
Make sure that following tools are installed on your Mac
- Apple’s Developer Tools
- MacPorts Package Manager
If you don’t know what are these then don’t worry. Follow this beginner tutorial to install Apple’s Developer Tools and MacPorts.
2. Install Git
In step #1 we installed Apple’s Developer Tools. It installs a set of tools for development and along with other tools it also install git. In the terminal, run
1
git --version
and it will return something like
1
git version 2.39.3 (Apple Git-145)
We will use this git. If you want to use the latest version then you can install it using port
1
sudo port install git
Setup your git username and commit email address.
1
2
git config --global user.name "username"
git config --global user.email "useremail@host.com"
Replace username
and useremail@host.com
with your’s.
Now it’s time to learn version control using git
3. Setting up a GitHub account
Create a GitHub account to host your code on cloud and share it other developers.
4. Connect git with GitHub
You need to connect your git to GitHub. You can either connect over HTTPS os SSH. For beginner I would recommend to connect over SSH. Follow the following docs:
4.1 Generate a new SSH key
Open your terminal and run the command below after replacing "demo@example.com"
with your email address you used to create your GitHub account in step #3
1
ssh-keygen -t ed25519 -C "demo@example.com"
This will prompt you for a file to save keys (press enter for default file location) and then asks for password (choose a password or press enter for empty password)
1
2
3
Generating public/private ed25519 key pair.
Enter file in which to save the key (/xxxx/xxxx/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
This will create a pair of private and public keys id_ed25519
and id_ed25519.pub
in ~/.ssh
directory.
Once these keys are generated successfully, run the ssh-agent in the background
1
eval "$(ssh-agent -s)"
4.2 Modify your ~./ssh/config
file
Check if ~./ssh/config
exists, else create one
1
touch ~./ssh/config
Open ~./ssh/config
in your favorite text editor and add these lines
1
2
3
4
5
6
7
8
9
10
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
Host *
# Uncomment below line if you choose a password to generate keys
# UseKeychain yes
AddKeysToAgent yes
IdentitiesOnly yes
4.3 Add SSH keys to your macOS Keychain
1
2
ssh-add -D # Will delete all identities added previously from the ssh agent
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
Check the added key with ssh-add -l
.
4.4 Add the generated key to your github account
- Copy the generated public SSH key on clipboard
1
pbcopy < ~/.ssh/id_ed25519.pub
Go to your GitHub account and click on your the avatar on top right corner and then click on Settings
On the left sidebar, click on SSH and GPG keys and then click on New SSH Key
Add some title if you wish (like “My MacBook’s SSH Key”)
In Key Type section, select Authentication Key
Paste the public key in the Key section.
4.5 Test the Connection
1
ssh -T git@github.co
If you setup your git and GitHub account correctly then the above command should return the below message
1
Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.
5. Create a local repo and host it on GitHub
Create a new repository demo
on GitHub. After creating the repo successfully you will see three options to clone your repo: HTTPS, SSH and GitHub CLI. Choose SSH and copy the remote url.
5.1 Clone a Remote Repository
Open your terminal and clone this repo typing git clone
and then paste your repo remote url.
1
git clone git@github.com:haccks/demo.git
Now you can go to the demo
directory and start making changes and push it to the GitHub.
5.2 Host a Local Repository
Create a local repo named demo
1
mkdir demo
and perform these actions
1
2
3
4
5
6
7
8
9
10
cd demo
git init
git add README.md
git commit -m "First commit"
# Connect your local repo with the remote repo
git remote add origin git@github.com:haccks/demo.git
git branch -M main
git push -u origin main
If you already have a local repo then ignore the first four commands and use the last three only.
We are done here!
If you want to use multiple github accounts on Mac then follow this tutorial: How to use multiple github accounts on Mac?
References:
1. Generating a new SSH key and adding it to the ssh-agent.
2. Adding a new SSH key to your GitHub account.