Building Module 7 · Git: Your Safety Net

GitHub: Your Cloud Backup

Last reviewed

Beginner

What you'll learn

~25 min
  • Create a GitHub account and install the GitHub CLI
  • Create a remote repository and push a local project to GitHub
  • Understand and configure .gitignore to protect secrets and skip generated files
  • Set up authentication with gh auth login
  • Follow a pre-push checklist before sharing code

You’ll push your first project to GitHub and have a cloud backup you can access from anywhere.

Why This Matters

GitHub is three things at once:

  1. Your backup. If your laptop dies, your code is safe in the cloud.
  2. Your portfolio. Employers and collaborators check GitHub profiles. Every project you push is a signal that you build things.
  3. Your collaboration hub. When you work with others — or even with yourself across multiple machines — GitHub is how code moves between them.

Most professional developers use GitHub or a similar Git hosting platform (GitLab, Bitbucket, Azure DevOps). Setting it up now means every project you build from here forward is automatically backed up and shareable.

The Mental Model: Google Drive for Code

You already use Google Drive or OneDrive to back up documents. GitHub does the same thing for code — but better. Instead of just storing the latest version, GitHub stores the complete history of every change you’ve ever made. You can go back to any point in time, see who changed what, and restore old versions instantly.

Google DriveGitHub
Upload files to the cloudPush commits to a remote repo
Share a link with collaboratorsShare a repo URL
See when a file was last modifiedSee full commit history with messages
One version (unless you dig into history)Full commit history preserved by default

Prerequisites

Before starting this lesson, make sure you have:

  • Git installed and configured (Module 7, Lesson 1) — run git --version to verify
  • A local repository with at least one commit (Module 7, Lessons 2-3) — run git log --oneline to verify

If either of those fails, go back and complete the earlier lessons first.

Step 1: Create a GitHub Account

If you already have a GitHub account, skip to Step 2.

  1. Go to github.com/signup (or choose Continue with Google)
  2. Choose a username — this will be part of your profile URL (github.com/your-username), so pick something professional
  3. Enter your email and create a password
  4. Verify your email address when asked
  5. Your new account is on GitHub Free, which is all you need for this course. You can decide later whether you need a paid plan.
💡Choose your username wisely

Your GitHub username is visible on every project you share. Use your real name or a professional handle. Avoid joke names — future employers will see this.

Step 2: Let Your Agent Handle the GitHub CLI

The GitHub CLI (gh) lets you create repos and sign in from the terminal. You do not need to install it by hand. Ask your agent:

Check whether the GitHub CLI (gh) is installed. Install it if needed, help me sign in, and publish this repo to my GitHub account. Ask me before anything is public.

Approve each step as it asks, and finish the browser sign-in when it opens. Then confirm with the agent that gh --version works.

🔍Installing gh by handclick to expand

If you prefer to install it yourself:

  • macOS: brew install gh
  • Windows (PowerShell): winget install --id GitHub.cli --source winget
  • Linux, WSL, Ubuntu, Debian, Fedora: follow GitHub’s official steps at github.com/cli/cli#installation. The gh in distribution repositories is often too old to work.

Verify with gh --version.

Authenticate with GitHub

Before you can push code, you need to log in:

Terminal window
gh auth login

This starts an interactive walkthrough. As of gh 2.103 (October 2026), choose these options:

  1. Where do you use GitHub? → GitHub.com
  2. How would you like to authenticate GitHub CLI? → Login with a web browser
  3. What is your preferred protocol for Git operations on this host? → HTTPS (simpler for beginners; SSH works too)
  4. Authenticate Git with your GitHub credentials? → Yes

Older versions of gh ask the same questions in a different order.

It will give you a one-time code, open your browser, and ask you to paste it. Once done, you’re authenticated.

🔍HTTPS vs SSHclick to expand

When gh auth login asks about your “preferred protocol,” you are choosing how your computer talks to GitHub:

  • HTTPS (recommended for beginners): Uses your browser or a token for authentication. Simpler to set up, works through most firewalls.
  • SSH: Uses cryptographic keys stored on your machine. More convenient once set up (no repeated logins), but requires generating and managing key files.

Either works fine. If you are unsure, choose HTTPS. You can switch later.

ℹSSH-restricted environments (university HPC, government networks)

If your network blocks browser-based auth, choose Paste an authentication token instead. Generate a personal access token (classic) at github.com/settings/tokens with the repo, read:org, and gist scopes (the minimum gh accepts), then paste it when prompted. For SSH key setup, run gh auth login and select the SSH protocol: gh offers to upload your existing SSH key, or to generate and upload a new one if you have none.

Verify that authentication worked:

Terminal window
gh auth status

You should see your username and the scopes you’ve granted.

.gitignore Essentials

Not everything belongs on GitHub. Some files are generated, some are enormous, and some contain secrets that should never be shared. The .gitignore file tells git which files to skip. Set this up before your first push.

