Session 7 · Remotes and GitHub#
⏱ about 75 minutes · 🧰 you need: Session 6 done, a free GitHub account
The big idea#
So far everything lived on your computer. To work with other people, or just to keep a backup, you need a copy somewhere else that everyone can reach. That shared copy is called a remote, and the most popular home for remotes is GitHub.
Everyday example: a shared Google Drive folder. Everyone keeps a working copy on their own laptop; the Drive folder is the “official” copy. You upload (push) your changes and download (pull) other people’s.
Git is distributed: every clone is a complete repository with the whole history. The remote is not special technically. It is simply the copy everyone agrees to share.
1. The vocabulary#
Word |
Meaning |
|---|---|
remote |
a named link to another copy of the repo (usually on GitHub) |
origin |
the default name of the remote you cloned from |
clone |
download a whole repo (and its history) for the first time |
push |
upload your commits to the remote |
fetch |
download new commits from the remote, but do not touch your files or branches |
pull |
fetch plus merge into your current branch |
upstream (branch) |
the remote branch your local branch is linked to, e.g. |
2. Create a repo on GitHub#
Log in to github.com and click New (top left, green) → Create a new repository.
Name it
hello-git. Keep it Public (or Private). Do not tick “Add a README” (we already have code to push).Click Create repository. GitHub shows you the commands below.
3. Connect with SSH (once per computer)#
GitHub needs to know it is really you. The safest way for daily use is an SSH key: a matched pair of files, a private key that stays on your computer (never share it), and a public key you give to GitHub, like a lock you hand out whose only key you keep.
ssh-keygen -t ed25519 -C "asha@example.com"
Press Enter to accept the default location. A passphrase is optional but recommended. Then show the public key:
cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... asha@example.com
Copy the whole line. On GitHub: Settings → SSH and GPG keys → New SSH key, paste it, save. Test:
ssh -T git@github.com
Hi asha-kumar! You've successfully authenticated, but GitHub does not provide shell access.
Important
Copy only the file ending in .pub. The file without .pub is your private key. Never paste it anywhere.
Note
HTTPS instead of SSH: you can use https://github.com/you/repo.git URLs. GitHub no longer accepts account passwords for Git; you need a personal access token (Settings → Developer settings → Tokens) or the GitHub CLI (gh auth login). SSH avoids this, which is why we use it here.
4. Push an existing project to GitHub#
Make a small local repo (or reuse one), then link it to the empty GitHub repo:
cd ~/git-lab
mkdir hello-git && cd hello-git
git init
echo "# Hello Git" > README.md
git add . && git commit -m "Add readme"
git remote add origin git@github.com:YOUR-USERNAME/hello-git.git
git remote -v
origin git@github.com:asha-kumar/hello-git.git (fetch)
origin git@github.com:asha-kumar/hello-git.git (push)
git remote add <name> <url> just saves a nickname for a URL. Now upload:
git push -u origin main
Enumerating objects: 3, done.
Writing objects: 100% (3/3), 220 bytes | 220.00 KiB/s, done.
To github.com:asha-kumar/hello-git.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
origin= which remote.main= which branch.-u(set upstream) links your localmaintoorigin/main, so later a plaingit pushandgit pullwork with no extra words. You need-uonly the first time for each branch.
Refresh the GitHub page: your README is there. 🎉
5. Clone: get a project that exists on GitHub#
cd ~/git-lab
git clone git@github.com:YOUR-USERNAME/hello-git.git hello-git-copy
cd hello-git-copy
git log --oneline
git remote -v
git clone downloads everything, checks out the default branch and sets origin for you. The last argument is the folder name (optional).
6. The daily loop with a remote#
git pull ← start of day: get what others did
... edit, git add, git commit ...
git push ← share your commits
Try it. In hello-git-copy:
echo "Learning Git!" >> README.md
git commit -am "Update readme from the copy"
git push
Now pretend to be a teammate who uses the other folder (hello-git):
cd ~/git-lab/hello-git
git pull
Updating 2a2a2a2..3b3b3b3
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)
7. Fetch vs pull#
git pull is really two commands in one:
git fetch # download new commits; nothing in your folder changes
git log --oneline main..origin/main # peek at what is new
git merge origin/main # now bring them in (git pull = these two steps)
|
|
|
|---|---|---|
Downloads new commits |
✅ |
✅ |
Changes your branch and files |
❌ |
✅ (merges) |
Risk of conflicts |
none |
possible |
Use it when |
you want to look before you merge |
you just want to catch up |
origin/main is a remote-tracking branch: your computer’s last-known picture of main on GitHub. It only updates when you fetch, pull or push.
8. When a push is rejected#
git push
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'github.com:asha-kumar/hello-git.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. ...
Meaning: someone pushed before you. Git will not let you overwrite their work. The fix is always the same:
git pull # merge their work in (resolve conflicts if any)
git push
Danger
Never use git push --force on a shared branch (it overwrites other people’s commits). When you truly must (for example after rebasing your own feature branch), use the safer form git push --force-with-lease, which refuses if someone else pushed in the meantime.
9. Working with remote branches#
git switch -c feature/login # create locally
git push -u origin feature/login # publish it
git branch -a # list local + remote branches
git switch feature/login # (on another computer) after git fetch, creates a tracking branch
git push origin --delete feature/login # delete a remote branch
git remote prune origin # forget remote branches that were deleted
10. Authentication and other remotes#
Task |
Command |
|---|---|
See remotes |
|
Add another remote |
|
Change a URL (for example HTTPS → SSH) |
|
Rename a remote |
|
Remove a remote |
|
Common mistakes#
Mistake |
Fix |
|---|---|
|
SSH key not added to GitHub, or you copied the wrong file. Re-check Step 3 and |
|
You already added it. Use |
|
You are in the wrong folder. |
|
You have no commits yet (or your branch is named |
Committing the |
The secret is now public. Rotate the secret immediately, then see Session 12. |
Pulling with uncommitted changes |
Commit or stash first. |
Hands-on exercises#
Tryout 1 · Publish. Push a repo to GitHub with a README and one more file. Confirm both are visible on the website.
Answer
echo "print('hi')" > app.py
git add app.py && git commit -m "Add app.py"
git push
Refresh the repository page on GitHub.
Tryout 2 · Two copies. Using the two folders (hello-git and hello-git-copy), make a change in each (to different files), push from one, and pull in the other. Then make a change to the same line in both and see what happens on the second push.
Answer
Edit
a.txtin one folder, commit, push. Editb.txtin the other, commit,git pull(clean merge),git push.Edit line 1 of
README.mddifferently in both and commit in both. Push from the first. In the secondgit pushis rejected; rungit pull, get a conflict, resolve it as in Session 6, commit, push.
Tryout 3 · Look before you leap. Make a commit in one folder and push it. In the other folder use git fetch and git log main..origin/main --oneline to see it without changing your files. Then git merge origin/main.
Answer
git fetch
git log main..origin/main --oneline # shows the new commit
git status # "Your branch is behind 'origin/main' by 1 commit"
git merge origin/main
Recap#
✅ A remote is a shared copy of the repo;
originis the usual name. GitHub hosts them.✅ SSH keys prove who you are: public key to GitHub, private key stays with you.
✅
git clone(first time),git push(upload),git pull(fetch + merge),git fetch(look only).✅
git push -u origin <branch>links a branch to its remote twin.✅ A rejected push means:
git pullfirst. Avoid--force.
Next: Session 8 · Collaborating.