Using git on command line to access Github respositories - EpiModel/EpiModeling GitHub Wiki

Github Personal Access Token

A personal access token (PAT) on Github is used to access private repositories, such as when you are trying to install a private R package using install_github.

Creating a PAT on Your Local Computer

If you have not already created a PAT, follow these instructions. If you have, skip to the next section.

You can use the usethis package to create a new PAT for use in R, like this:

usethis::create_github_token()

Copy the PAT, and then put it in your .Renviron file as a system variable called GITHUB_PAT. You can open your .Renviron like this:

usethis::edit_r_environ()

Your .Renviron file should contain a line like this, but with your own unique PAT on the right-hand side.

GITHUB_PAT=XXXXXX

Save your .Renviron file. Note where it is saved locally.

Note that there are other potentially more sophisticated and secure ways to manage your Github PAT, as detailed in the usethis vignette.

Copying your PAT to the HPC

You will also need the same .Renviron file on the HPC. If the .Renviron file locally only contains you PAT, you can just copy it over using the secure copy function scp. Run this line in the terminal on your local computer:

scp ~/.Renviron sph:~/

Note that this assumes: 1) the .Renvion is your home directory on a linux-like computer (there may be a different location for .Renviron on Windows); and 2) you have set up your config file correctly, so you can reference the HPC with only sph (otherwise you will need to run [email protected]:~/ instead of sph)

On the HPC, you can take a look to see that it is there with:

cat .Renviron

Setting up SSH with Github

Similar to using SSH to log in the HPC without needing a password (described here), you can also set up SSH access to Github from the HPC. For this, you will follow the similar steps as above:

  1. Use ssh-keygen to generate a public/private keys in your .ssh folder on the HPC (whereas last time you created the ssh key on your local computer). Make sure you use follow the instructions here to specify your Github.com email address when creating a key. Do not worry about adding your key to the ssh-agent steps after generating a key; these are unnecessary.
  2. Instead of using ssh-copy-id, manually copy the contents of your new public key, following these instructions and it to your Github account.
  3. Test your connection, following these instructions.

If you want to skip this step, or it is not working, that is fine. It just means you will need to enter your Github.com password each time you interface with Github (e.g., cloning repositories).

Github Workflow

The typical Github workflow will be to develop your code locally (on your own computer), push the changes to Github (changes to both your project repositories, and pull the repository on the HPC. Typically, changes are not made to files on the HPC that then get pushed back up to Github, to make the workflow easier, but that is possible too.

Cloning

To clone an existing repository (e.g., your project repository) means to download a current copy of it for the first time. Navigate over to your projects folder subdirectory (i.e., /projects/epimodel/NETID/) and then type:

git clone [email protected]:EpiModel/REPO.git

replacing REPO with the name of your repository. This will create a new folder in your folder that will mirror what is on Github.

In the case you do not have SSH set up between the HPC and Github, you will need to clone with HTTPS, like so:

git clone https://github.com/EpiModel/REPO.git

Pulling

To pull an existing repository means to update the already downloaded copy of a repository you have already cloned. Navigate over to the repository directory (i.e., /projects/epimodel/NETID/REPO) and then type:

git pull

Changing Branch

You may be working across multiple branches on your Github project repository. In the case that you want to switch branches, use the following after git pull:

git checkout BRANCHNAME