97 Git: pull, status, add, commit, push

Written by Mariam Walaa and last updated on 7 October 2021.

97.1 Introduction

In this lesson, you will learn how to:

  • Create a local repository in a folder
  • Make changes to a remote repository
  • Maintain a local repository

Prerequisites include:

Highlights:

  • You can start tracking a folder of your work locally using Git.
  • You can share a local repository with team mates using GitHub.

97.2 Overview

There are two common scenarios for getting you and your team mates set up to use Git and GitHub:

  1. Your team hasn’t started the project yet, so one of your team mates needs to create a Git repository which you can clone and start pushing files to and pulling files from.
  2. You’ve already started working on the project locally and want to share it with your team mates so they can add their own work.

Both of these scenarios are described in great detail by Jenny Bryan from RStudio. You can find and follow those instructions here:

97.3 Videos

97.4 Exercises

In this section, we’ll go through exercises that help you understand the process of tracking and committing files within a local repository using Git.

We will begin by opening Git Bash, creating a new folder directory, and creating a new file as follows:

  1. Create a new folder directory: mkdir DoSS-Toolkit-Git-Demo
  2. Go to the folder directory: cd DoSS-Toolkit-Git-Demo
  3. Create a new file: touch README.md

We now have a folder directory with a README file.

97.4.1 Exercise 1

If you’ve done this correctly, you should now have a local repository in your folder.

97.4.2 Exercise 2

If you’ve done this correctly, you should now have started tracking README.md. You can check the status using git status.

97.4.3 Exercise 3

If you’ve done this correctly, you should now have a committed README.md file in your folder (i.e., Git knows about this file and has stored a version of it that you’ve decided to commit).

If you want to share this folder with your team mates, you would have to create a remote repository on GitHub. To do this, you can follow these instructions from Happy With Git R.

97.4.4 Exercise 4

97.4.5 Exercise 5

97.4.6 Exercise 6

97.5 Common Mistakes & Errors

Below are some common mistakes and errors that you may come across while using Git:

  • You try to run git commit after making changes to a file but you aren’t currently tracking that file, so you need run git add first.

  • You run git status and see that a file is listed under both Changes to be committed and Changes not staged for commit. This happens when you start tracking a file and make more changes to it before you commit it. Now, the initial version is tracked and the most recently updated version is still untracked, so they appear under both sections.

  • You try to run git push to push your updates to the remote repository while there are some new updates in the remote repository, likely from another team mate, that you don’t already have. The error you get will likely be something like:

    • error: Your local changes to the following files would be overwritten by merge: ... Please commit your changes or stash them before you merge..

    This is an issue for Git because Git has your team mate’s new updates, and now you’re telling Git to add your own updates without your team mate’s updates, so Git doesn’t know what to do. The best way to avoid this issue is to always run git pull before you start working on your files locally. However, there’s a better explanation on what to do if you run into this issue by Jenny Bryan from RStudio in this section from Happy Git With R.

97.6 Next Steps

If you would like to learn more about Git, here are some additional resources you may find helpful:

If you’ve completed this tutorial successfully and would like to learn how to use Git within RStudio, there is a helpful section in Happy Git With R about connecting RStudio to Git and GitHub.

97.7 Exercises

97.7.1 Question 1

97.7.2 Question 2

97.7.3 Question 3

97.7.4 Question 4

97.7.5 Question 5

97.7.6 Question 6

97.7.7 Question 7

97.7.8 Question 8

97.7.9 Question 9

97.7.10 Question 10