SOFTWARE TRAINING for STUDENTS @ UNIVERSITY of WISCONSIN-MADISON

Hands-On Activity:
GitHub: Git and GitHub for Beginners

Before You Start

These activities go with the GitHub: Git and GitHub for Beginners workshop. Work through them in order: each one builds on the repository you made in the one before.

You’ll need:

Shortcuts are written for both systems: Cmd on Mac, Ctrl on Windows. On a Mac, GitHub Desktop’s settings are under GitHub Desktop → Settings. On Windows they’re under File → Options.

Activity 1: Sign In to GitHub and GitHub Desktop

Objective:

Get signed in to GitHub on the web and in the GitHub Desktop app, so the rest of the activities can save your work to your account.

1. Sign in to GitHub on the web

2. Open GitHub Desktop and sign in

  • Open GitHub Desktop. If it isn’t installed, download it from the link above and install it.
  • Go to Settings → Accounts (Mac) or Options → Accounts (Windows) and click Sign in.
  • Your browser opens. Click Authorize to let GitHub Desktop use your account, then switch back to the app.

3. Check your name and email

  • In the same settings window, open the Git tab.
  • Make sure your name and email are filled in. Every commit you make is labeled with these.

Check Your Work:

  • The Accounts tab shows your GitHub username and profile picture.

Extra Practice:

  • Open Terminal (Mac) or PowerShell (Windows) and run git --version to see whether command-line git is installed too.

Activity 2: Create a Repository and Make Your First Commits

Objective:

Create a new repository on your computer, add a file to it, and save two commits.

1. Create a new repository

  • Choose File → New Repository… (Cmd + N / Ctrl + N).
  • Name: my-first-repo
  • Local Path: leave the default, as long as it isn’t inside a OneDrive or iCloud folder.
  • Click Create Repository.

2. Add a file

  • Choose Repository → Show in Finder (Mac) or Show in Explorer (Windows) to open the repo folder.
  • In your text editor, create a new file and save it in that folder as yourfirstname.txt, for example bucky.txt.
  • On a Mac with TextEdit, choose Format → Make Plain Text before saving.
  • Back in GitHub Desktop, your file appears in the Changes tab with a green plus icon.

3. Make your first commit

  • In the Summary box at the bottom left, type Add my first file.
  • Click Commit to main.

4. Make changes and commit again

  • Open yourfirstname.txt, type today’s date, and save.
  • Create a second file called new.txt in the same folder.
  • In GitHub Desktop, click yourfirstname.txt in the Changes tab to see the line you added, highlighted in green.
  • Commit both files with the summary Add date and new file.

Check Your Work:

  • Click the History tab. You should see your two commits, newest at the top.

Extra Practice:

  • Create a file called private-notes.txt in the repo folder.
  • In the Changes tab, right-click it and choose Ignore File. A .gitignore file appears instead.
  • Commit the .gitignore file with the summary Ignore private notes.

Activity 3: Publish Your Repository to GitHub

Objective:

Put your repository on GitHub, then push a new commit to it.

1. Publish the repository

  • Click Publish repository at the top of GitHub Desktop.
  • Keep the name my-first-repo.
  • Leave Keep this code private checked.
  • Click Publish Repository.

2. View it on GitHub

  • Choose Repository → View on GitHub (Cmd + Shift + G / Ctrl + Shift + G). Your files and commits are now on GitHub.com.

3. Push a new commit

  • Add another line to yourfirstname.txt and save.
  • Commit it with the summary Add another line.
  • Click Push origin at the top.

Check Your Work:

  • Refresh your repo’s page on GitHub.com. The new commit message shows next to your file.

Extra Practice:

  • On GitHub.com, click yourfirstname.txt, then the pencil icon to edit it. Add a line and click Commit changes.
  • In GitHub Desktop, click Fetch origin, then Pull origin. The line you added on the web now appears in your file.

Activity 4: Clone the STS Practice Repository and Pull Changes

Objective:

Make a copy of a repository that’s already on GitHub, then pull down a change that someone else makes.