The starter template

Ask your agent to create a .gitignore file for your project, or use this reference starter template in your project root:

REFERENCE FILE .gitignore starter template for web and Python projects open reference hide reference Reference example of a .gitignore file your agent generates to keep secrets, dependencies, and build output out of Git.
# Dependencies (downloaded, not created by you)
node_modules/
__pycache__/
venv/
# Build output (generated, not source code)
dist/
build/
.output/
# Environment variables (secrets!)
.env
.env.local
.env.production
# OS files (junk your operating system creates)
.DS_Store
Thumbs.db
# Editor and tool files (personal settings)
.vscode/
.idea/
.claude/settings.local.json
# (Commit the rest of .claude/: settings.json and skills are shared project config.)
# Logs
*.log

The rule of thumb

Commit things you created. Skip things that are generated or downloaded.

Commit thisDo NOT commit this
Source code (.js, .html, .css, .py)node_modules/ (downloaded)
Configuration (package.json, config files).env files (secrets)
.gitignore itselfBuild output (dist/, build/)
README and documentationOS junk (.DS_Store, Thumbs.db)
Test filesLarge binary files (videos, datasets)
KNOWLEDGE CHECK

Which of these files should go in your .gitignore because it holds secrets and must never be committed?

⚠Accidentally committed a secret? Act fast.

If you pushed a .env file or any file with API keys to GitHub, those secrets are in your git history even if you delete the file. You must:

  1. Immediately revoke the exposed keys at your API provider’s dashboard and generate new ones
  2. Remove the file from tracking: git rm --cached .env
  3. Add .env to your .gitignore
  4. Commit those changes: git commit -m "Remove .env from tracking"

For thorough history cleanup, look into git filter-repo — the tool recommended by Git and GitHub for rewriting history to scrub secrets from old commits. But revoking the keys is the urgent first step.

Step 3: Create Your First Remote Repository

ℹAlready pushed to GitHub in Lesson 3?

If you followed the manual push instructions in Lesson 3, your remote is already set up. Skip to “Future Pushes” below — you just need git push from now on.

Make sure you’re inside your local git project (the one with commits from previous lessons):

⚠Check your .gitignore BEFORE your first push

Once you push to GitHub, your code is on the internet. Make sure .gitignore is set up first (see above). If you accidentally push secrets like API keys, they are in the git history even if you delete them later. Rotate any exposed keys immediately.

Terminal window
cd ~/my-project # Replace with your project folder (e.g., ~/ai-class, or $HOME\ai-class in PowerShell)
git branch -M main # Ensure your branch is named "main" (the modern convention)
gh repo create my-first-project --public --source=. --remote=origin --push
ℹmain vs. master

Git itself still names a new repository’s first branch master unless you configure it otherwise; Git’s documentation says this will change to main when Git 3.0 is released. This course uses main, so the git branch -M main command renames your current branch to main. If your branch is already called main, the command does nothing harmful.

That single command does everything. Here’s what each flag means:

FlagWhat it does
my-first-projectThe name of your GitHub repo
--publicAnyone can see it (use --private if you prefer)
--source=.Use the current directory as the source
--remote=originName the remote connection “origin” (the convention)
--pushImmediately push all your commits to GitHub

After it runs, you’ll see a URL like https://github.com/your-username/my-first-project. Open it in your browser — your code is on GitHub.

ℹIf this command fails

Common issues and fixes:

  • “remote origin already exists”: You already have a remote set up. Check it with git remote -v. Update it with git remote set-url origin https://github.com/YOUR-USERNAME/REPO.git.
  • Authentication error: Run gh auth status to check if you are logged in. If not, run gh auth login again.
  • Push rejected: Run git pull --rebase origin main to sync with the remote, then git push.
💡Why 'origin'?

“Origin” is just a nickname for the GitHub URL. When you run git push, git looks up where “origin” points and sends your code there. You can name it anything, but “origin” is the universal convention — every tutorial, every AI tool, and every collaborator expects it.

Back up your project to GitHub

Ask your agent in plain English:

Create a private GitHub repository for this project and push it, then give me the link.

Open the link in your browser to confirm your files are there. The agent picks the right commands for you; you do not need to memorize any flags.

The manual way (without gh CLI)

If you prefer or if gh isn’t available:

  1. Go to github.com and click + then New repository
  2. Name it, choose Public or Private, and do not add a README or .gitignore (you already have files)
  3. Click Create repository
  4. Copy the commands GitHub shows you and run them:
Terminal window
git remote add origin https://github.com/YOUR-USERNAME/my-first-project.git
git branch -M main
git push -u origin main

Both methods get you to the same place. The gh CLI just does it in one step.

Step 4: The Daily Push Workflow

Now that your repo is connected, pushing new work is simple:

Terminal window
git add .
git commit -m "Add feature description"
git push

Three commands. Your changes are staged, committed, and backed up to GitHub. This is the workflow you’ll use every time you finish a chunk of work.

💡When to push

Push after every meaningful commit — when a feature works, when you fix a bug, when you’re done for the day. Don’t wait until the project is “finished.” Frequent pushes mean frequent backups.

Pre-Push Checklist

Before your first push to GitHub (and as a habit for every new project), verify these items:

  1. .gitignore is in place and includes generated/downloaded folders (e.g., node_modules/, venv/, __pycache__/), .env, and build output (dist/)
  2. No API keys or passwords in any committed file — search your code for strings like sk- (OpenAI and Anthropic keys), AKIA (AWS), ghp_ or github_pat_ (GitHub tokens), or password =
  3. The project builds and runs locally — run your project’s start command (npm run dev, python app.py, or your equivalent) and verify it works
  4. Commits have meaningful messages — not “stuff” or “asdf”
  5. Project config has a reasonable name — package.json, pyproject.toml, or equivalent will be public if the repo is public
ℹMake this a habit

The checklist takes 30 seconds and prevents embarrassing (or dangerous) mistakes. Treat it like a pilot’s pre-flight check — boring but critical.

🧬In Your Field: Biotechclick to expand

Research data and lab notebooks. GitHub repositories make excellent homes for research tool code, analysis scripts, and computational protocols. Keep your raw data in separate storage (institutional servers, cloud storage) and use git for the code that processes that data. Your commit history becomes a traceable record of how your analysis evolved — “Add normalization step for RNA-seq counts,” “Fix batch effect correction,” “Update p-value threshold to 0.01.” When you publish, you can link to the exact commit used for the results in your paper. Some journals now require this level of reproducibility.

📊In Your Field: MIS / Businessclick to expand

Private repos for business projects. When building dashboards, automation scripts, or report generators for a business context, use --private instead of --public when creating repos. Private repositories are free on GitHub and ensure that proprietary business logic, client data schemas, and internal tool configurations stay confidential. You can always change a repo from private to public later — but you can’t un-publish something once it’s been public. When in doubt, start private.

🏛️In Your Field: Government Devclick to expand

GitHub Enterprise and restricted environments. Many government agencies use GitHub Enterprise, GitLab, or Azure DevOps instead of public GitHub. The git commands are identical; the only difference is the URL in git remote add origin. Check with your IT team about which hosting service is approved. Some agencies require security scanning (like GitHub Secret Protection and GitHub Code Security, the products formerly sold together as GitHub Advanced Security, or Snyk) before code can be pushed. If your network restricts access to github.com, ask about VPN exceptions for developer tools or use your agency’s internal Git hosting. The key point: get your work off your local machine and into a backed-up, access-controlled repository.

🔧

When Things Go Wrong

Use the Symptom → Evidence → Request pattern: describe what you see, paste the error, then ask for a fix.

Symptom
git push is rejected with 'fetch first' error
Evidence
! [rejected] main -> main (fetch first) / Updates were rejected because the remote contains work that you do not have locally
What to ask the AI
"My git push was rejected because the remote has changes I don't have. How do I pull the remote changes first and then push? Should I use git pull --rebase or a regular git pull?"
Symptom
Permission denied when pushing to GitHub
Evidence
remote: Permission to user/repo.git denied / fatal: unable to access / Permission denied (publickey)
What to ask the AI
"I'm getting a permission denied error when trying to git push. My authentication might not be configured. How do I re-run gh auth login to fix this?"
Symptom
I accidentally pushed my .env file with API keys
Evidence
My .env file with OPENAI_API_KEY is visible on GitHub
What to ask the AI
"I accidentally pushed secrets to GitHub. I need to: 1) revoke the exposed API key immediately, 2) remove .env from git tracking with git rm --cached .env, 3) add .env to .gitignore, 4) commit and push. For history cleanup, use git filter-repo."
Symptom
Error: remote origin already exists
Evidence
fatal: remote origin already exists
What to ask the AI
"Git says remote 'origin' already exists when I try to add it. How do I check what origin currently points to with git remote -v, and how do I update it with git remote set-url if needed?"

Key Takeaways

  • GitHub is your cloud backup and portfolio. Every project you push is safe from hardware failure and visible to collaborators (or employers).
  • Push after every meaningful change. Don’t wait until the project is “done.” Frequent pushes mean frequent backups.
  • .gitignore protects your secrets. Always set it up before your first commit. Never push .env files, node_modules/, or build output.
  • The gh CLI simplifies everything. One command to create a repo and push — no browser required.
  • The daily rhythm is simple: git add ., git commit -m "message", git push. Three commands, ten seconds, complete backup.
KNOWLEDGE CHECK

What does a .gitignore file do?

KNOWLEDGE CHECK

You just created a new project and want to back it up to GitHub. What's the correct order of steps?

Search lessons