Module 3 · Git & GitHub: From First Commit to Pull Request
GitHub and Remotes: Push, Pull and Clone
Connect the repository on your computer to GitHub. Learn what a remote is, how to sign in safely, and the everyday rhythm of clone, pull, commit and push, with the commands practised in the terminal and the real thing on GitHub.
About 45 minutes
One repository, many copies
So far everything has lived on your own computer. GitHub gives your repository a second home on the internet. Your project then exists in (at least) two places:
- Local: the repository on your computer, where you work.
- Remote: a copy on GitHub (a remote is any other copy of the repository).
Git moves commits between them with a few simple commands. The remote you will use most is called origin, which is just the standard nickname for the repository you cloned from or pushed to first.
Output
Your computer GitHub
(local repo) --- git push ---> (origin)
<--- git pull ---Create a repository on GitHub
- On GitHub, click +, then New repository.
- Choose a short name with hyphens, for example
recipe-notebook. - Add a description. Choose Public (for a portfolio) or Private.
- If you already have a project on your computer, leave "Add a README" unticked, so GitHub does not create a first commit that clashes with yours.
- Click Create repository. GitHub shows you the address, like
https://github.com/ada-okafor/recipe-notebook.git.
Connect and push
On a repository you already have, you connect the remote and push your commits:
Shell (bash)
git remote add origin https://github.com/ada-okafor/recipe-notebook.git
git remote -v # check it: shows the address for fetch and push
git push -u origin maingit remote add origin <url>gives the remote a name. You do this once.git push -u origin mainsends yourmainbranch toorigin. The-umakes Git remember the link, so later you can just typegit push.
Refresh the GitHub page and your files and commit history are there.
Try it. This project has a commit. Connect it to a pretend remote and push.
Files in your project folder
Type a command below and press Enter. Type help for the commands you can use. Press the up arrow to bring back an earlier command.
Run git remote add origin https://github.com/ada-okafor/recipe-notebook.git, then git remote -v, then git push -u origin main, and look at the log: git log --oneline now shows origin/main beside your commit, showing the remote is up to date.
Signing in: passwords do not work
When Git talks to GitHub, it needs to know it is really you. GitHub does not accept your account password for Git operations. You have three good options:
| Option | How it works | Best for |
|---|---|---|
| GitHub Desktop or VS Code sign-in | Click sign in. A browser window confirms it. The tool remembers you | Beginners: easiest |
| HTTPS with a personal access token | You create a token (like a long password) in GitHub Settings, then Developer settings, then Personal access tokens, and paste it when Git asks | Quick command-line use |
| SSH keys | You create a key pair, give GitHub the public half, and never type a password again | Daily professional use |
Whichever you use, remember: a token or a private key is a secret. Do not put it in a file you commit, and do not share it. If it leaks, delete it on GitHub right away.
The four commands you will use every day
| Command | What it does |
|---|---|
git clone <url> | Download a whole repository (all its history) to your computer for the first time |
git pull | Get the latest commits from the remote and merge them into your branch |
git push | Send your new commits to the remote |
git fetch | Get the remote's commits to look at, without merging them yet |
Shell (bash)
git clone https://github.com/ada-okafor/recipe-notebook.git
cd recipe-notebookclone creates a folder, copies everything, and automatically sets up origin for you. You clone a repository once.
The daily rhythm
Once you work with a remote, your day follows a pattern:
git pullto start with the latest work.- Do your work, and commit it in small pieces.
git pullagain, in case teammates pushed while you worked.git pushto share your commits.
The most common error teaches why step 3 matters:
Output
! [rejected] main -> main (fetch first)
error: failed to push some refsIt means the remote has commits you do not have yet. The fix is simple: git pull (resolve any conflict as in lesson 6), then git push again.
Seeing remote branches
After you push or fetch, Git keeps a read-only bookmark of where the remote's branches are, written origin/main. In git log --oneline you can see HEAD -> main, origin/main when you are level with the remote. If main is ahead of origin/main, you have commits to push.
Fork: your own copy of someone else's project
You cannot push to a repository you do not own. To contribute to someone else's project (or to start from it), you make a fork: your own copy of it on GitHub, under your account. You clone your fork, make changes, and ask the owner to take them through a pull request, which is the next lesson.
Try it
First, connect a project to GitHub and push it. (The remote is simulated here, and you do the real thing in the last task.)
Task
8 minThis repository only exists on your computer. Add a remote called origin that points to a GitHub address such as https://github.com/ada-okafor/recipe-notebook.git, check it, and push main with the -u option.
Your repository is checked for
- You added a remote called origin with a GitHub address
- The remote exists in the repository
- Your main branch is pushed to origin
- You used -u to link main to origin/main
- Everything is committed (clean)
Files in your project folder
Type a command below and press Enter. Type help for the commands you can use. Press the up arrow to bring back an earlier command.
Now the daily rhythm. The project is already on GitHub. Make a change and push it.
Task
8 minThe project is already on GitHub. Add Garri to shopping.txt, commit it, pull to check for new work, and then push it.
Your repository is checked for
- You pulled before pushing
- shopping.txt is committed, with at least three commits in total
- shopping.txt includes Garri
- Your new commit is pushed (origin/main matches main)
- Everything is committed (clean)
Files in your project folder
Type a command below and press Enter. Type help for the commands you can use. Press the up arrow to bring back an earlier command.
Finally, do the real thing. Put a project of yours on GitHub, from your own computer.
Task
20 minOn your own computer (or with GitHub Desktop), make a repository for a real project with at least two commits, create a repository on GitHub, connect it with git remote add origin, and push it. Paste your repository link on the first line, and on the second line the commands you used to connect and push.
Your work is checked for
- A link to a GitHub repository
- The remote add command
- The push command
Practice
A colleague tells you to git clone a project you already have on your computer. Which command should you use instead to get the latest changes from GitHub? Type the full command.
Earn your badge
Take the module check
Questions on this module. Pass and you earn the GitHub Collaboration badge, free.
Create a free account to earn the badge