Welcome to the first part of my blog series where I share my journey with R, GitHub, and RStudio. It has been a long time since I did some blogging. This post serves as both a guide for others and a personal record of the steps I followed to set up a private GitHub repository integrated with RStudio. Let's dive into the process!

Why Use a Private GitHub Repository?

Using a private GitHub repository offers several advantages:

  • Confidentiality: Keep your work private until you're ready to share it with collaborators.
  • Control Access: Invite only trusted collaborators to view or contribute to your project.
  • Version Control: Track changes, revert to previous states, and manage your project's history effectively.
  • Accountability: This is on a very personal note. One of my well wisher, Dr Manoov had suggested that I use GitHub as it will help me keep myself accountable to the amount of work that I do everyday as I can see the same in real time.

Installing Git

Before setting up your GitHub repository, you need to install Git on your machine. Below are the commands for both Windows and Ubuntu. You can skip this section if you have already got git installed on your machine. Git is the software that enables us to communicate with the github servers. This is very much like the OneDrive or Google Drive clients that are installed on our computers for syncing our files. Although there are other solutions like the github desktop and other tools, I have found the control that is there when we are using the command line unsurpassed.

Installing Git on Windows

  1. Download the Git installer from the official website: Git for Windows.
  2. Run the installer and follow the prompts. You can use the default settings unless you have specific preferences.
  3. After installation, open PowerShell or Command Prompt and verify the installation:
    PowerShell
    
    git --version
            

    You should see the installed Git version, e.g., git version 2.40.0.windows.1.

Installing Git on Ubuntu

  1. Open the terminal.
  2. Update your package list:
    Bash
    
    sudo apt update
            
  3. Install Git:
    Bash
    
    sudo apt install git
            
  4. Verify the installation:
    Bash
    
    git --version
            

    You should see the installed Git version, e.g., git version 2.25.1.

Initializing Git in Your Project Folder

Once Git is installed, the next step is to initialize it in your project folder.

  1. Navigate to your project directory:
    Bash
    If there are spaces in your path, on Windows machines in particular, remember to wrap the path inside double quotes "".
    
    cd /path/to/your/project
            
  2. Initialize Git:
    Bash
    
    git init
            

    This command creates a hidden .git directory that tracks your project's version history.

Creating a GitHub Repository

Now, let's create a private repository on GitHub and link it to your local project.

Steps to Create a GitHub Repository

  1. Log in to your GitHub account. If you don't have one, [sign up here](https://github.com/join).
  2. Click on the + icon in the top-right corner and select New repository.
  3. Fill in the repository details:
    • Repository name: Choose a meaningful name for your project.
    • Description: (Optional) Provide a brief description of your project.
    • Visibility: Select Private to keep your repository confidential.
  4. Click Create repository.

Understanding Private vs. Public Repositories

Choosing between a private and public repository depends on your project's needs:

  • Private Repository: Only you and the collaborators you invite can view or contribute to the repository. Ideal for confidential or in-progress projects.
  • Public Repository: Anyone can view the repository. Suitable for open-source projects or when you want to share your work with the community.

Linking Your Local Repository to GitHub

  1. After creating the repository on GitHub, you'll see instructions to link it with your local repository.
  2. Add the remote repository:
    Bash
    
    git remote add origin git@github.com:yourusername/your-repo.git
            

    Replace yourusername and your-repo with your GitHub username and repository name, respectively.

  3. Verify the remote repository:
    Bash
    
    git remote -v
            

    You should see output like:

    
    origin  git@github.com:yourusername/your-repo.git (fetch)
    origin  git@github.com:yourusername/your-repo.git (push)
    
          
  4. Make your initial commit and push:
    Bash
    
    git add .
    git commit -m "Initial commit: Set up project structure"
    git push -u origin master
            

Setting Up SSH Keys for GitHub

SSH keys provide a secure way to authenticate with GitHub without using passwords. While adding an email address during SSH key generation is optional, it aids in key management and identification.

Generating a New SSH Key

  1. Open your terminal or PowerShell.
  2. Generate the SSH key:
    Bash
    
    ssh-keygen -t ed25519 -C "your_email@example.com"
    # If you prefer RSA
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
            

    The email is optional but recommended for key identification.

  3. Follow the prompts to save the key. Press Enter to accept the default file location (~/.ssh/id_ed25519 or ~/.ssh/id_rsa).
  4. Optionally, enter a passphrase for added security.

Adding Your SSH Key to the SSH Agent

One of my biggest learnings today was realizing that the SSH agent wasn't running. If you encounter this issue, here's how to restart the SSH agent and add your key:

Bash

# Start the SSH agent in the background
eval "$(ssh-agent -s)"
# Add your SSH private key to the agent
ssh-add ~/.ssh/id_ed25519
# For RSA key
ssh-add ~/.ssh/id_rsa
    

Adding the SSH Key to Your GitHub Account

  1. Copy the public key to your clipboard:
    Bash
    
    cat ~/.ssh/id_ed25519.pub
    # For RSA key
    cat ~/.ssh/id_rsa.pub
            
  2. Log in to GitHub and navigate to Settings > SSH and GPG keys.
  3. Click on New SSH key, provide a descriptive title (e.g., "Ubuntu Laptop"), and paste the copied key into the Keyfield.
  4. Click Add SSH key to save.

Testing Your SSH Connection

To ensure everything is set up correctly, test your SSH connection to GitHub:

Bash

ssh -T git@github.com
    

You should see a message like:


Hi your_username! You've successfully authenticated, but GitHub does not provide shell access.

  

Setting Up RStudio with GitHub

Integrating GitHub with RStudio enhances your workflow by providing seamless version control within your R projects.

RStudio Desktop Integration

  1. Open RStudio and navigate to Tools > Global Options.
  2. Select the Git/SVN tab.
  3. Ensure that the path to the Git executable is correctly set. On Ubuntu, it’s typically /usr/bin/git. On Windows, it might be C:\Program Files\Git\bin\git.exe.
  4. Click OK to save the settings.
  5. To clone a repository, go to File > New Project > Version Control > Git, enter the repository URL (use the SSH URL, e.g., git@github.com:username/repo.git), and follow the prompts.

RStudio Server Integration

Using RStudio Server requires slightly different steps due to its remote nature:

  1. Access your RStudio Server instance via your web browser.
  2. Open the terminal within RStudio Server.
  3. Ensure that your SSH keys are present in the ~/.ssh/ directory.
  4. Start the SSH agent and add your key:
    Bash
    
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_ed25519
            
  5. Clone your GitHub repository using the SSH URL:
    Bash
    
    git clone git@github.com:username/repo.git
            

Best Practices

  • Use Descriptive Commit Messages: Clearly describe what changes each commit includes.
  • Regular Commits: Commit changes frequently to track progress and make it easier to identify issues.
  • Use Branches: Develop features or fixes in separate branches to keep the main branch stable.
  • Integrate renv: Use renv for dependency management to ensure reproducible environments across different machines.

To sum up

Setting up a private GitHub repository and integrating it with RStudio will help to manage your R projects effectively. By following the steps outlined above, you can ensure that your work is securely version-controlled and easily accessible to your collaborators. In the next part of this series, I'll delve deeper into setting up Git on both Windows and Linux machines, troubleshooting common issues, and optimizing your RStudio environment for seamless collaboration.

 

Topics
No topics.
Created:
24th of December 2024
01:34:16 PM
Modified:
24th of December 2024
01:37:59 PM
Loading Conversation