Git Setup on Mac
1. Install Git
If you’ve installed XCode (or its Command Line Tools), Git may already be installed. To find out, open a terminal and enter the following command.
git version
Apple actually maintains and ship their own fork of Git, but it tends to lag behind mainstream Git by several major versions. You may want to install a newer version of Git.
There are several ways to install Git on a Mac but the easiest way is to install it using Homebrew.
brew install git
2. Setup SSH
Using SSH is the recommended way to use remote git repositories.
2.1. Generate SSH Keys
Create a .ssh
directory in your home if it does not exist.
cd
mkdir .ssh
ssh-keygen -t rsa -b 4096 -C "<your@email.com>"
When prompted, give the filename to save the key in the following format: /Users/<user>/.ssh/github-user-1
.
Remember the passphrase used, it will be asked for establishing connection every time.
2.2. Upload the SSH Public Key to the Remote Git Repository Account
Follow the instructions for your corresponding remote repository host accounts:
3. Create SSH Config
The SSH config file will help Git use the correct user, host, and SSH key to establish the connection via git commands.
Create a config file at ~/.ssh/config
and add the configuration entries in the following format.
# user-1 github personal account
Host github-user-1
HostName github.com
User git
IdentityFile ~/.ssh/github-user-1
IdentitiesOnly yes
# user-2 github work account
Host github-user-2
HostName github.com
User git
IdentityFile ~/.ssh/github-user-2
IdentitiesOnly yes
# user-1 gitlab personal account
Host gitlab-user-1
HostName gitlab.com
User git
IdentityFile ~/.ssh/gitlab-user-1
IdentitiesOnly yes
Note: Replace github-user-1
, github-user-2
, and gitlab-user-1
with your usernames/filenames.
4. Set Git User Name and Email
You can set a git user name and email globally using the following command.
git config --global user.email "<your@email.com>"
git config --global user.name "<name>"
You can override the global git user name and email for specific repositories by setting the local user name and email.
cd <your-git-repository>
git config --local user.email "<your@email.com>"
git config --local user.name "<name>"
5. Setup Repository
Clone a Repository
After the setup, you must clone a new remote git repository using the following command format.
git clone git@github-user-1:user-name/my-repository.git
Enter your passphrase when prompted and you are ready to work with the remote repository.
Set Remote URL of a Repository
After the setup, you must set or change the remote URLs of your repositories as per the following command format.
git remote add origin git@github-user-1:user-name/my-repository.git
git remote set-url origin git@github-user-1:user-name/my-repository.git
Enter your passphrase when prompted and you are ready to work with the remote repository.
Conclusion
- This setup allows you to use multiple remote git hosts on the same computer.
- It is more secure as it prompts you for a passphrase every time you try to connect (push, pull, fetch, etc.) with your remote repositories.
- You do not have to save your remote git account passwords locally.
- You can easily disable connections with any of your computers by deleting the SSH public key from your remote git account.