1. Clone the practice repository

  • Go to https://go.wisc.edu/stsgithub. It opens the STS practice repo, stsprojects/intro-to-git.
  • Click the green Code button, then Open with GitHub Desktop.
  • Or, in GitHub Desktop, choose File → Clone Repository… (Cmd + Shift + O / Ctrl + Shift + O), click the URL tab and paste https://github.com/stsprojects/intro-to-git.git.
  • Choose a Local Path that is not inside your my-first-repo folder, then click Clone.

2. Pull the trainer’s change

  • Wait for your trainer to push a change to the practice repo.
  • Click Fetch origin. When it changes to Pull origin, click it.

Check Your Work:

  • The History tab shows the trainer’s new commit, and the new or changed file is in your folder.

Extra Practice:

  • Change a file in the practice repo, commit it, and try to push. GitHub Desktop tells you that you don’t have permission to push to this repo, because only STS can. To contribute to a repo you don’t own, you’d fork it and open a pull request, which Activity 6 shows.

Activity 5: Undo a Commit

Objective:

Practice the three ways to fix a commit: undo, amend and revert. Use your my-first-repo repository.

1. Undo a commit you haven’t pushed

  • Add a line to yourfirstname.txt and commit it with the summary Oops.
  • Click Undo at the bottom of the Changes tab. The commit disappears, and your change goes back to the Changes list.
  • Right-click the file and choose Discard Changes to throw the change away.

2. Amend a commit message

  • Make a small change and commit it with a typo in the summary: Updte file.
  • In the History tab, right-click that commit and choose Amend Commit….
  • Fix the summary to Update file and click Amend last commit.

3. Revert a commit you’ve already pushed

  • Click Push origin to push your commits.
  • In the History tab, right-click the Update file commit and choose Revert Changes in Commit.
  • A new commit appears that undoes it. Push again.

Check Your Work:

  • The History tab shows both Update file and a Revert "Update file" commit. Nothing was deleted from the history.

Activity 6: Create a Branch and Open a Pull Request

Objective:

Make a change on a separate branch, then merge it into main with a pull request on GitHub.

1. Create a branch

  • In my-first-repo, click Current Branch, then New Branch (Cmd + Shift + N / Ctrl + Shift + N).
  • Name it add-favorite-food and click Create Branch.

2. Commit a change on the branch

  • Add the line My favorite food is ... to yourfirstname.txt and save.
  • Commit with the summary Add favorite food. The button now says Commit to add-favorite-food.
  • Click Publish branch.

3. Open and merge a pull request

  • Click Preview Pull Request, then Create pull request. GitHub.com opens.
  • Click Create pull request on the web page.
  • Click Merge pull request, then Confirm merge.

4. Update main on your computer

  • In GitHub Desktop, click Current Branch and choose main.
  • Click Fetch origin, then Pull origin.

Check Your Work:

  • On the main branch, yourfirstname.txt now includes your favorite food line.

Extra Practice:

  • Delete the branch you’re done with: switch to it, then choose Branch → Delete….

Activity 7: Resolve a Conflict

Objective:

Cause a conflict on purpose by changing the same line in two places, then fix it.

1. Change the same line in two places

  • Make sure you’re on main and have pulled the latest changes.
  • On GitHub.com, edit yourfirstname.txt: change the first line to Edited on GitHub and click Commit changes.
  • Without pulling, change the same first line on your computer to Edited on my laptop, save, and commit.

2. Pull and see the conflict

  • Click Fetch origin, then Pull origin.
  • GitHub Desktop shows that yourfirstname.txt has a conflict.

3. Fix the conflict

  • Open the file in your editor. git has marked both versions between <<<<<<< and >>>>>>>.
  • Decide what the line should say, delete the marker lines, and save.
  • Back in GitHub Desktop, click Continue merge, then Push origin.

Check Your Work:

  • The file has no conflict markers left, and the History tab shows a merge commit.

Bonus: Use git on the Command Line

Objective:

Do the same commit-and-push steps with commands instead of buttons.

1. Open a terminal in your repository

  • In GitHub Desktop, with my-first-repo open, choose Repository → Open in Terminal (Mac) or Open in Command Prompt (Windows).

2. Make a commit and push it

Add a line to yourfirstname.txt, save it, then run:

git status
git add yourfirstname.txt
git commit -m "Commit from the command line"
git log --oneline
git push

Check Your Work:

  • Your new commit shows in GitHub Desktop’s History tab and on GitHub.com.