☁️ Chapter 10: Remote Repositories — Cloud-Based Code Storage
Adding a Remote Repository
A local repository is great, but the code only exists on one computer. What if the hard drive fails? Who do you cry to? That's why we need to push code to a remote repository (like GitHub, GitLab, or Gitee).
bash
# View remote repositories
git remote -v
(empty output, meaning no remote repository yet)
# Add a remote repository (SSH)
git remote add origin git@github.com:yourname/my-first-website.git
# Add a remote repository (HTTPS)
git remote add origin https://github.com/yourname/my-first-website.git
# View again
git remote -v
origin git@github.com:yourname/my-first-website.git (fetch)
origin git@github.com:yourname/my-first-website.git (push)
# origin is the default name for the remote repository, you can use other names
# But everyone uses origin, so it's recommended to stay consistentPush — Push to Remote
bash
# First push (-u sets upstream tracking)
git push -u origin main
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 8 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (9/9), 1.75 KiB | 1.75 MiB/s, done.
Total 9 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:yourname/my-first-website.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
# After that, just push directly (tracking is already set up)
git push📝 Note:
-u(i.e.,--set-upstream) is only needed on the first push. It establishes a "tracking relationship" between the local and remote branches. After that, just usegit pushandgit pull.
Fetch vs Pull
These two commands are often confused, but the difference is actually simple:
bash
# fetch: download only, don't merge (safe, lets you review changes first)
git fetch origin
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), 2.10 KiB | 2.10 MiB/s, done.
From github.com:yourname/my-first-website
f4e5d6c..a7b8c9d main -> origin/main
# Check what's new on remote
git log HEAD..origin/main --oneline
# Confirm it's OK, then merge
git merge origin/main
# ─────────────────────────────────
# pull: download and auto-merge (= fetch + merge)
git pull
remote: Enumerating objects: 5, done.
From github.com:yourname/my-first-website
f4e5d6c..a7b8c9d main -> origin/main
Updating f4e5d6c..a7b8c9d
Fast-forward
README.md | 3 +++
1 file changed, 3 insertions(+)
create mode 100644 README.md💡 Tip:
git pull=git fetch+git merge. If you've configuredpull.rebase = true, thengit pull=git fetch+git rebase. Beginners should use the default pull (merge mode) first, then consider rebase mode once they're more comfortable.
SSH vs HTTPS
| Comparison | SSH | HTTPS |
|---|---|---|
| Address format | git@github.com:user/repo.git | https://github.com/user/repo.git |
| Authentication | SSH key (password-free once set up) | Username/password or Token |
| Security | Higher | Relies on HTTPS |
| Use case | Daily development (recommended) | Temporary cloning, CI/CD |
| Firewall | May be blocked (port 22) | Rarely blocked (port 443) |