Skip to content

GitHub: Collaboration and Remote Repositories

Building on Git Basics, this tutorial covers using GitHub for hosting, collaboration, and project management.

Introduction to GitHub

GitHub is a web-based platform for hosting Git repositories. While alternatives exist (GitLab, Gitea, Forgejo), GitHub is the most widely used in open science and academia.

Benefits of GitHub: - Free public repositories (unlimited) - Issue tracking and project management - Pull request workflow for code review - CI/CD integration (GitHub Actions) - Community and discoverability - Educational benefits (GitHub Student Pack)

Getting Started with GitHub

Create an Account

  1. Go to github.com
  2. Sign up with your email
  3. Recommended: Use your institutional email for student/educator benefits
  4. Students
  5. Educators

Authentication

You need to authenticate to push code to GitHub. Choose one:

Option 1: HTTPS with Personal Access Token (easier for beginners)

  1. Go to Settings → Developer settings → Personal access tokens
  2. Create a fine-grained token with repo scope
  3. In the Permissions section check Contents and provide read and write access
  4. Creating the token will give you a passkey (only appears once) that you can save to a safe place
  5. When git push prompts for password, use your token passkey

Option 2: SSH Keys (more secure long-term)

# Generate keys (if you don't have them)
ssh-keygen -t ed25519 -C "your.email@example.com"

# Add public key to GitHub
# Settings → SSH and GPG keys → New SSH key
cat ~/.ssh/id_ed25519.pub

Hands-On Example: Creating and Contributing to a Repository

Let's walk through a complete workflow together. You'll create a simple project, make changes on a feature branch, and merge it back to main using a Pull Request.

Step 1: Create a Repository on GitHub

  1. Click the + icon in the top right corner of GitHub
  2. Select New repository

Create new repository button

Click the + icon and select "New repository"
  1. Name your repository: hello-github (use lowercase)
  2. Add a description: "My test GitHub repository"
  3. Choose Public (so everyone can see it)
  4. Check the box: Add a README file
  5. Click Create repository

Repository creation form

Fill in the repository details

Congratulations! Your repository is now live at github.com/your-username/hello-github.

Step 2: Clone the Repository to Your Computer

Open your terminal and run:

git clone https://github.com/your-username/hello-github.git
cd hello-github

Clone repository from GitHub

Click the blue "Code" button to copy the clone URL

Configure Git to use SSH:

git remote set-url origin git@github.com:username/repo.git

Verify the repository is set up:

git remote -v  # Shows that origin points to GitHub

Step 3: Make Your First Edit Locally

Create a simple Python script:

# Create a new file
cat > hello.py << 'EOF'
def greet(name):
    """A simple greeting function."""
    return f"Hello, {name}!"

if __name__ == "__main__":
    print(greet("World"))
EOF

Step 4: Create a Feature Branch

Never work directly on main. Instead, create a branch:

git checkout -b feature/add-greeting

Now verify you're on the new branch:

git branch  # Shows you're on feature/add-greeting

Step 5: Commit and Push Your Changes

# Stage your changes
git add hello.py

# Commit with a descriptive message
git commit -m "feat: add greeting function"

# Push to GitHub
git push -u origin feature/add-greeting

Push confirmation in terminal

Git output after pushing to GitHub

Step 6: Create a Pull Request

Go to your repository on GitHub. You'll see a notification about your new branch:

Pull request prompt

GitHub prompts you to create a PR for your new branch

Click Compare & pull request. Fill in the PR form:

  • Title: "Add greeting function"
  • Description:
    This PR adds a simple greeting function.
    
    ## Changes
    - Added greet() function
    - Added example usage
    

Pull request form

Fill in your PR title and description

Click Create pull request.

Step 7: Review the Pull Request

Your PR page shows:

  • The changes you made (blue = additions)
  • A "Conversation" tab for comments
  • A "Files changed" tab to review the diff

Pull request overview

Your pull request is ready for review

Click on the Files changed tab to see exactly what you modified:

Pull request diff view

Green lines show what you added

Step 8: Merge Your Pull Request

Once you're satisfied with your changes, merge the PR:

  1. Click the drop-down button next to Merge pull request
  2. Choose Squash and merge and click it (keeps history clean)
  3. Click Confirm squash and merge

Merge button and options

Click "Merge pull request" to integrate your changes

Your code is now on main! You can optionally delete the feature branch.

Step 9: Update Your Local Repository

Back on your computer, switch to main and pull the changes:

git checkout main
git pull origin main

Verify your file is there:

ls hello.py
python hello.py  # Runs your script!

Step 10: Create an Issue to Track Future Work

Issues are how teams track bugs, feature requests, and tasks. Let's create one:

  1. Go to your repository on GitHub
  2. Click the Issues tab
  3. Click New issue

New issue button

Click "New issue" to create a task
  1. Fill in the issue form:
  2. Title: "Add farewell function"
  3. Description:
    We should add a farewell function to complement the greeting.
    
    ## Acceptance Criteria
    - Function returns a goodbye message
    - Follows the same style as greet()
    - Include example usage
    
  4. Click Submit new issue

Issue creation form

Fill in the issue title and description

Notice GitHub assigns your issue a number (e.g., #2). You can now reference this in commits and PRs! Bonus point : Do you know why the issue gets assigned #2 and not #1?

Step 11: Create a Feature Branch Linked to the Issue

Create a new branch for this issue:

git checkout main
git pull origin main
git checkout -b feature/add-farewell

Now implement the farewell function:

cat >> hello.py << 'EOF'

def farewell(name):
    """A simple farewell function."""
    return f"Goodbye, {name}!"

if __name__ == "__main__":
    print(greet("World"))
    print(farewell("World"))
EOF

Commit your changes referencing the issue number:

git add hello.py
git commit -m "feat: add farewell function

- Implement farewell() function
- Add to main execution

Closes #2"

The Closes #2 keyword tells GitHub to automatically close issue #2 when this PR is merged!

Push to GitHub:

git push -u origin feature/add-farewell

Go to GitHub and create a new PR:

  1. Click Compare & pull request
  2. Fill in:
  3. Title: "Add farewell function"
  4. Description:
    Implements the farewell function requested in issue #2.
    
    ## Changes
    - Added farewell() function
    - Updated main execution
    
    Closes #2
    

PR linked to issue

Reference the issue number in your PR description
  1. Click Create pull request

When you merge this PR, GitHub will automatically close issue #2!

Step 13: Enable Branch Protection Rules

Let's set up best practices to require code review before merging to main:

  1. Go to your repository → Settings tab
  2. Click Branches in the left sidebar
  3. Click Add classic branch protection rule under "Branch protection rules"

Branch protection settings

Navigate to branch protection rules
  1. Configure the rule:
  2. Branch name pattern: main
  3. Check: Require a pull request before merging
  4. Check: Require at least 1 approval
  5. Check: Dismiss stale pull request approvals
  6. Click Create

Branch protection options

Select protection rules for the main branch

Now, direct pushes to main are blocked! All code must go through a PR with review.

Step 14: Collaboration - Forking and Contributing (Optional)

Want to contribute to someone else's project? Use fork workflow:

  1. Go to the repository you want to contribute to - https://github.com/sidmohite/hello-github-fork
  2. Click Fork in the top right

Fork button

Fork creates a copy of the repository under your account
  1. Clone your fork: Locally, change your directory to where you want to place this "forked" project and then

    git clone https://github.com/your-username/hello-github-fork.git
    cd hello-github-fork
    

  2. Add upstream remote to track the original repo:

    git remote add upstream https://github.com/sidmohite/hello-github-fork.git
    git remote -v  # Verify you have both origin and upstream
    

  3. Create a feature branch and make changes:

    git checkout -b fix/bug-fix
    # ... make changes ...
    git commit -m "fix: resolve issue with feature"
    git push origin fix/bug-fix
    

  4. On GitHub, click Compare & pull request to submit PR to the original repo

  5. Maintain your fork by pulling updates from upstream:
    git fetch upstream
    git checkout main
    git merge upstream/main
    git push origin main
    

Step 15: Best Practices Checklist

Now that you've gone through the full workflow, here's what you should always follow:

✓ Always use feature branches

git checkout -b feat/description or fix/description

✓ Write clear, atomic commits

git commit -m "feat: concise description of change"

✓ Link commits to issues

Closes #42  or  Fixes #1

✓ Require code review before merging (via branch protection) - Set in Settings → Branches → Add rule

✓ Use squash and merge for feature branches to keep history clean - Keep main history readable

✓ Never force push to main

# Never do this on main:
git push --force origin main  # ❌ Dangerous!

✓ Keep PRs focused and small - Easier to review - Easier to revert if needed - Fewer merge conflicts

✓ Add meaningful PR descriptions - Explain the "why", not just the "what" - Reference related issues - Include testing instructions


Key Concepts from This Workflow

✓ Branching: Separate branches protect the main code
✓ Commits: Each commit is a checkpoint with a clear message
✓ Issues: Track bugs, features, and tasks
✓ Pull Requests: A review gate before code reaches main
✓ Merging: Integrating changes with full history
✓ Branch Protection: Enforce code review requirements
✓ Forking: Contribute to projects you don't own

This workflow is the standard across industry and open source projects. Practice it repeatedly!

Summary

You now have the knowledge to effectively use GitHub for collaboration, code review, and project management. Practice these workflows in your own repositories!

Resources