Lab 1: Git and GitHub
This session introduces version control with Git and GitHub. Setting up these tools first means you can track your work from day one.
Week 1 software requirement
Bring your laptop in week 1 and confirm Git/GitHub access. Install both R and RStudio in week 1, and use RStudio as the standard editor for this course. Instructions are in Lab 2: Install R and Set Up Your IDE.
What is version control?
Version control tracks every change you make to your files. Instead of
saving e.g. report_v2_final_FINAL.docx, you save a single file and Git
remembers its entire history. You can go back to any previous version,
see exactly what changed, and collaborate without overwriting each
other's work.
GitHub is a website that hosts your Git repositories online. It serves as a backup and lets you share your work. There are other services like GitLab and Bitbucket, but GitHub is the most popular.
Why bother?
- Your lab diary and final report will be easier to manage.
- You will never lose work (every version is saved).
- Employers value version control skills.
- It is the standard tool for reproducible research -- much more powerful than OSF presentations because every change is tracked and time-stamped.
Step 1: Create a GitHub account
- Go to https://github.com.
- Click Sign up and follow the prompts. (Get the student version -- see below).
- Choose a username you are happy to use professionally (e.g.,
jsmith-nz, notxXx_gamer_xXx). - Verify your email address.
Student benefits
Apply for the GitHub Student Developer Pack with your university email. It includes free access to GitHub Copilot, cloud credits, and other developer tools.
Step 2: Install Git
macOS
Open Terminal (search for it in Spotlight) and type:
git --version
If Git is not installed, macOS will prompt you to install the Xcode Command Line Tools. Follow the prompts.
Windows
Download Git from https://git-scm.com/download/win. Run the installer and accept the defaults.
Verify installation
Open a terminal (Terminal on macOS, Git Bash on Windows) and type:
git --version
You should see something like git version 2.44.0.
Step 3: Configure Git
Tell Git your name and email (use the same email as your GitHub account):
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Step 4: Set up SSH authentication
Before you can push code, GitHub needs to verify who you are. SSH keys let your computer prove its identity without a password each time. You do this once and it works from then on.
Check whether you already have a key
Many students already have an SSH key from a previous course,
internship, or Git setup. Before creating a new one, check what is
already in your ~/.ssh/ folder:
ls -la ~/.ssh
If you see files such as id_ed25519 and id_ed25519.pub, you may
already have a usable key. If you are unsure, it is fine to create a
fresh key for this course. GitHub allows more than one SSH key on an
account.
Generate an SSH key
Open a terminal and run:
ssh-keygen -t ed25519 -C "your.email@example.com"
Use the email address attached to your GitHub account.
You will see prompts like these:
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/yourname/.ssh/id_ed25519):
Press Enter to accept the default location unless you already have a
key there and want to keep it. If you already have an id_ed25519 key
and want a separate course key, you can type a different file name such
as ~/.ssh/id_ed25519_psyc434.
Next you will be asked about a passphrase:
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
You may press Enter twice to skip it. A passphrase adds security, but it also means you may need to unlock the key when you restart your computer.
This creates two files in ~/.ssh/:
| File | What it is | Share it? |
|---|---|---|
id_ed25519 | Your private key | Never. Do not copy, email, upload, or commit this file. Anyone who has it can impersonate you. |
id_ed25519.pub | Your public key | Yes. This is what you give to GitHub. |
Protect your private key
The file
~/.ssh/id_ed25519(no.pub) is your private key. Treat it like a password. Never paste it into a chat, never commit it to a repository, never upload it anywhere. If you suspect it has been exposed, delete the key from your GitHub account immediately at github.com/settings/keys and generate a new one.
Confirm that the files were created
Run:
ls -l ~/.ssh
You should see both the private key and the public key. If you saved the
key under a custom name, look for that name instead of id_ed25519.
Add the key to the SSH agent
The SSH agent remembers your key so Git can use it automatically.
Start the agent:
eval "$(ssh-agent -s)"
Then add your key:
ssh-add ~/.ssh/id_ed25519
If you used a custom file name, replace id_ed25519 with that name.
On some macOS systems, the following command is preferred because it stores the passphrase in the keychain:
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
If that command gives an error, use the plain
ssh-add ~/.ssh/id_ed25519 command instead.
Add the public key to your GitHub account
Copy the public key (the file ending in .pub) to your clipboard.
macOS:
pbcopy < ~/.ssh/id_ed25519.pub
Windows (Git Bash):
cat ~/.ssh/id_ed25519.pub
Select and copy the output manually.
If pbcopy does not work on your system, you can also print the key
manually:
cat ~/.ssh/id_ed25519.pub
Copy the entire line beginning with ssh-ed25519.
Then add it to GitHub:
- Go to github.com/settings/keys.
- Click New SSH key.
- Give it a title (e.g., "My laptop").
- Paste the key into the Key field.
- Click Add SSH key.
Test the connection
ssh -T git@github.com
The first time, you will usually see a message like:
The authenticity of host 'github.com (IP ADDRESS)' can't be established.
ED25519 key fingerprint is ...
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Type yes and press Enter. You should then see:
Hi your-username! You've successfully authenticated, but GitHub does not provide shell access.
That message means it worked. All future git push and git pull
commands will authenticate automatically.
Common problems
If you get Permission denied (publickey)
This usually means one of four things:
- You copied the wrong key to GitHub.
- You copied the private key instead of the public key.
- Your key was not added to the SSH agent.
- You are using a different GitHub account from the one attached to the key.
Work through these checks:
ls -l ~/.ssh
ssh-add -l
cat ~/.ssh/id_ed25519.pub
Then compare the printed public key with the one shown at github.com/settings/keys.
If ~/.ssh/id_ed25519.pub does not exist
The key pair was not created where you think it was. Run ls -la ~/.ssh
and look for the file name you chose during ssh-keygen.
If you see Agent admitted failure to sign
Run:
ssh-add ~/.ssh/id_ed25519
and then test again with ssh -T git@github.com.
If
ssh -Thangs or failsSome university networks block SSH. If you are on campus Wi-Fi and the command hangs, try from a different network (e.g., your phone hotspot). If it still fails, contact the course coordinator.
Step 5: Create a private repository on GitHub
- Go to github.com/new.
- Name your repository
psy434-labs(or similar). - Set visibility to Private.
- Check Add a README file.
- Click Create repository.
Privacy and submission
Your GitHub repository must remain private for the duration of the course. Do not change its visibility to public. Lab diaries are submitted as
.mdfiles via NUKU, not through GitHub. GitHub is your version-control tool; NUKU is where marking happens. Submission instructions for each lab diary appear on the NUKU assignment page.
Step 6: Clone the repository to your computer
Cloning downloads a copy of the repository to your machine and links it to GitHub.
- On your repository page, click the green Code button.
- Select SSH and copy the URL (it starts with
git@github.com:). - Open a terminal and navigate to where you want to store your work:
mkdir -p ~/Documents/psy434
cd ~/Documents/psy434
- Clone the repository:
git clone git@github.com:YOUR-USERNAME/psy434-labs.git
Replace YOUR-USERNAME with your GitHub username.
- Move into the repository folder:
cd psy434-labs
You now have a local copy linked to GitHub.
Sanity check that you are in the right place:
pwd
ls
You should see your location end with psy434-labs, and you should see
the files in your repository (it may be empty at first).
If you are new to Terminal
These commands help you check where you are and what files you have:
pwd # print the current folder (your location) ls # list files in the current folder cd .. # go up one folder cd ~ # go to your home folderIf you ever see an error like "No such file or directory", run
pwdandlsto check your location and spelling.
Checkpoint
If you have cloned your repository successfully, you are on track. Everything below can be finished before next week's lab if you run out of time.
Step 7: The basic Git workflow
The everyday workflow has three steps: stage, commit, push.
Recommended editor
RStudio is the recommended editor for creating and editing
.md,.R, and.qmdfiles. It reduces file-extension errors (for example, accidentally savingREADME.md.txt) and all lab instructions assume RStudio. Install instructions are in Lab 2. You may use another editor, but you will need to adapt instructions yourself.
1. Create your first file
Your repository already has a README.md from Step 5. Open it and
replace its contents with:
# PSYCH 434 lab diary
This is my lab diary for PSYCH 434 (2026).
Save the file in the root of your repository folder (psy434-labs/).
Use RStudio if available (File > New File > Text File), then save as
README.md.
Windows file extensions
If you create the file in File Explorer, make sure it is named
README.md(notREADME.md.txt). If you cannot see extensions, turn on "File name extensions" in File Explorer.
Creating a file from the command line
If you are already in your repository folder, you can create an empty file with:
touch README.mdThen open it in a text editor and paste in the text above. If you are not sure you are in the right place, run
pwdand check that the folder name ends withpsy434-labs.
2. Check what changed
Before staging, check what Git sees:
git status
You should see README.md in red under "Untracked files". This command
is your best friend: run it whenever you are unsure what state your
repository is in.
A typical first output looks something like this:
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present
Read this output slowly:
On branch maintells you which branch you are working on.Untracked filesmeans Git sees the file, but is not yet tracking it.- The suggested
git addcommand tells you the next step.
If you see "not a git repository"
You are not in your repository folder. Run
pwdandls, thencd psy434-labsand trygit statusagain.
3. Stage the change
Staging tells Git which changes you want to include in your next snapshot:
git add README.md
Run git status again immediately afterward:
git status
Now README.md should appear in green under a heading such as
Changes to be committed. That means the file is staged and ready for
the next commit.
You can inspect exactly what is staged with:
git diff --staged
This command shows the line-by-line changes that will go into the commit. It is a good habit to check this before every commit.
Safety: always name the files you are staging
git add README.mdstages one file. You know exactly what will be committed. Commands likegit add .orgit add -Astage everything Git can see in the current directory and below. If you run one of these from the wrong folder, you can accidentally stage thousands of files, including private data, SSH keys, or your entire home directory. One student rangit add ..(note the two dots, meaning the parent folder) and began uploading gigabytes of data.Rules:
- Always run
pwdfirst. Confirm you are inside your repository folder.- Always run
git statusbefore committing. Check that only the files you expect appear in green.- Prefer naming files explicitly (e.g.,
git add README.md diaries/lab-01.md). Usegit add .only when you have just checkedgit statusand every listed file belongs in the commit.- If you see hundreds of files listed in
git status, stop. You are probably in the wrong directory. Do not commit. Runpwd, thencdto the right folder.
4. Commit the change
A commit is a snapshot with a short message describing what you did:
git commit -m "add readme with course details"
Think of the commit message as an instruction to your future self. Good commit messages are short and specific.
Good examples:
git commit -m "add readme with course details"
git commit -m "start lab 01 diary"
git commit -m "fix typo in README"
Poor examples:
git commit -m "stuff"
git commit -m "update"
git commit -m "final final really final"
Sanity check that Git recorded your commit:
git log -1 --oneline
You should see your commit message listed.
Common mistake
There must be a space after
commitand between-mand the message:git commit -m "update readme" # correct git commit-m "update readme" # wrong: git does not recognise commit-m git commit -m"update readme" # wrong: missing space before the message
If Git says "nothing to commit"
Either you forgot to save the file in your editor, or you forgot to stage it. Run:
git statusIf the file appears under
Changes not staged for commit, rungit add README.mdagain. If Git shows nothing changed, save the file and try again.
5. Push to GitHub
Push sends your commits to GitHub so they are backed up online:
git push
If you set up authentication in Step 4, this should work without a password prompt.
If you see an error about an upstream branch, run:
git push -u origin HEAD
That command tells Git which remote branch your local branch should track. You usually only need to do it once.
If the push succeeds, Git will print a summary showing which branch was updated on GitHub.
If
git pushis rejectedSometimes GitHub has changes that your computer does not yet have, for example if you edited the README on the GitHub website. In that case, run:
git pull --rebase git pushIf Git reports a conflict, stop and ask for help rather than guessing.
Step 8: Check your work
Go to your repository page on GitHub. You should see the updated README file with your changes.
You should also see:
- The latest commit message near the top of the file list.
- The commit count link, which you can click to view history.
- Your updated README rendered as formatted text on the repository front page.
If the page has not changed, refresh the browser and check that your
git push command succeeded.
Quick reference
| Command | What it does |
|---|---|
git status | Show which files have changed |
git add <file> | Stage a file for the next commit |
git add -A | Stage all changes |
git commit -m "message" | Save a snapshot with a message |
git push | Upload commits to GitHub |
git pull | Download changes from GitHub |
git log --oneline | Show commit history |
Workflow summary
Edit files →
git add→git commit -m "message"→git push. Repeat.
Emergency: stuck in a rebase
If you accidentally start a rebase and want to get back to where you were:
git rebase --abortIf you are resolving a rebase conflict, the usual flow is:
git add <file> git rebase --continueIf you are unsure, run
git statusand ask for help before you try random commands.
What never belongs in a repository
Git remembers everything you commit, even after you delete the file. If you push a secret to GitHub, assume it is compromised. Removing it from later commits does not remove it from the history.
Never commit any of the following:
| Category | Examples |
|---|---|
| SSH or API keys | ~/.ssh/id_ed25519, .env files, API tokens |
| Passwords and credentials | database connection strings, login details |
| Personal data | NZAVS datasets, participant records, anything with names or ID numbers |
| Large binary files | .zip, .rds, .csv, .mp4 (your .gitignore already excludes these) |
Your .gitignore blocks most data and output files automatically, but
it cannot protect you if you stage files from outside your repository or
override it with a force flag.
If you accidentally push something private
- Do not panic, but act quickly.
- Revoke the credential. If it is an SSH key, delete it from github.com/settings/keys and generate a new one (repeat Step 4). If it is an API token or password, revoke it on the service that issued it. Once revoked, the exposed copy is useless.
- Delete and recreate the repository. Removing the file from a later commit does not remove it from the history. The simplest fix is to delete the repository on GitHub, create a new one, and push your current local folder to it. Your local files are unaffected.
- If personal data was exposed (e.g., participant records with names or ID numbers), this is a privacy breach. Report it to the course coordinator immediately so it can be escalated to the university.
Terminal basics
You have already used a few terminal commands (cd, git clone). The
terminal is a text interface for your computer. Every command you type
runs a small program. Here are the commands you will use most often.
Where am I?
pwd
pwd (print working directory) shows the folder you are currently in.
List files
ls
ls lists the files and folders in the current directory. To see hidden
files (names starting with .), use:
ls -a
Git stores its data in a hidden folder called .git. Try ls -a inside
your repository to see it.
Change directory
cd ~/Documents/psy434/psy434-labs
cd moves you into a folder. A few shortcuts:
| Shortcut | Meaning |
|---|---|
~ | Your home folder |
.. | One level up |
. | The current folder |
So cd .. moves up one level, and cd ~ takes you home.
Create a folder
mkdir diaries
mkdir (make directory) creates a new folder.
Create an empty file
touch lab-01.md
touch creates an empty file if it does not already exist. (Windows
users: touch works in Git Bash but not in PowerShell or Command
Prompt. Make sure you are using Git Bash.)
Terminal quick reference
| Command | What it does |
|---|---|
pwd | Print the current directory |
ls | List files |
ls -a | List files including hidden ones |
cd <folder> | Change directory |
cd .. | Go up one level |
mkdir <name> | Create a folder |
touch <name> | Create an empty file |
Organise your repository
Set up a simple folder structure for the course. From inside your repository folder:
mkdir diaries data R
Git does not track empty folders. To make sure diaries/, data/, and
R/ appear on GitHub, add a placeholder file to each:
touch diaries/.gitkeep data/.gitkeep R/.gitkeep
(data/.gitkeep is the only file in data/ that should be tracked.)
Each folder has a purpose:
| Folder | Contents |
|---|---|
diaries/ | Weekly lab diary entries (.md files) |
data/ | Datasets you generate or download (ignored by Git because of .gitignore, but useful for keeping your project organised locally) |
R/ | R scripts and Quarto documents (.R, .qmd) |
A tidy repository separates source files (code you write) from generated output (plots, PDFs, HTML). Source files go into Git; output does not. If your code is correct, anyone can regenerate the output by running it. This principle, that results follow from code, is the basis of reproducible research.
Naming conventions
Good file names are lowercase, use hyphens instead of spaces, and sort naturally:
| Good | Bad | Why |
|---|---|---|
lab-01.md | Lab 1.md | spaces break terminal commands |
lab-02.md | lab2.md | the hyphen and zero-padding (01, 02, …) keep files in order |
clean-data.R | CleanData_FINAL(2).R | one clear name, no version suffixes |
fig-ate-by-age.png | Figure 3.png | describes content, not position |
Three rules of thumb:
- No spaces. Use hyphens (
-) or underscores (_). Spaces require quoting in the terminal and cause errors in scripts. - Zero-pad numbers.
01,02, ...10sort correctly;1,2, ...10does not (your system puts10before2). - Name for content, not sequence. A file called
analysis-ate.Ris still meaningful six months later;script3.Ris not.
Next, create a .gitignore file. This tells Git to ignore files that
should not be tracked (system files, R temporary files, datasets, large
binaries, etc.).
If you are using RStudio:
- Go to
File > New File > Text File. - Paste the contents below.
- Go to
File > Save As.... - Save the file as
.gitignorein the root of your repository (psy434-labs/). - In the Files pane, click
More > Show Hidden Filesso dotfiles are visible. - Open
.gitignorefrom the Files pane whenever you want to edit it.
Paste the following contents:
# system files
.DS_Store
Thumbs.db
# R
.Rhistory
.RData
.Rproj.user
*.Rproj
# data files
data/**
!data/.gitkeep
*.rds
*.qs
*.parquet
*.arrow
*.csv
*.xlsx
*.sav
*.dta
# large binary and archive files
*.zip
*.7z
*.tar
*.gz
*.bz2
*.xz
*.dmg
*.iso
*.mp4
*.mov
*.avi
*.mp3
*.wav
# output files
*.pdf
*.html
*.png
*.jpg
*.jpeg
*.svg
*.gif
*.tiff
_files/
*_cache/
.quarto/
Save the file in the root of your repository (the same folder as
README.md). The filename must start with a dot: .gitignore, not
gitignore.
Your repository should look like this:
psy434-labs/
├── .gitignore
├── README.md
├── R/
│ └── .gitkeep
├── data/
│ └── .gitkeep
└── diaries/
└── .gitkeep
Notice that data/ exists on your computer but Git ignores its contents
by default. This is intentional: data files can be large, and anyone
with your code can regenerate them. Keep data local; keep code in Git.
Before every commit, check that you are staging only source files
(.md, .R, .qmd, and small config files):
git status
git diff --cached --name-only
If you accidentally stage a data file or large object, unstage it:
git restore --staged <file>
If you already committed it, remove it from tracking while keeping the local copy:
git rm --cached <file>
git commit -m "stop tracking data file"
git push
Lab diary files go in the diaries/ folder, named by week number:
diaries/
├── lab-01.md
├── lab-02.md
├── lab-03.md
├── ...
└── lab-10.md
There is no lab-07.md (week 7 is test 1). Create your first diary file
now:
touch diaries/lab-01.md
Markdown basics
Markdown is a plain-text format that converts to formatted documents.
You write in a .md file using simple symbols for headings, bold,
lists, and so on. GitHub renders markdown automatically, so your diary
will look formatted when you view it online.
Headings
Use # symbols. More # signs mean smaller headings:
# Heading 1
## Heading 2
### Heading 3
Paragraphs
Separate paragraphs with a blank line. A single line break without a blank line will not start a new paragraph.
Bold and italics
This is **bold** and this is *italic*.
Lists
Unordered lists use -:
- first item
- second item
- third item
Numbered lists use 1., 2., etc.:
1. first step
2. second step
3. third step
Inline code
Wrap code in single backticks:
Use the `git push` command to upload your work.
Links
[GitHub](https://github.com)
Markdown reference
GitHub has a concise guide to GitHub-flavoured markdown: Basic writing and formatting syntax.
Write your first lab diary
Create your week 1 diary entry now. Open diaries/lab-01.md in RStudio
and write ~150 words covering:
- What this lab covered and what you did.
- A connection to the week's lecture content.
- One thing you found useful, surprising, or challenging.
Use at least one heading, one bold or italic word, and one list. When you are done, stage these files, commit, and push:
git add .gitignore diaries/lab-01.md diaries/.gitkeep data/.gitkeep R/.gitkeep
git commit -m "add lab 01 diary and repo structure"
git push
Check your repository on GitHub to confirm the file appears and the markdown renders correctly.
Editors
RStudio is the recommended editor for this course. All examples and in-class instructions assume RStudio. You are welcome to use any editor you prefer (VS Code, Zed, Neovim, etc.), but you will need to translate instructions on your own. Avoid rich-text editors (Word, Pages, TextEdit) that can silently change file format or extensions.
Alternative: GitHub Desktop
If you prefer a graphical interface, download GitHub Desktop. It provides the same stage/commit/push workflow with buttons instead of terminal commands. Either approach is fine for this course.