If you want to follow along with the presentations, you will need R, RStudio, a GitHub or GitLab account, and GitHub Desktop or Git+GitHub Desktop. Note GitHub Desktop works with both GitHub and GitLab and I will illustrate both.

Summary of the set-up steps. You can also consult this great online reference.

Git from RStudio

For the class, I am showing Git within RStudio, but it can be a real headache to get working for beginners. The following set-up is most likely to work.

  1. Get a GitHub or GitLab account
  2. Install Git for your operating system
  3. Install R (if needed)
  4. Install RStudio (if needed)
  5. Tell RStudio where git.exe is located
  6. Tell Git how to store and get your GitHub/GitLab username and password
  7. Do your first Push from the shell to store your username and password.

Git from GitHub Desktop

I normally work with Git from GitHub Desktop only. It tends not to have the authentication headaches that Git in RStudio has. However, for the class I am going to work with Git from RStudio but I will also show GitHub Desktop. GitHub Desktop doesn’t have the authentication headaches that Git in RStudio has.

  1. Get a GitHub or GitLab account
  2. Install Git for your operating system
  3. Install GitHub Desktop
  4. Install R (if needed)
  5. Install RStudio (if needed)

Next steps depend if you are a GitHub user or GitLab user:

GitHub: Login to GitHub from GitHub Desktop

GitLab:

  1. Create a PUBLIC repo on GitLab.
  2. Copy its URL.
  3. Open GitHub Desktop. Click File > Clone Repository > URL. Then paste in the URL of the GitLab repo. Pick a directory to save the repo in.
  4. Open the new repo in RStudio and make a change to a file. Save.
  5. Go back to GitHub Desktop. Commit the change.
  6. Push. A window will popup asking for your username and password.
  7. You’re done. Next time it won’t need to ask.

To use both, do both sets of steps above.

Details

GitHub and GitLab

NWFSC staff

GitHub If you are using GitHub in a work capacity, as opposed to purely non-work or school, then the repositories you post should be intended for public use (as opposed to just a personal repo). GitHub is officially allowed for a platform for release of products and provides a nice platform for that. I’ll show a few examples. There is paperwork to fill out info and checklist. Key point is that GitHub is not NOAA-controlled and sensitive information and data that needs to stay on government computers should not be on GitHub.

GitLab If you want to have repositories for work that is not intended for the public or that involve data, code, or reports that cannot be on a non-NOAA computer, then you can use the NWFSC GitLab server. Contact Marcus Nedelmann at NWFSC to get an account. No paperwork needed. Note it is easy to import back and forth between GitHub and GitLab.

Non-NOAA participants or those using GitHub/GitLab purely for non-NOAA work

If you don’t already have an account on GitHub, go to github.com and click the “sign up” link near upper right of the page. It is pretty self-explanatory. Go ahead and get a free account. The free GitHub account does not have private repositories. Everything is public and viewable. You can upgrade (for money) and get private repositories.

GitLab is quite similar to GitHub as a platform but is missing some of the nice features of creating a website for your repos. You can do it but just not as integrated. But GitLab’s free account gives you unlimited private repos.

Git for your OS

GitHub Desktop

GitHub Desktop is a nice Git interface for basic work. Despite the name, GitHub Desktop works just as well with GitLab.

  1. Go to desktop.github.com and install.
  2. Open GitHub Desktop, go to ‘GitHub Desktop > Preferences’ menu.
  3. Under ‘Accounts’, sign into your GitHub account.
  4. Under ‘Git’, enter your name (or initials) and the email you used for your GitHub account.

If you are using GitLab:

  1. Create a PUBLIC repo on GitLab.
  2. Copy its URL.
  3. Open GitHub Desktop. Click File > Clone Repository > URL. Then paste in the URL of the GitLab repo. Pick a directory to save the repo in.
  4. Open the new repo in RStudio and make a change to a file. Save.
  5. Go back to GitHub Desktop. Commit the change.
  6. Push. A window will popup asking for your username and password.
  7. You’re done. Next time it won’t need to ask.

R and RStudio

  • R: Make sure you have a fairly recent version of R.
    Go to CRAN and find the download link for your computer system. Those using a NOAA computer, contact IT to help you update/install.

  • RStudio: Install the latest version of RStudio. Get it from RStudio and install the appropriate one for your operating system (Windows or Mac). Again contact IT to help install if you are on a NOAA computer.

Set up RStudio to use Git

If you want to use Git within RStudio, do these steps. Note it is not necessary to use Git in RStudio. You can use GitHub Desktop (or any other Git GUI or the terminal command line).

  1. Open RStudio
  2. Go to Global Options (from the Tools menu)
  3. Click Git/SVN
  4. Click Enable version control interface for RStudio projects
  5. You will need the location of the git executible. On my mac the value in the Git executible box is /usr/bin/git. On my PC, it is C:/Program Files/Git/bin/git.exe. If you install Git for Windows locally, it may be at C:\Users\your.username\AppData\Local\Programs\Git\bin\git.exe.

Tell Git how to get your username and password for GitHub/GitLab

  1. Open a terminal window.
  2. Type git config --get credential.helper
  3. If you are on a PC, you should see manager and if on a mac, osxkeychain
  4. If not type, Mac: git config --global credential.helper osxkeychain Windows: git config --global credential.helper manager

There is one more step that comes up on your first push to the remote. You’ll need to do your very first Push from the Shell within RStudio so that it knows your username and password.

  1. Open up your local repo in RStudio.
  2. Make a change to a file and commit the change.
  3. Click on the Git tab, click the cog icon and click Shell...
  4. Type git push
  5. It should ask for your GitHub or GitLab username and password.

GitLab and SSH keys and SSL tokens

In order to connect to GitLab, you will need to make an SSH key. GitLab will ask you for this. Note, I am not 100% sure GitLab actually needs this. If it let’s you, you can try skipping this.

Windows users: The txt file with the public token will be xxx.pub and Windows sees that as a MSPublisher file. Don’t double-click the file to open. Use Open With, and select a text editor.

  1. Open a terminal
  2. Type this ssh-keygen -t ed25519 -C "gitlab"
  3. Hit ‘return’ at each prompt (for filename and for password). Don’t enter anything.
  4. Ignore what is printed out. Navigate to Users/yourname/.ssh/id_ed25519
  5. Open with an editor (like Notepad or TextEdit)
  6. Copy the key. It’s long and begins with ssh-ed25519.
  7. Paste into the page where GitHub asks for it. Lost that place? It’s in Settings>SSH keys

Help!

Can’t get Git, GitHub/GitLab, and RStudio to connect?

First off, can you get GitHub Desktop to work? If so, I would say just use that for awhile. You don’t need to use Git in RStudio.

If you want to use Git in RStudio, read on. You can also consult this great online resource.

There are two typical problems. 1. Git doesn’t know how to ask you for your username and password. 2. Your repo is pointing to the wrong GitHub/GitLab account. This can happen when you try to copy someone else’s account.

Does RStudio or GitHub Desktop complain about no SSL tokens when you try to Push?

  1. Open a terminal window.
  2. Paste in this code git config --global http.sslVerify false

Does RStudio complain about needing access to your credentials?

  • First, you should have a fairly recent version of RStudio because the Git integration has gotten better.
  • If you have a pop-up window, click ‘Deny’. Hopefully a terminal window pops up and asks for your GitHub or GitLab username and password.

Does RStudio say it cannot find bash.exe?

RStudio doesn’t know where git.exe is. First, have you installed Git yet?

If you are on Windows and you have not installed Git, install Git for Windows. Mac users should have it already. Mac It should be installed. Open a terminal window and type, git –version. If it asks to install XCode, say Yes.

Next, tell RStudio where to find git.exe. You need to tell it under Tools > Global Options > Git/SVN > Git executible. On my mac the value in the Git executible box is /usr/bin/git. On my PC, it is C:/Program Files/Git/bin/git.exe but IT installed it for me and installed for all users (so with admin). If you install Git for Windows locally (meaning you install it just for your account instead of for all users on your computer), it should be at C:\Users\your.username\AppData\Local\Programs\Git\bin\git.exe

Does RStudio ask for your username and password every time you try to push?

You need to do a push from the terminal to store your username/password.

  1. Open up your project in RStudio and click the Git tab.
  2. Click on the cog icon and click Shell...
  3. Type git config --get credential.helper and make sure you see manager (or wincred) if on Windows and osxkeychain if on Mac.
  4. If not, look above for the git command to add those credential helpers.

When you try to push with RStudio do you get an error with askpass?

RStudio doesn’t know how to open a terminal to ask for your username and password. You’ll need to push from the shell the first time.

So what’s going on is that Git (within RStudio) doesn’t know how to ask you for your GitHub username and password.

  1. Open up RStudio and make sure your project is showing in the top right with the blue cube with R in it (meaning you have that RStudio project loaded).
  2. Make a change to a file in your project and save.
  3. Click on the Git tab in the upper right window.
  4. Commit that change by clicking on the little box next to the change (in the Git window), adding a comment and clicking Commit.
  5. Click on the little cog and select Shell…
  6. Type git config --list. You should see credentials.helper=manager (if on Windows) or credentials.helper=osxkeychain (if on Mac). If not type, one of these commands. Mac: git config --global credential.helper osxkeychain Windows: git config --global credential.helper manager.
  7. Now Type git push. You should be asked for your GitHub username and password.
  8. Now make another change to a file, commit the change, and try to Push with the Green arrow.

Still no luck authenticating?

Git might have the wrong remote.

  1. Open up your project in RStudio.
  2. Click the Git tab. Then click on the cog icon and click Shell...
  3. Type git remote -v and see if what is listed looks correct.
  4. If not, the command to set the new remote is git remote set-url origin URL Replace URL with the URL of your repository!!

Week 2: Installing packages from GitHub/GitLab

Windows people: You will need to install Rtools.

Run this code to confirm that you can install packages from GitHub:

devtools::install_github("RVerse-Tutorials/TestPackage")

To install from the gitlab.com the command is similar. <gitlab account> is a dummy. Replace with the real values.

devtools::install_gitlab("<gitlab account>/repo name")

To install from an internal GitLab server, like the NWFSC GitLab, use this. Note for the NWFSC GitLab, you need to be on the network since this is an internal (not public) server. Again, replace the dummy variables with the real values.

devtools::install_gitlab("<gitlab account>/repo name", host="<url of the server>")

(Optional) Packages with C++ code

We will not be doing this, but often you will want to install packages with C++ code. To do that, you will need Rtools (Windows) or Xcode (Mac)

  • Rtools Windows users Rtools also so you can build packages with C++ code if needed. See comments here about changing path. Rtools

  • Xcode Mac users Open terminal and type the following command xcode-select --install


NWFSC Math Bio Program