Version Control and Collaboration

Git & GitHub

Weekly design


Pre-class video


First Steps in Learning the Use of Git & GitHub in RStudio



Course materials and books


Class: Version Control and Collaboration in R


We cover this topic with this link:

https://biostats-r.github.io/biostats/github/


Introduction

Version control is an essential skill for any data scientist or programmer, especially when working in teams. It helps manage changes to your codebase and facilitates collaboration. Git is a popular version control system, and GitHub is a web-based platform that uses Git to host and manage code repositories. R Studio, a powerful integrated development environment (IDE) for R, integrates seamlessly with Git and GitHub, making version control and collaboration straightforward.


Objectives

By the end of this tutorial, you will be able to:

  • Understand the basics of Git and GitHub.

  • Set up Git and GitHub with R Studio.

  • Perform essential version control operations within R Studio.

  • Collaborate with others using GitHub.


1. Understanding Git and GitHub

What is Git?

Git is a distributed version control system that tracks changes in source code during software development. It allows multiple developers to work on a project simultaneously without overwriting each other’s work.

Key Concepts:

  • Repository: A directory that contains your project files and the entire history of changes made to them.

  • Commit: A snapshot of your repository at a specific point in time.

  • Branch: A parallel version of your repository. The main branch is often called master or main.

  • Merge: Combining changes from different branches.


What is GitHub?

GitHub is a web-based platform that hosts Git repositories. It provides a collaborative environment with features like issue tracking, project management, and more.


2. Setting Up Git and GitHub with R Studio

Prerequisites

Configuring Git in R Studio

  1. Install Git: Download and install Git from Git’s official website. Follow the installation instructions for your operating system.

  2. Configure Git: Open a terminal or command prompt and set your Git username and email:

  3. Linking Git with R Studio: Open R Studio, go to Tools > Global Options > Git/SVN. Ensure the Git executable is correctly detected. If not, manually browse to the location of the Git executable.

  4. Cloning a Repository: In R Studio, go to File > New Project > Version Control > Git. Enter the URL of a GitHub repository you wish to clone. This will create a local copy of the repository on your machine.


3. Performing Essential Version Control Operations

Creating a Repository

  1. Create a new project: Go to File > New Project > New Directory > New Project.

  2. Initialize Git: Check the box that says Create a git repository.

  3. Commit Changes:

    • Make some changes to your files.

    • Open the Git pane in R Studio.

    • Select the files you want to commit, write a commit message, and click Commit.

  4. Push to GitHub:

    • Open the terminal in R Studio.

    • Add the remote repository URL:

    • Push your changes:

Branching and Merging

  1. Create a New Branch:
  1. Merge Changes:

    • Switch back to the master branch:
    • Merge the new branch:
  2. Resolving Conflicts

Conflicts occur when changes are made to the same part of a file on different branches. Git will mark the conflict in the file, and you need to manually resolve it before committing the changes.


4. Collaborating with Others

Forking and Pull Requests

  1. Fork a Repository: On GitHub, navigate to the repository you want to contribute to and click Fork.

  2. Clone Your Fork: Clone the forked repository to your local machine:

  3. Create a Branch and Make Changes:

    • Create a new branch:

    • Make your changes and commit them.

  4. Push Your Changes:

  5. Create a Pull Request: Go to the original repository on GitHub and click New pull request. Select your branch and submit the pull request for review.

Collaborating within a Team

  • Pull Changes: Regularly pull changes from the main repository to keep your local repository up to date:

  • Reviewing Code: Use GitHub’s review feature to comment on and approve changes made by your collaborators.