Skip to main content

Collaborating with GitHub

Sharing Code and Working with Others

· 3 min read

In the previous unit, we learned to track changes locally with Git. Now let's connect to GitHub for backup, sharing, and collaboration.

Collaborating with GitHub

What Is GitHub?

GitHub is a platform that hosts Git repositories online. It's like cloud storage for your code, but with powerful collaboration features. You can back up your projects, share them publicly or privately, and work with others.

Setting Up GitHub

First, create a GitHub account if you don't have one. The free tier offers unlimited public and private repositories.

Then create a new repository on GitHub. Give it a name but don't add any files yet since we'll push from our local repository.

Connecting to GitHub

Link your local repository to GitHub:

git remote add origin https://github.com/username/repo-name.git

Replace username and repo-name with your GitHub username and repository name.

Pushing Code

Upload your commits to GitHub:

git push -u origin main

The -u flag sets up tracking so future pushes only need git push. Refresh your GitHub page to see your code online.

Pulling Changes

If you (or a collaborator) made changes on GitHub, download them with:

git pull origin main

Always pull before starting new work to ensure you have the latest code.

Working with Branches

Branches let you work on features without affecting the main code:

git checkout -b feature-name    # Create and switch to new branch
git checkout main # Switch back to main

Make your changes on the branch, then push it:

git add .
git commit -m "Add new feature"
git push origin feature-name

Pull Requests

A pull request (PR) asks to merge your branch into main. On GitHub:

  1. Click "Compare & pull request" after pushing your branch
  2. Describe your changes
  3. Request review if working with others
  4. Merge when approved

PRs allow code review before changes go live.

Project: Publish Your Turtle Project

Let's push the Turtle project we created in the last unit.

If you haven't already, create a repository on GitHub. Then connect and push:

cd path/to/your/turtle-project
git remote add origin https://github.com/username/turtle-shapes.git
git push -u origin main

Now create a feature branch to add functionality:

git checkout -b add-colors

Modify your code to add color support, then commit and push:

git add .
git commit -m "Add random colors to shapes"
git push origin add-colors

On GitHub, create a pull request to merge add-colors into main. Review the changes, then merge the PR.

Finally, update your local main branch:

git checkout main
git pull origin main

You've now completed a full development workflow: branch, develop, push, review, merge, and sync.

In the next unit, we'll learn to manage Python dependencies with virtual environments.