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:
- After installing Git (see Installing Git for Windows below), use Git Bash
- Search for “Git Bash” in the Start menu
- 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.
pwdExample output:
/Users/yourname
ls - List Files
Lists files and folders in the current directory.
lsTo see more details (permissions, size, date modified):
ls -lTo see hidden files (files starting with .):
ls -acd - Change Directory
Navigates to a different folder.
cd foldernameUseful shortcuts:
cd ~- Go to your home directorycd ..- Go up one level (to the parent folder)cd ../..- Go up two levelscd -- Go back to the previous directory
Examples:
cd Documents # Enter the Documents folder
cd ~/Desktop # Go to Desktop from anywhere
cd .. # Go up one foldermkdir - Make Directory
Creates a new folder.
mkdir newfolderTo create nested folders at once:
mkdir -p Github/projects/iamlmv - 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 renamecp - 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 ~/githubStep 3: Navigate to this folder:
cd ~/githubAll 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-nameThis 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 pullImportant: 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)
Open your terminal
Create your Github folder (if you haven’t already):
mkdir ~/github- Navigate to the Github folder:
cd ~/github- Clone the course repository:
git clone https://github.com/jjcurtin/book_iaml- Verify the clone worked:
lsYou should see book_iaml listed.
Getting Updates (Do Periodically)
When instructed to pull new materials:
- Navigate to the repository:
cd ~/github/book_iaml- Pull the latest changes:
git pullYou should see a summary of files that were updated, or “Already up to date” if there are no new changes.
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
- Go to https://git-scm.com/downloads/win
- 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
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
Open Git Bash (search for it in the Start menu)
Type the following command and press Enter:
git --versionYou should see output like:
git version 2.43.0.windows.1
- Test that shell commands work:
pwd
lsAdding 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
cdto navigate into the cloned repository before runninggit pull.
“Permission denied”
- On macOS/Linux: You may need to add
sudobefore 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 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: