SOFTWARE TRAINING for STUDENTS @ UNIVERSITY of WISCONSIN-MADISON

GitHub: Git and GitHub for Beginners

About this Workshop

This manual introduces git, a version control system, and GitHub, a platform for storing and sharing git projects. It’s written for complete beginners. You don’t need any experience with git, version control or the command line.

What this manual covers

  • What version control is, and how git and GitHub are different
  • How to create a repository, add files and make commits
  • How to publish a repository to GitHub, clone one, and pull and push changes
  • How to undo a commit, work with branches, and contribute using forks and pull requests
  • What a conflict is and how to resolve one

The examples use GitHub Desktop, a free app that handles the most common git tasks. Where it helps, the matching command-line commands are shown too.

To practice, work through the Hands-On Activities, which follow the same order as this manual. The one-page cheat sheet and a conflict practice file are in the class files: use the Files button at the top of this page.

What You’ll Need

A GitHub account

Basic GitHub accounts are free for individuals. Create one at https://github.com/signup, or sign in to an existing account at https://github.com/login.

GitHub Desktop

Download GitHub Desktop from https://desktop.github.com/download/. There are versions for Mac (Apple silicon and Intel) and Windows. After installing it, open the app and sign in with your GitHub account.

GitHub Desktop comes with its own copy of git, so you don’t need to install git separately to use it.

git on the command line (optional)

If you’d also like to use git by typing commands in a terminal:

  • Mac: git comes with Apple’s Xcode Command Line Tools. Install them by running xcode-select --install in Terminal.
  • Windows: install Git for Windows from https://git-scm.com/install/windows.

To check that git is installed, run:

git --version

Other ways to use git

Many code editors, such as VS Code, also have git built in. Try a few options and use whichever works best for you.

What Is git?

git is a version control system (VCS): a tool that tracks changes to the files in a project and lets several people work on the same set of files. It’s free, open-source and one of the most widely used tools among software developers.

Why use version control?

Using a version control system like git in your projects:

  • Lets multiple people work on the same project at the same time
  • Makes sure everyone is working with the same project files
  • Helps collaborators avoid accidentally overwriting each other’s work
  • Lets you try out changes on a separate branch without breaking the working version
  • Makes it easy to go back to an earlier version if something goes wrong

git was originally created by Linus Torvalds, the creator of Linux, and is now maintained by a large open-source community. No single person or company owns it, and you can install it anywhere: on your laptop, on a server or in the cloud.

What Is GitHub?

GitHub is a cloud platform for storing git projects online and collaborating on them. It’s owned by Microsoft. Accounts are free for individuals, and paid plans add features for larger teams.

Why people use GitHub

  • It makes collaborating with other people easy
  • You can reach your projects from anywhere with an internet connection
  • You can share your work publicly, or keep it private (for example, class assignments)
  • Many people link their GitHub profile on their resume, especially when applying for tech jobs

Alternatives to GitHub

GitHub is popular, but it isn’t the only place to host git projects. Others include GitLab and Bitbucket. UW–Madison runs its own GitLab, which some CS courses use.

You don’t need an impressive GitHub profile. There’s a common myth that “real” developers need a big GitHub history full of commits. That isn’t true, and plenty of professional developers have very little public activity on GitHub.

git vs. GitHub

git and GitHub are related, but they’re different things.

  • git is the open-source tool that tracks changes to your files. You install it on your computer, and it works without an internet connection.
  • GitHub is a website and service that stores git projects online. It makes sharing and collaborating easier.

Do I need GitHub to use git?

No. You can use git entirely on your own computer. GitHub (or a similar service) becomes useful when you want a backup online, want to work from more than one computer, or want to collaborate with other people.

GitHub Desktop vs. the Command Line

git was built as a command-line tool. You can use it by typing commands in a terminal program, such as PowerShell on Windows or Terminal on Mac, but that can feel confusing or intimidating if you haven’t used a terminal before.

