The many faces of GitHub

This Spotlight introduces Git and GitHub, with examples using GitHub Desktop, Git Bash and RStudio.

What is Git?

Before you can understand what GitHub is, you must first understand the concept of Git:

Git

Git is distributed version control software for projects, used both individually and in teams. It records project history in a local repository, so you can inspect earlier versions and create commits without an internet connection. Collaborators can exchange commits through a shared remote repository. Git itself does not require a GitHub account. You can operate Git either in the command line of your operating system or in GitHub Desktop, a Git client with a graphical user interface.

You need to know the following commands when working with Git:

  • git clone [URL]: Clone a Git repository from the URL to your local computer.
  • git init: Initialise a Git project locally. You can optionally upload this to GitHub or another host later.
  • git add [file]: Stage the file’s current content for the next commit. The staging area (also called the index) holds the changes you have selected. If you edit the file again, run git add again to stage the newer content.
  • git commit: Record the staged changes in the local repository. A commit creates a point in the project history that you can inspect or return to later.
  • git push origin [branch]: Send the commits needed to update the named branch on the remote called origin. This shares committed work; unstaged or staged changes that have not been committed are not uploaded.
  • git fetch origin: Download commits and update remote-tracking references such as origin/main. Fetching alone does not integrate these changes into your current local branch or change its working files; you can review them before integration.

For more detail, see Git’s introduction to version control and the documentation for git add, git push and git fetch.

What is GitHub?

GitHub is the host for your Git projects and has similar features to a social network.

  • You can use Git to access your projects stored in GitHub from anywhere.
  • GitHub repositories can be public or private. Public repositories can be viewed by anyone; access to private repositories is restricted to people with permission. GitHub Free includes unlimited public and private repositories, although some features and usage allowances depend on the plan. See GitHub's current plans.
  • You can also contribute to existing programs by fixing bugs and thus actively contribute to open source tools.
  • GitHub is largely used for large projects in certain programming languages. However, it is just as possible to manage simple documents in GitHub.

Watch how Eddie and his team use GitHub:


Difference between GitHub and Git in a nutshell

  • Git is a distributed version control system. It supports local work, branches and collaboration through shared repositories.
  • GitHub hosts Git repositories and adds collaboration tools such as issues, pull requests and code review.
Git GitHub Octocat GitHub
Version control software A repository hosting and collaboration service
Runs locally, including on computers used as Git servers Provides hosted remote repositories and web tools
Can be used through the command line or graphical clients Can be accessed through Git, the website or clients such as GitHub Desktop
Records commits and manages branches in a repository Hosts shared repositories and supports issues, pull requests and code review

Setting up Git and GitHub

Before you can get started with Git and GitHub, you need a GitHub account and you need to install Git locally. Here’s how:

  • First, create an account on GitHub. This is pretty straightforward and done quickly without any pitfalls.
  • To use GitHub productively, you need Git on your local computer. Install Git: Choose the installer you need for your operating system.
  • On Windows, launch Git Bash. On macOS and Linux, open a terminal.
  • With git config --global user.name "[Name]" you now have to set your name.
  • As with your name, set your email address using git config --global user.email "[Mail]". Use the email address associated with your GitHub account.

GitHub tutorial: setting up a repository

If you want to get started with a project, you can create a repository in two ways:

First:

  • To create a new repository on the GitHub page click on the + next to your profile picture on the top right corner.
  • Give your project a name and choose Public or Private visibility. Private repositories are available with GitHub Free; choose the visibility appropriate for your project.
  • Then click on the Create repository button. You can then add the repository locally using git clone as described above.

Second:

  • Locally, create a repository with git init and make an initial commit after staging your files. Separately, create an empty repository on GitHub, without adding a README, licence or .gitignore there.
  • Connect the local repository to that existing remote with git remote add origin https://github.com/[username]/[projectname].git. This command records the remote address locally; it does not create a repository on GitHub.
  • Share your committed work with git push -u origin [branch-name], replacing [branch-name] with your current branch name (shown by git branch --show-current). GitHub authentication is required. The push uploads your commits to the repository you created.

See GitHub’s instructions for creating a repository and Git’s documentation for git remote add.

For a guided installation and setup, watch Git & GitHub Crash Course For Beginners from Traversy Media.


GitHub Desktop

Focus on what matters instead of fighting with Git. Whether you’re new to Git or a seasoned user, GitHub Desktop simplifies your development workflow. GitHub Desktop will allow us to easily start using version control by offering a Graphical User Interface (GUI) to use Git. A GUI allows users to interact with a program using a visual interface rather than relying on text commands.

GitHub Desktop Changes view: changed files on the left, a diff on the right, and commit summary and commit button below the file list.
GitHub Desktop interface used in this tutorial (existing course screenshot).

Step-by-step introduction to GitHub Desktop:

1. Download GitHub Desktop

GitHub Desktop download page, with the Windows download button above an application preview.
Download GitHub Desktop.

2. Log in to GitHub Desktop to connect to your GitHub account.

GitHub Desktop welcome screen: choose Sign in to GitHub.com to connect an account.
Sign in to GitHub Desktop 1/4.
GitHub.com sign-in form in GitHub Desktop with username or email, password and Sign in controls.
Sign in to GitHub Desktop 2/4.
Configure Git screen in GitHub Desktop: enter the name and email recorded in commits, then choose Continue.
Sign in to GitHub Desktop 3/4.
GitHub Desktop usage-statistics preference and Finish button at the end of setup.
Sign in to GitHub Desktop 4/4.

3. So far there is no local repository on your computer.

Empty GitHub Desktop window offering to create, add or clone a repository.
No local repositories available.

4. First, choose an empty folder to store the local data, for example C:/GITHUB/.
Second, select a repository you created on GitHub and clone it to your computer. You will then see a local repository.

GitHub Desktop File menu with Clone repository selected.
Cloning an existing repository 1/2.
Clone a repository dialog: select the remote repository and local destination, then choose Clone.
Cloning an existing repository 2/2.

5. Copy some .txt files to the local repository on your computer. GitHub Desktop shows the changes. Select the files or changes you want to include, enter a commit summary (mandatory) and an optional description, then click commit. This records a local commit; use the publish or push action in the next step to share it on GitHub.

GitHub Desktop Changes view: changed files on the left, a diff on the right, and commit summary and commit button below the file list.
Selecting changes and creating a local commit in GitHub Desktop.

6. For a branch that is not yet on GitHub, click Publish branch; for an already published branch, use Push origin to share new local commits. Your local branch and its corresponding remote branch can contain different commits. Use Fetch origin to check for remote updates. Fetching alone does not integrate them into your local branch. If updates are available, Desktop offers Pull origin to integrate them; resolve any conflicts before continuing. See the official instructions for pushing changes with GitHub Desktop.

GitHub Desktop toolbar highlighting Publish branch for sharing the local branch with the remote repository.
Sharing commits with the remote repository.

Further information: Activity: Use the GitHub Desktop client

GitHub Desktop introduction A quick intro for GitHub Desktop on Windows:


Git Bash

Git for Windows includes Git Bash, a Bash shell environment for running Git and other command-line programs. You can open it from the Windows Start menu or a folder’s context menu. Bash stands for “Bourne Again Shell”. A shell interprets typed commands; Git is a separate version control program that you can run from that shell. The Git commands are the same as those used in a terminal on Linux, macOS or other Unix systems.

Command Description
Getting & Creating Projects  
git init Initialise a local Git repository
git clone ssh://git@github.com/[username]/[repository-name].git Create a local copy of a remote repository
Basics  
git status Check status
git add [file-name.txt] Add a file to the staging area
git add -A Stage all added, modified and deleted files
git commit -m "[commit message]" Commit changes
git rm -r [file-name.txt] Remove a file (or folder)
Sharing & Updating Projects  
git push origin [branch name] Push a branch to your remote repository
git push -u origin [branch name] Push changes to remote repository (and remember the branch)
git push Push changes to remote repository (remembered branch)
git pull Fetch and integrate changes from the configured upstream branch (by merging or rebasing, depending on configuration)
git pull origin [branch name] Fetch the named remote branch and integrate it into the current local branch
Inspection & Comparison  
git log View changes
git log --summary View changes (detailed)
git log --oneline View changes (briefly)
git diff [source branch] [target branch] Preview changes before merging


Git Bash introduction Learn how to upload files to GitHub using Git Bash on Windows in this video from Amarindaz.



GitHub in RStudio

Once you’ve installed Git, you’ll need to activate it on your system by following these steps:

Part 1
Tell RStudio where to find the Git installation

  • Open RStudio and go to Tools > Global Options… click on Git/SVN
  • Check Enable version control interface for RStudio projects
  • Set the path to the Git executable that you just installed. Open a shell, if you don’t know where Git is installed.
  • Windows: type where git and hit enter. The path should be something like: C:/Program Files (x86)/Git/bin/git.exe
  • Linux/macOS: type which git and hit enter. The path should be something like: /usr/bin/git
  • Restart RStudio, if it worked out you will find the Git icon on the top toolbar, as shown below.
RStudio Global Options, Git/SVN panel, with the path to the Git executable and an SSH-key field.
Git in RStudio.

Set up Git
Configure the author name and email address that Git records in your commits. The author name does not have to match your GitHub login. Use an email address associated with your GitHub account, or your GitHub-provided no-reply address. These global settings apply to repositories for your current operating-system account unless overridden in a repository. In RStudio, go to Tools > Shell and run the following commands, replacing the example name and email address with your own:

git config --global user.name "Your Name"
git config --global user.email "name@provider.com"

See GitHub’s guidance on Git author names.

RStudio Tools menu with Shell selected, opening a terminal in the project directory.
Git in RStudio.

Part 2
Create a new RStudio project with Git

There are three ways to use version control in an RStudio project.

a) Create a new project and create a local Git repository:
Select File > New Project…, create a project from a New Directory and check the option Create a git repository. To push to a remote repository later, add that remote repository using the Git shell. If you already know which online repository you want to use for your project, option c) is more convenient.

b) Create a new project from a folder under version control:
In this case, you only need to create an RStudio project for that directory; version control is automatically enabled. Go to File > New Project…, choose Existing Directory and create the project.

c) Create a new project based on a remote Git repository:
Select File > New Project…, choose Version Control and then Git. Enter the URL of the repository you want to clone and create the project. Use its HTTPS URL if you have not configured SSH authentication.

Animation of creating an RStudio project from a Git repository: choose Version Control and Git, enter the repository URL and local directory, then create the project.
Git in RStudio.

To create an RStudio project from a GitHub repository, open the repository’s page, click Code, select HTTPS and copy the repository URL. It will look like https://github.com/yourusername/rstudio-git-test.git. See GitHub’s cloning instructions. The existing screenshot below shows an older interface, where the button was labelled Clone or download.

GitHub Clone or download menu showing the HTTPS repository URL to copy into RStudio.
Git in RStudio.

You now know the basics of Git, GitHub Desktop, GitHub, Git Bash and Git integration in RStudio.

Austin Powers meme joking that committing without first pulling remote updates is living dangerously.
Live dangerously.

Continue with the next Spotlight, CSS in R Markdown.

Updated: