git --versiongit version 2.37.0 (Apple Git-136)
MacOS should already come with git. You can test this by running the following code in Terminal:
git --versiongit version 2.37.0 (Apple Git-136)
If that returns an error you will need to install Xcode from the app store. If that fails, you can also install git directly from the git website.
If you haven’t already, you will need to create an account with one (or more) of the git repo services such as Github or Gitlab.
You can either use HTTPS or ssh protocols. There are pros and cons to each which I will document as I bump into them. HTTPS is generally considered to be easier to setup, but ssh is generally more secure.
Currently not documented.
Here we will walk through the process of setting up your computer to use the ssh protocol. If you want a more detailed set of instructions, see GitHub’s guide on the same process.
If you wish to use the ssh protocol, you must first generate an ssh key. To do this run the following code in Terminal. There will be a prompt to add a passphrase but you can just hit enter to not use a passphrase.
In all the code chunks below, you should replace website and key_id_comment to be specific towards your use-case. See next two notes below this code chunk for more explanation on what they are for.
ssh-keygen -t ed25519 -f ~/.ssh/website_ed25519 -C "key_id_comment"You should use different keys for different sites that require an ssh key, but you don’t have to. If you want to use the same key then instead of substituting website in -f ~/.ssh/website_ed25519 with the website for the key (e.g. -f ~/.ssh/github_ed25519), you can remove that section altogether. By default, ssh-keygen -t ed25519 -C "key_id_comment" will place a key named id_ed25519 in ~/.ssh.
key_id_comment is just something to help you identify the key, but not something you absolutely need. I usually make it the same as the website I am using (e.g. “github.com”). If you don’t want to use it, remove -C "key_id_comment".
ed25519 is one algorithm used to create ssh keys but it may not be the best one for you. See here for more information.
Next you will need to add the key to the ssh agent. First start the ssh agent by running:
eval "$(ssh-agent -s)"Newer MacOS users will need to create a config file in the .ssh folder, if it hasn’t already been created. To do so, run:
touch ~/.ssh/configThen using whatever method you prefer (e.g. vim, Rstudio, etc.), open the file and add the following lines:
Host website.com
AddKeysToAgent yes
IdentityFile ~/.ssh/website_ed25519Finally, add the key to the ssh agent by running:
ssh-add ~/.ssh/website_ed25519You will need to repeat these steps for each website you wish to have a unique key for. If you exit the terminal, you will need to restart the ssh agent (via the eval command). You also won’t need to recreate the config file (via the touch command), but you will need to edit it to include new lines for the new Host and Identity files.
With an ssh key in place you can now add it to your github account. For more detailed instructions see Github’s guide.
The first step is only required based on how you plan on cloning the app the first time, but it can’t hurt to do regardless. This step involves you adding github.com to the list of known hosts. This is done by running the following in terminal:
ssh-keyscan -t ed25519 github.com >> ~/.ssh/known_hostsThe second step is to copy the public key you generated previously. If you followed the naming recommendations in that step, you can run the following in terminal:
pbcopy < ~/.ssh/github_ed25519.pubThis should automatically add the key to your clipboard, but alternatively you can run the following:
cat ~/.ssh/github_ed25519.puband manually copy everything that appears after hitting enter (up until the new command prompt line).
The third step is to go to your github.com profile settings, navigate to the section labeled SSH and GBG keys, and add a new ssh key. There is a button which opens a popup window. You will provide a name for the key (e.g. Hutch Laptop 2022) and paste the public key into the text box below.
You should now be ready to clone repositories from github.com. When using an ssh key to clone git repos, you cannot use the https URL in the clone command. The appropriate path will be something along the lines of “git@github.com:user_name/repo_name.git” where username and repo are specific to the repo being cloned. You can find the correct path by going to the git repo you wish to clone. On the main page of the repo, there will be a box called “Code” that gives a drop-down menu if you click on it. In the drop-down, you will see options for cloning the repo. Select “SSH” and the correct path will appear. If you are using Rstudio’s projects to create a project from git, that path can be pasted into repository URL box. If you are using the command line git function the code will look something like:
git clone git@github.com:user_name/repo_name.gitWith an ssh key in place you can now add it to your gitlab account. For more detailed instructions see Gitlab’s guide.
The first step is only required based on how you plan on cloning the app the first time, but it can’t hurt to do regardless. This step involves you adding gitlab.com to the list of known hosts. This is done by running the following in terminal:
ssh-keyscan -t ed25519 gitlab.com >> ~/.ssh/known_hostsThe second step is to copy the public key you generated previously. If you followed the naming recommendations in that step, you can run the following in terminal:
pbcopy < ~/.ssh/gitlab_ed25519.pubThis should automatically add the key to your clipboard, but alternatively you can run the following:
cat ~/.ssh/gitlab_ed25519.puband manually copy everything that appears after hitting enter (up until the new command prompt line).
The third step is to go to your gitlab.com profile preferences, navigate to the section labeled SSH keys, and add a new ssh key. There is a button which opens a popup window. You will provide a name for the key (e.g. Hutch Laptop 2022) and paste the public key into the text box below.
You should now be ready to clone repositories from gitlab.com. When using an ssh key to clone git repos, you cannot use the https URL in the clone command. The appropriate path will be something along the lines of “git@gitlab.com:user_name/repo_name.git” where username and repo are specific to the repo being cloned and the creator of that repo. You can find the correct path by going to the git repo you wish to clone. On the main page of the repo, there will be a box called “Clone” that gives a drop-down menu if you click on it. In the drop-down, you will see options for cloning the repo. Copy the path for “Clone with SSH”. If you are using Rstudio’s projects to create a project from git, that path can be pasted into repository URL box. If you are using the command line git function the code will look something like:
git clone git@gitlab.com:user_name/repo_name.git