Why GitHub Desktop?

  • It covers the git features and tasks people use most often
  • Many people find its interface easier and more intuitive
  • You don’t need to remember the syntax and options for lots of different commands
  • You can see at a glance which files have been added, removed or changed since your last commit

When you might want the command line

Some advanced or specialized git features are only available on the command line. You may want to learn it eventually, but for many people GitHub Desktop does everything they need.

Other desktop apps

GitHub Desktop is one of many apps for working with git. Others include Sourcetree (free), GitKraken (free, with paid plans) and Fork. See the full list at https://git-scm.com/tools/guis.

Repositories

A git project is stored in a repository, often shortened to repo. A repo is a folder on your computer that contains:

  • All of the project’s files, such as code, images and other assets
  • A version history that records every change made over the life of the project
  • Documentation, such as a README file
  • Other information that’s useful to people viewing or contributing to the project, such as release notes

Two ways to start

  • Initialize a new repo, starting from scratch on your computer
  • Clone an existing repo, usually by downloading it from GitHub (see Remote Repositories and Cloning below)

Create a new repository in GitHub Desktop

  1. Choose File → New Repository… (Cmd + N on Mac, Ctrl + N on Windows).
  2. Name: enter a name. This also becomes the folder name.
  3. Description: optionally, add a short description of the project.
  4. Local Path: choose where on your computer the repo folder should be saved.
  5. Optionally, have GitHub Desktop create a README, a .gitignore file or a license.
  6. Click Create Repository.

On the command line

Move to the folder where you want the repo, then run git init with the repo’s name. This creates a new folder with that name and starts a repo inside it.

cd path/to/your/projects
git init my-first-repo

Adding Files and .gitignore

Before git can save a file in a commit, the file has to be added to the commit. This is also called staging the file.

In GitHub Desktop

Any file you create, change or delete inside the repo folder appears in the Changes tab. Files are checked by default, which means they’ll be included in your next commit. Uncheck a file to leave it out of that commit.

The small icon next to each file shows what happened to it: green for a new file, yellow for a changed file and red for a deleted file.

GitHub Desktop Changes tab listing a new file, bucky.txt, checked and ready to commit, with the Commit to main button below

TODO: I need to add an updated screenshot

On the command line

Add one file, or add every changed file in the folder with a dot:

git add notes.txt
git add .
git status

git status shows which files are staged and which aren’t.

Ignoring files with .gitignore

Sometimes your project folder has files that shouldn’t be part of the repo, like system files, very large data files or files that contain passwords. List those files in a file named .gitignore in the repo folder, and git will ignore them.

  • GitHub Desktop: right-click (or two-finger click) the file in the Changes list and choose Ignore File.
  • Command line or any editor: open the .gitignore file and add each file name on its own line.

Commits

A commit saves a snapshot of a set of changes to your project. A commit can be as small as fixing a typo or as large as adding a new feature. Every commit needs a commit message: a short description of what changed.

Good commit habits

  • Keep each commit focused on one change, so it’s easier to see where something was introduced
  • Make sure all the changes in a commit are related. A commit can still touch several files or lines.
  • On small or solo projects this is flexible, but on larger projects or when working with others, one change per commit is a good habit

Make a commit in GitHub Desktop

  1. In the Changes tab, check the files you want to include.
  2. Type a short summary in the Summary field (required). Add more detail in Description if you like.
  3. Click Commit to main. The button shows the name of the branch you’re on.
GitHub Desktop commit area with the summary "Adding my first file" and a short description filled in above the Commit to main button

TODO: nvm i need to update screenshots across the board

After you commit, the Changes list clears and your commit appears in the History tab.

See what changed

Click a file in the Changes tab to see exactly what changed in it. Added lines are highlighted in green and removed lines in red.

GitHub Desktop showing two changed files, bucky.txt and new.txt, with the added line highlighted in green in the diff view

TODO

On the command line

git commit -m "Add my first file"
git log --oneline

git log --oneline lists your commits, newest first.

Publishing a Repository

A repo you create in GitHub Desktop only exists on your computer until you publish it to GitHub.

  1. Click Publish repository at the top of GitHub Desktop.
  2. Choose a name for the repo on GitHub. This is usually the same as the folder name.
  3. Optionally, add a description.
  4. Decide whether to keep Keep this code private checked.
  5. Click Publish Repository.
GitHub Desktop Publish Repository dialog with fields for the repository name and description and the Keep this code private checkbox

TODO

Keep class assignments private. If you uncheck Keep this code private, anyone can see your code on GitHub.com. Leave it checked for class assignments.

Remote Repositories and Cloning

A remote repository is a copy of a repo that’s hosted online, such as on GitHub, and that you work with over the internet. When you have a local copy of a remote repo, git refers to the remote as origin.

Cloning makes a local copy of a remote repo on your computer, including its full history.

Clone from GitHub.com

  1. Go to the repo’s page on GitHub. For example, STS’s practice repo is at https://github.com/stsprojects/intro-to-git.
  2. Click the green Code button.
  3. Click Open with GitHub Desktop, or copy the repo’s URL to paste into GitHub Desktop.
The stsprojects/intro-to-git repository page on GitHub.com, showing its files and the green Code button used to clone the repo

Clone in GitHub Desktop

  1. Choose File → Clone Repository… (Cmd + Shift + O on Mac, Ctrl + Shift + O on Windows).
  2. Click the URL tab and paste the repo’s URL.
  3. Local Path: choose where on your computer to save the repo.
  4. Click Clone.

You can’t clone a repo into a folder that’s already a git repo. If you try, GitHub Desktop shows an error. Choose a different folder.

On the command line

git clone https://github.com/stsprojects/intro-to-git.git

Pull and Push

Pull and push keep your local repo and the remote repo in sync.

  • Pull downloads new commits from the remote repo and adds them to your local copy.
  • Push uploads the commits you’ve made locally to the remote repo.

Pull in GitHub Desktop

  1. Click Fetch origin at the top to check for new commits.
  2. If there are new commits, the button changes to Pull origin. Click it.
  3. Your files update, and the new commits appear in the History tab.

If you have uncommitted changes in files that the pull would change, GitHub Desktop offers to stash them. Stashing sets your changes aside, then restores them after the pull.

GitHub Desktop showing the Pull origin button in the toolbar and a prompt to pull 1 commit from the origin remote

TODO

Push in GitHub Desktop

After you commit, click Push origin at the top. If the remote has commits you don’t have yet, you’ll need to pull first.

You can only push to a remote repo if you’re a collaborator on it. See Forks and Pull Requests below for how to contribute to other people’s repos.

On the command line

git pull
git push

Undoing a Commit

It’s common to need to change something after you’ve already made a commit. Which option to use depends on whether you’ve pushed the commit yet.

Commits you haven’t pushed yet

  • Undo: removes the commit but keeps all of its changes in your files, so you can redo it. In GitHub Desktop, click Undo under the commit box.
  • Amend: keeps the commit but lets you change its message or add and remove files. In the History tab, right-click your latest commit and choose Amend Commit….

Don’t amend commits you’ve already pushed to a shared repo. Amending rewrites the project history, which can cause problems for collaborators and lose work.

Commits you’ve already pushed

Revert creates a new commit that undoes the changes from an earlier commit. Nothing is removed from the history, so it’s the safe choice for commits that are already pushed. In the History tab, right-click the commit and choose Revert Changes in Commit.

On the command line

git commit --amend -m "Better commit message"
git revert <commit-id>

Reset and rebase

You may also see git reset and git rebase. This workshop doesn’t cover them, because both rewrite history and can permanently lose work if used incorrectly.

  • Reset moves your branch back to an earlier commit and drops the commits that came after it.
  • Rebase replays your commits on top of another branch. It gives a tidier, straight-line history, but it rewrites the commits.

Branches

