← All chapters

Chapter 4 Sidequest

Sidequest: Your First GitHub Push

Setting up GitHub and pushing your first repository to the cloud.

ℹ️What You'll Need
  • About 20 minutes
  • An email address
  • A phone number (for 2FA)

Your Git repository lives on your computer right now. That's fine—but what if your hard drive dies? What if you want to work on your project from a different computer?

That's where remote repositories come in. GitHub is the most popular place to store them—like a cloud backup for your code.

Step 1: Create a GitHub Account

Head over to github.com and click "Sign up." Pick a username you won't be embarrassed by in five years—this is your professional identity now.

💡Username Tips
  • Keep it simple and readable
  • Avoid numbers if possible (unless they're meaningful)
  • This will be part of your URLs forever

Good: maxpower, max-power, themaxpower Avoid: xXxMaxPower2024xXx, coolhacker9000

You'll need to verify your email and set up two-factor authentication (2FA). Yes, it's annoying. Yes, it's necessary. People try to hack into GitHub accounts all the time because that's where the code lives.

Step 2: Create a Repository on GitHub

Once you're in, click the green "New" button to create a repository.

📁.

Click "Create repository." You'll see a page with instructions. We want the section that says "push an existing repository from the command line."

Step 3: Set Up SSH Keys

Before you can push code to GitHub, you need to prove you're you. The most secure way is with SSH keys—a pair of files that act like a really long password.

😕
Max

What's SSH?

"Secure Shell. It's how computers talk to each other securely. Right now, we just need to create a key pair—one private key (never share this) and one public key (you give this to GitHub)."

$Generate an SSH key

It'll ask where to save the key—just press Enter for the default location. It'll ask for a passphrase—pick something you can remember, or leave it blank (less secure, but simpler for learning).

Now you have two files:

  • ~/.ssh/id_ed25519 — Your private key. Never share this.
  • ~/.ssh/id_ed25519.pub — Your public key. This goes on GitHub.
$Copy your public key

Select and copy the entire output (it starts with ssh-ed25519 and ends with your email).

Now go to GitHub → Settings → SSH and GPG Keys → New SSH Key. Paste it in, give it a name like "My laptop," and save.

$Test the connection
terminal output(text)

That message means it worked! You're connected.

Step 4: Connect Your Local Repo to GitHub

Now we tell your local Git repository where the remote copy lives.

$Add the remote origin
⚠️Use YOUR Username

Replace YOUR-USERNAME with your actual GitHub username. The URL should match what GitHub showed you after creating the repository.

$Push your code
terminal output(text)
😮
Max

It's on the internet! I can see it on GitHub!

Refresh your GitHub repository page. Your files are there. Every commit you made is there. The full history, backed up in the cloud.

Step 5: The Daily Workflow

From now on, after you commit locally, push to keep GitHub in sync.

$The push workflow

And if you ever work from a different computer (or want to start fresh), you can clone the repository:

$Clone from GitHub
💡Public vs Private Repositories
  • Private: Only you can see it. Good for personal projects.
  • Public: Anyone can see (but not change) your code. Good for sharing.

You can change this setting anytime in the repository settings.

When Things Get Complicated

Once you have code in two places—your computer and GitHub—things can get tangled. Here are the scenarios that will definitely happen to you eventually.

"Someone Changed the File on GitHub!"

Maybe you edited a file directly on GitHub's website (it has an editor). Maybe you worked on a different computer. Now you try to push and Git says no:

terminal output(text)
Max

What?! I just want to push my changes!

"Git is protecting you," I said. "There are changes on GitHub that you don't have. If you just pushed, you might overwrite them. First, you need to pull those changes down."

$Pull changes from GitHub

Git will try to merge the remote changes with your local changes. If the changes were in different files, it just works. If they're in the same file... you might get a merge conflict. We'll deal with that in a second.

But there's a cleaner way to do this: pull --rebase.

$Pull with rebase (cleaner history)

"What's the difference?" Max asked.

"Regular git pull creates a 'merge commit'—a record that you combined two histories. It's fine, but it clutters your log. git pull --rebase pretends your changes happened after the remote changes. Cleaner history, same result."

"For a personal project, either works. But as you get more advanced, you'll appreciate rebase."

"Git Won't Let Me Pull Because I Have Uncommitted Changes!"

This one's common. You edited a file but didn't commit. Now you try to pull and Git complains:

terminal output(text)
😕
Max

Stash? What's stash?

"Stash is like a temporary pocket. You can stuff your uncommitted changes in there, do what you need to do (like pull), then take them back out."

$Stash your changes
terminal output(text)

Your changes are now safely stored. Your working directory is clean.

$Now pull works
$Bring your changes back

"Pop" takes the most recent stash and applies it to your current files. Your changes are back, combined with whatever you pulled.

💡Stash Commands
git stash          # Hide uncommitted changes
git stash pop      # Bring them back and remove from stash
git stash list     # See all stashed changes
git stash drop     # Delete the most recent stash
git stash apply    # Bring them back but keep in stash too

"I Messed Everything Up—How Do I Start Over?"

Sometimes you just want to throw away everything and get back to the last commit. Or the last pushed commit. Git has several "reset" options:

⚠️Reset Options (Use Carefully!)
# Soft reset: undo commit, keep changes staged
git reset --soft HEAD~1

# Mixed reset (default): undo commit, keep changes but unstaged
git reset HEAD~1

# Hard reset: undo commit AND throw away all changes
git reset --hard HEAD~1

HEAD~1 means "one commit before HEAD (your current position)."

--hard is dangerous! It throws away work. Only use it when you're sure.

"But what if I already pushed?" Max asked. "And I want to match what's on GitHub?"

$Reset to match GitHub (nuclear option)

"This throws away all your local changes and makes your folder match exactly what's on GitHub. It's the 'I give up, start fresh' option."

"Use it when you've made a mess and just want a clean slate. But be aware—any uncommitted work is gone forever."

What About Passwords?

Remember how we talked about .gitignore and never committing secrets? This is extra important now that your code is on the internet.

Before you push, always check:

$Check what's about to be pushed
⚠️If You Accidentally Push Secrets

If you accidentally push a password or API key to GitHub:

  1. Change the secret immediately. Assume it's compromised.
  2. Don't just delete the file—it's still in your Git history.
  3. For serious cases, you need to rewrite history (ask for help with this).

Prevention is easier than cleanup. Check before you push.

Summary

"That's a lot," Max said.

"It is. But here's the thing—you won't remember all of this. Nobody does. What matters is knowing that solutions exist. When you hit an error, you'll think 'oh right, stash' or 'I need to pull first' and look up the command."

"Git is like that. It's a toolbox. You learn the tools as you need them."

Type a command below or scroll through content...
$
Vol
0/4
Ch
0/1
Cmd
0
Egg
0/7
LVL 1
Newbie
0 XP