Version Control and Collaboration
Git & GitHub
Pre-class video
First Steps in Learning the Use of Git & GitHub in RStudio
Course materials and books
How to Use Git/GitHub with R
https://rfortherestofus.com/2021/02/how-to-use-git-github-with-r
Book: Happy Git and GitHub for the useR
Other courses and materials for further studies
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
ormain
.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
Install R
Install R Studio
Install Git
Create a GitHub account
Configuring Git in R Studio
Install Git: Download and install Git from Git’s official website. Follow the installation instructions for your operating system.
Configure Git: Open a terminal or command prompt and set your Git username and email:
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.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
Create a new project: Go to
File
>New Project
>New Directory
>New Project
.Initialize Git: Check the box that says
Create a git repository
.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
.
Push to GitHub:
Open the terminal in R Studio.
Add the remote repository URL:
- Push your changes:
Branching and Merging
- Create a New Branch:
Merge Changes:
- Switch back to the
master
branch: - Merge the new branch:
- Switch back to the
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
Fork a Repository: On GitHub, navigate to the repository you want to contribute to and click
Fork
.Clone Your Fork: Clone the forked repository to your local machine:
Create a Branch and Make Changes:
Create a new branch:
Make your changes and commit them.
Push Your Changes:
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.