Every repo starts with a main version of the project, called the main branch. A branch is a separate line of work inside the same repo. Branches let you try new features or make big changes without affecting the main branch until you’re ready.

You may see the term HEAD. That’s how git keeps track of which branch you’re currently working on.

Create a branch in GitHub Desktop

  1. Click Current Branch at the top.
  2. Click New Branch (Shift + Cmd + N on Mac, Ctrl + Shift + N on Windows).
  3. Type a name for the branch and click Create Branch.
  4. To share the branch on GitHub, click Publish branch.
GitHub Desktop Current Branch menu open, showing the branch list and the New Branch button

TODO

Switch branches

Click Current Branch and choose the branch you want. If you have uncommitted changes, GitHub Desktop asks whether to leave them on the current branch or bring them to the new one.

Merge a branch

When the work on a branch is finished, you merge it into another branch, usually main, to combine their changes. This is often done with a pull request on GitHub (see below). In GitHub Desktop, you can also switch to main and choose Branch → Merge into Current Branch….

On the command line

git switch -c my-new-feature
git switch main

git switch -c creates a new branch and switches to it. You may also see the older command git checkout, which still works. “Checking out” a branch means switching to it.

Forks and Pull Requests

Anyone can view a public repo, but you can’t push changes to it unless you’re a collaborator. To contribute to someone else’s repo, you can:

  • Ask to be added as a collaborator (how this works depends on the team or project), or
  • Fork the repo, make your changes, then open a pull request

Forks

A fork is your own copy of someone else’s repo, stored in your GitHub account. You can change your fork freely without affecting the original. Forks are also how people start their own versions of open-source projects.

Pull requests

When your changes on a branch or fork are ready, you open a pull request (PR). A pull request asks the repo’s owners to review your changes and merge them into their project.

In GitHub Desktop, after you push a branch, click Preview Pull Request or Create Pull Request to open one on GitHub.com.

Conflicts

A conflict happens when two changes affect the same part of a file and git can’t tell which one to keep. This usually happens when you pull or merge changes that overlap with your own. Conflicts need a person to resolve them.

What a conflict looks like

git marks the conflicting part of the file like this:

<<<<<<< HEAD
Your version of the line
=======
The other version of the line
>>>>>>> main

Resolve a conflict

  1. GitHub Desktop lists the files with conflicts. Open each one in your editor.
  2. Decide what the final text should be. Keep one version, the other, or a mix of both.
  3. Delete the conflict marker lines (<<<<<<<, ======= and >>>>>>>) and save the file.
  4. Commit the result.
GitHub Desktop diff view of a file with conflict markers, showing two different versions of a line separated by equals signs

TODO

Pulling often and keeping commits small makes conflicts less likely.

Want to practice? Download conflict-practice.txt from the class files and follow the steps inside it.

Key Terms

  • git: the open-source tool that tracks changes to files
  • GitHub: a cloud platform for storing git projects and collaborating on them
  • Repository (repo): a git project. It can be local (on your computer) or remote (online, such as on GitHub).
  • Staging: adding files to your next commit
  • .gitignore: a file listing files that git should ignore
  • Commit: a saved snapshot of a set of changes, with a commit message describing what changed
  • Remote / origin: the online copy of a repo; origin is git’s default name for it
  • Clone: make a local copy of a remote repo
  • Pull: download new commits from the remote repo to your local repo
  • Push: upload your local commits to the remote repo
  • Branch: a separate line of work inside a repo, used to try changes without affecting main
  • Fork: your own copy of someone else’s repo, in your GitHub account
  • Merge: combine the changes from one branch into another
  • Pull request: a request for a repo’s owners to review your changes and merge them
  • Conflict: overlapping changes that git can’t combine on its own, so you resolve them by hand

Learn More

You now know enough to use git and GitHub for your own projects. git can do much more, but most of it is advanced or only needed in specific situations, and some of it is only available on the command line.

Need help? Drop in to STS office hours or request an appointment at https://sts.doit.wisc.edu.