04. Setting up Git and GitHub
In this guide, we will cover the basics of setting up Git and GitHub. We will also cover the basics of using Git and GitHub to manage a project. This guide is intended for beginners who are new to Git and GitHub. It is also intended for those who are new to using Git and GitHub with R.
Introduction
This textbook places a heavy emphasis on reproducible research. The most important thing you can do to ensure reproducibility is to use a version control system and a hosting service. For most people, the best option is Git and GitHub.
What is Git, Github? And why should I use it?
Git is a version control system. It allows you to track changes to files and folders over time. It also allows you to collaborate with others on projects. Think of it as MS Word’s “Track Changes” feature on steroids.
Git is a command line tool, but there are also GUIs to interact with Git in a more user-friendly way. Git is a great tool for managing projects. It is especially useful for managing projects that involve multiple people.
GitHub is a web-based hosting service for Git repositories. It allows you to store your Git repositories in the cloud. It also allows you to collaborate with others on projects by sharing projects. GitHub is a great place to store and share your code. It is also a great place to find code that others have shared.
Combining Git with GitHub allows you to store your Git managed project repositories in the cloud. This means that you can access your repositories from anywhere. It also means that you can collaborate with others on projects. You can also use GitHub to share your code with others. This is especially useful for making your projects reproducible.
How do I set up Git and GitHub?
Install and setup Git
The process for installation and setup will differ based on what operating system you are using. If you are using a Windows machine, you will likely need to install Git. If you are using a Mac or Linux machine, you will likely already have Git installed.
Windows users can install Git by downloading the installer from https://git-scm.com/downloads. Once you have downloaded the installer, you will need to run it. You will need to follow the instructions in the installer to complete the installation.
Terminal pane in RStudio
Mac and Linux users can verify that Git is installed by opening a terminal window and typing git --version
. If Git is installed, you will see a version number. If Git is not installed, you will see an error message. If you need to install Git, you can do so following these instructions:
- Mac users can install Xcode Command Line Tools by running
xcode-select --install
in the terminal. This will install Git along with other tools that may be useful for development. - Linux users can install Git using the package manager for their distribution. For example, Ubuntu users can install Git by running
sudo apt-get install git
in the terminal.
Once you have Git installed, you will need to set up your Git configuration. Most of the defaults will be fine for now, but you will need to at least set your name and email address, this will be the same address you use to create your GitHub account, so choose accordingly.
You can do this by opening a terminal window and entering the following commands (changing the name and email address to your own):
git config --global user.name "Your Name"; \
git config --global user.email "your.email@email.edu"
Alternatively, you can use {usethis}(Wickham et al. 2024) in R to set up Git in your environment. Open RStudio, install and/ or load {usethis}, and run the following code in the console:
use_git_config(user.name = "Jerid Francom", user.email = "francojc@wfu.edu")
How do I set up GitHub?
To set up GitHub, you will need to create an account. You can do this at [github.com(https://github.com). Be sure to use the email address you used in the Git setup!
The service is free and there are extra features available for students and educators. Once you have created an account, you will be able to create repositories. You can also create organizations and teams. You can use these to collaborate with others on projects.
Understanding the basic Git and GitHub workflow
Once your Git installation and Github account are set up, a number of options for working with and managing your project are available to you. At this point, let’s focus on one typical scenario that you will encounter early on in this textbook.
Some definitions are in order.
First, let’s define some key nouns:
- Repository: This is a collection of folders (directories) and files in a project that are managed by Git.
- Remote Repository: This is the repository that is stored on GitHub. It can be your repository or a repository owned by someone else. These repositories can be public, accessible by anyone, or private, accessible only by those with permission.
- Local Repository: This is the repository that is stored on your local machine. This is where you will make changes to your project. The local repository may be a copy of a remote repository or a new repository that you create that is not stored on GitHub (yet, or ever).
Now, some key verbs:
- Clone: This is the process of copying a remote repository to your local machine.
- Edit: This is the process of making changes to the files in your local repository.
There are many other actions that you can perform with Git and GitHub, but these are sufficient to get you started with this textbook.
How do I manage my project with Git and GitHub?
Let’s describe the step-wise process in Scenario A, visualized in the diagram above. This scenario involves cloning a remote repository to your local machine and then making changes to the files in the local repository. Conveniently, this is the process you will follow when working on a lab assignment in this textbook.
- Navigate to the repository on GitHub
- Click on the ‘Code’ button and copy the clone URL (https) to your clipboard.
- Open RStudio
- From the menu, select File > New Project > Version Control > Git
- Paste the URL of the repository into the ‘Repository URL’ field
- Choose the directory location for the project
- Click ‘Create Project’
RStudio will then clone the repository to your local machine and open the project as a new RStudio project. You can now make changes to the files in the project.
Summary
In this guide, we covered the basics of setting up Git and GitHub. We also covered the basics of using Git and GitHub to manage a project. We discussed the purpose of Git and GitHub, and why you should use them. We also discussed how to install and set up Git, how to set up GitHub, and the basic Git and GitHub workflow for managing a project.
We will return to Git and GitHub later in the textbook to cover more functionality and use cases.