Unit 7: Sharing
Your project, online.
Unit 7 of 8 in Git and version control for kids. Its 5 lessons are A Copy That Lives Online, Ahead and Behind, Cloning, Catching Up and Remote Boss — below is everything each one explains, and a question or two from it to try.
Every terminal session on this page was replayed before it shipped, on a git whose every message is tested against the real one.
This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.
☁️ A Copy That Lives Online
What GitHub actually is
GitHub is not git. It is a computer on the internet that holds another copy of your repository.
That copy is called a remote. Yours is nearly always nicknamed origin. Sending commits there is a backup, and it is how other people get your work.
Introducing them
git remote add origin <address> teaches your repo where the online copy lives. It does not send anything yet — it is writing down an address.
Terminal
$ git remote add origin https://github.com/you/notes.git $ git remote -v origin https://github.com/you/notes.git (fetch) origin https://github.com/you/notes.git (push)
Pushing
git push -u origin main sends your commits up. The -u is only needed the first time: it remembers the pairing, so afterwards plain git push is enough.
Notice git now says whether you are ahead of the online copy.
Terminal
$ git push -u origin main To https://github.com/you/notes.git * [new branch] main -> main branch 'main' set up to track 'origin/main'. $ git status On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
Try it yourself
What is origin?
- The first commit
- The usual nickname for your online copy
- The main branch
- Your username
Answer it in the app
📶 Ahead and Behind
origin/main is a memory
Your repo keeps a branch called origin/main. It is your memory of where the online copy was last time you looked — not a live reading.
That is why git status can say "up to date" when it is not. It is telling you the truth about what it knows.
Terminal
$ git status On branch main Your branch is ahead of 'origin/main' by 1 commit. (use "git push" to publish your local commits) nothing to commit, working tree clean
Try it yourself
"Your branch is ahead of 'origin/main' by 1 commit." What should you do?
- git pull
- git push
- git merge
- Nothing is wrong, ignore it forever
Answer it in the app
📥 Cloning
Getting a whole project
git clone <address> downloads a repository — all of it, every commit and branch ever made — and sets origin up for you.
After cloning you have a complete, working copy. Everything you have learned works offline on it.
Terminal
$ git clone https://github.com/sam/recipes.git recipes Cloning into 'recipes'... done. $ cd recipes $ ls cake.txt $ git log --oneline cae6ffc (HEAD -> main, origin/main, origin/HEAD) Add the cake recipe
Try it yourself
What do you get when you clone?
- Only the newest files
- The files plus the entire history
- A link to the files online
- Just the branch names
Do you have to run git init after cloning?
- Yes, always
- No — a clone is already a repository
- Only for big projects
- Only if you want branches
Answer them in the app
🔄 Catching Up
Two ways to look
git fetch asks the server what is new and updates your origin/main memory. It does not touch your files. Perfectly safe.
git pull does the same thing and then merges the new commits into your branch. It is fetch plus merge.
Terminal
$ git fetch From https://github.com/team/notes 8d532b5..64bf0fc main -> origin/main $ git status On branch main Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded. (use "git pull" to update your local branch) nothing to commit, working tree clean
Actually getting the work
Now git pull brings the commits into your branch, and the new file appears on your disk. Because you had made no commits of your own, it is a plain fast-forward.
Terminal
$ git pull From https://github.com/team/notes 8d532b5..64bf0fc main -> origin/main Updating 8d532b5..64bf0fc Fast-forward sam.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 sam.txt $ ls notes.txt sam.txt
Try it yourself
Which one changes the files in your folder?
- git fetch
- git pull
- both
- neither
Answer it in the app
🏆 Remote Boss
Try it yourself
Which is safe to run at any moment, because it never changes your files?
- git pull
- git fetch
- git push
- git merge
Answer it in the app