Git and Shell Commands

This appendix provides a mini-tutorial on basic shell (command line) commands and Git. These skills are essential for this course because lab materials and course resources are available via a GitHub repository that you will need to clone and periodically pull updates from.

More generally, they are both essential tools for data science that you should being to incorporate into your data/code management and analysis workflows.

Opening the Terminal

The terminal (also called the command line or shell) is a text-based interface for interacting with your computer.

You can open an independent terminal window as follows:

  • macOS
    • Open Spotlight (Cmd + Space) and type “Terminal”, then press Enter
    • Or navigate to Applications > Utilities > Terminal
  • Windows:
  • Linux
    • Press Ctrl + Alt + T
    • Or search for “Terminal” in your applications menu

However, the preferred way to use the terminal for this course is via RStudio’s built-in terminal tab. It provides a fully functioning terminal within the RStudio interface.

Essential Shell Commands

Below are the fundamental commands you will use to navigate your file system.

pwd - Print Working Directory

Shows your current location in the file system.

pwd

Example output:

/Users/yourname

ls - List Files

Lists files and folders in the current directory.

ls

To see more details (permissions, size, date modified):

ls -l

To see hidden files (files starting with .):

ls -a

cd - Change Directory

Navigates to a different folder.

cd foldername

Useful shortcuts:

  • cd ~ - Go to your home directory
  • cd .. - Go up one level (to the parent folder)
  • cd ../.. - Go up two levels
  • cd - - Go back to the previous directory

Examples:

cd Documents          # Enter the Documents folder
cd ~/Desktop          # Go to Desktop from anywhere
cd ..                 # Go up one folder

mkdir - Make Directory

Creates a new folder.

mkdir newfolder

To create nested folders at once:

mkdir -p Github/projects/iaml

mv - Move or Rename a folder or file

Moves a file or folder to a new location, or renames it.

mv oldname newname              # Rename a file
mv file.txt Documents/          # Move file to Documents folder
mv file.txt Documents/newname.txt  # Move and rename

cp - Copy a folder or file

Copies a file or folder to a new location or name. The original file remains unaltered.

cp origfile newfile     # copies a file
cp file.txt Documents/  # copies a file to Documents folder with same file name
 
cp file.txt Documents/newname.txt  # copies a file to Documents with new filename 
cp -r origfolder newfolder   # copies a folder and its contents 

rm - Remove/Delete a file or folder

Deletes a file or folder. Be careful—this action is permanent and cannot be undone.

rm oldname.txt   # deletes a file
rm -r foldername    # deletes a folder and its contents 

Setting Up Your GitHub Folder

We recommend creating a dedicated folder for all your GitHub repositories. This keeps your projects organized and makes navigation easier.

Step 1: Open your terminal

Step 2: Create a github folder in your home directory:

mkdir ~/github

Step 3: Navigate to this folder:

cd ~/github

All repositories you clone should go inside this folder.

Git Basics

Git is a version control system that tracks changes to files. GitHub is a website that hosts Git repositories online, allowing for sharing and collaboration.

For this course, you primarily need two Git commands:

git clone - Clone a Repository

Downloads a repository from GitHub to your computer. You only need to do this once per repository.

git clone https://github.com/username/repository-name

This creates a folder with the repository name containing all the files.

git pull - Pull Updates

Downloads the latest changes from GitHub. Use this to get updated materials after the initial clone.

git pull

Important: You must be inside the repository folder when running git pull.

Example Workflow: Getting Course Materials

Here is the complete workflow for setting up and maintaining your course materials:

Initial Setup (Do Once)

  1. Open your terminal

  2. Create your Github folder (if you haven’t already):

mkdir ~/github
  1. Navigate to the Github folder:
cd ~/github
  1. Clone the course repository:
git clone https://github.com/jjcurtin/book_iaml
  1. Verify the clone worked:
ls

You should see book_iaml listed.

Getting Updates (Do Periodically)

When instructed to pull new materials:

  1. Navigate to the repository:
cd ~/github/book_iaml
  1. Pull the latest changes:
git pull

You should see a summary of files that were updated, or “Already up to date” if there are no new changes.

Always Pull Before Working

You should always pull before working within a repository, especially within a shared repository. This ensures you have the latest version of files and helps avoid merge conflicts.

Quick Reference

Command Description Example
pwd Show current directory pwd
ls List files ls -l
cd Change directory cd ~/Github
mkdir Create folder mkdir newfolder
mv Move/rename mv old.txt new.txt
cp copy cp old.txt new.txt
rm`` | delete/remove |rm old.txt| |git clone| Download repository |git clone | |git pull| Update repository |git pull`

Installing Git for Windows

If you are using Windows, you need to install Git to access both Git commands and a Unix-like terminal (Git Bash).

Download

  1. Go to https://git-scm.com/downloads/win
  2. Select the appropriate installer:
    • 64-bit Git for Windows Setup — Choose this for most Windows computers (Intel or AMD processors)
    • ARM64 Git for Windows Setup — Only choose this if you have a Surface Pro X or a device with a Qualcomm/Snapdragon processor
    If unsure, choose the 64-bit version.

Installation

Run the downloaded installer. The default options work well for most users, and you can click “Next” through most screens. A few screens worth noting:

  • Default Editor: I recommend using the Nano editor over Vim, when you are new to the CLI. However, vim is arguably the most powerful text editor available in the terminal and worth learning eventually as you become a power user!!
  • PATH Environment: Select “Git from the command line and also from 3rd-party software” (this is the recommended default)
  • HTTPS Transport Backend: Use the OpenSSL library. It’s bundled with Git, and is simpler and more consistent for your likely use case.
  • Terminal Emulator: Select “Use MinTTY” (the default)

For all other options, the defaults are fine. Click Install and wait for completion.

Verify Installation

  1. Open Git Bash (search for it in the Start menu)

  2. Type the following command and press Enter:

git --version

You should see output like:

git version 2.43.0.windows.1
  1. Test that shell commands work:
pwd
ls

Adding Git Bash to R Studio in Windows

To add Git Bash as your default terminal within R Studio, simply follow:

Tools > Global Options > Terminal > “New terminals open with:”

and select Git Bash!

Using Git Bash

Git Bash provides a Unix-like terminal on Windows. All the shell commands covered in this appendix (pwd, ls, cd, mkdir, mv) work in Git Bash exactly as described.

Tip: You can right-click in any folder in Windows Explorer and select “Git Bash Here” to open a terminal already navigated to that folder.

Troubleshooting

“command not found: git”

  • On Windows: Make sure Git is installed and you’re using Git Bash, not Command Prompt
  • On macOS: Install Git via Xcode Command Line Tools by running xcode-select --install

“fatal: not a git repository”

  • You’re not inside a Git repository folder. Use cd to navigate into the cloned repository before running git pull.

“Permission denied”

  • On macOS/Linux: You may need to add sudo before the command (you’ll be prompted for your password)
  • Make sure you have write permissions to the folder

Clone fails with authentication error

  • For public repositories like the course materials, no authentication should be needed
  • Ensure you’re using the HTTPS URL (starts with https://)

Learning More

If you’re curious about learning more Unix/shell commands beyond the basics covered here, the Software Carpentry tutorial is an excellent resource:

  • The Unix Shell

  • The first 7 chapters cover essential skills like navigating files, working with files and directories, pipes and filters, and shell scripts.

With respect to Git, clone and pull are just the start for using it. To learn more about Git, consider the following resources: