GitHub: remotes and your first push
Create a free GitHub account and repository, connect your local project with git remote, sign in securely, and upload your commits with git push, then see your code and history on github.com.
🎯 By the end of this lesson you will be able to:
- Create a GitHub account and an empty repository
- Connect a local repository to GitHub with git remote add
- Push commits with git push and sign in securely
Outline
What you'll learn and why it matters
So far your history lives only on your computer. If the laptop is lost, so is the project. Pushing to GitHub gives you an online backup, lets you work from several computers, and is how you share code with a team, show your work to employers, and publish websites with GitHub Pages.
Create a GitHub account
- Go to
github.comand choose Sign up. The free plan is all you need. - Pick a professional username: employers will see it in links like
github.com/your-name. Asha choseasha-kumar. - Verify your email address when GitHub asks.
- Turn on two-factor authentication (2FA) when prompted, using an authenticator app. GitHub requires it for accounts that contribute code, and it protects your work.
Create an empty repository on GitHub
- Click + at the top right, then New repository.
- Enter the Repository name:
my-website. - Choose Public (anyone can see it, ideal for a portfolio) or Private.
- Leave Add a README, .gitignore and license unticked: your project already has history, and the repository must start empty.
- Click Create repository. GitHub shows a page with the repository's address. Make sure HTTPS is selected and copy the address, which looks like
https://github.com/asha-kumar/my-website.git.
Connect your project: git remote
A remote is a copy of your repository somewhere else, such as on GitHub. By convention the main one is called origin. Add it once, in your project folder:
git remote add origin https://github.com/asha-kumar/my-website.git
git remote -vOutput
origin https://github.com/asha-kumar/my-website.git (fetch) origin https://github.com/asha-kumar/my-website.git (push)
-v (verbose) lists the remotes with their addresses: one for fetching (downloading) and one for pushing (uploading). Both normally point to the same place.
Push your commits
git push -u origin mainOutput
To https://github.com/asha-kumar/my-website.git * [new branch] main -> main branch 'main' set up to track 'origin/main'.
| Part | Meaning |
|---|---|
push | Upload commits that GitHub does not have yet |
origin | To which remote |
main | Which branch |
-u | Remember this link, so next time plain git push and git pull work. Only needed the first time for each branch |
In your own terminal you will also see a few progress lines such as *Enumerating objects* and *Writing objects*; we leave those out in this course because they change every time.
Now refresh the repository page on GitHub: your files, your commit messages and the full history are there. Click the commit count (such as 3 Commits) to browse it.
Signing in from the terminal
The first time you push, Git has to prove it is you. GitHub does not accept your account password in the terminal. Instead:
| Computer | What happens |
|---|---|
| Windows (Git for Windows) | Git Credential Manager, installed with Git, opens a browser window. Sign in to GitHub and approve. Git remembers you afterwards. |
| macOS and Linux | The easiest way is the free GitHub CLI: install it from cli.github.com, run gh auth login, choose GitHub.com, HTTPS and Login with a web browser, and follow the steps. It sets Git up to sign in for you. |
gh auth loginPushing after that
Once the link is set with -u, the daily routine is: commit locally as often as you like, then git push to upload.
printf '<footer>Made with HTML and CSS</footer>\n' >> index.html
git commit -q -am "Add footer"
git push
git statusOutput
To https://github.com/asha-kumar/my-website.git 1ce7ed1..b71dfe2 main -> main On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
Your branch is up to date with 'origin/main' means GitHub has everything you have. origin/main is Git's memory of where main was on GitHub the last time you talked to it.
Practice
Guided exercise
Connect and push
Connect Asha's project to the GitHub repository and push main for the first time. Then show the status.
git remote add originfollowed by the repository address.git push -u origin main(sign in if asked).git statusshould say the branch is up to date with origin/main.
Show the solution
git remote add origin https://github.com/asha-kumar/my-website.git
git push -u origin main
git statusOutput
To https://github.com/asha-kumar/my-website.git * [new branch] main -> main branch 'main' set up to track 'origin/main'. On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
How to get there: The remote is added once per project. -u links your main branch to GitHub's main, which is why status can now compare the two and say they are up to date.
Your turn
Ahead of GitHub
Make a commit without pushing, and use git status to see what Git says. Then push and check again.
Show the solution
printf 'p { line-height: 1.6; }\n' >> styles.css
git commit -q -am "Improve line spacing"
git status
git push -q
git statusOutput
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 On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
How to get there: ahead of 'origin/main' by 1 commit means you have a commit GitHub does not. After pushing, the branch is up to date again. Checking git status like this is a quick way to see if you forgot to push.
Common mistakes
Ticking *Add a README* when creating the repository
You see: Your first push is rejected because GitHub already has a commit you do not
Fix: Create the repository empty, or first run git pull origin main --allow-unrelated-histories.
Typing your GitHub password in the terminal
You see: Authentication failed or Password authentication is not supported
Fix: Sign in through the browser window (Windows) or gh auth login (macOS, Linux).
git remote add twice
You see: error: remote origin already exists
Fix: Check with git remote -v; change it with git remote set-url origin <address>.
Forgetting to push
You see: Your work is missing on another computer or for your team
Fix: git status says *ahead of 'origin/main'* when you have unpushed commits.
Recap
- GitHub hosts an online copy of your repository: create it empty, then connect it.
git remote add origin <url>links the project;git remote -vchecks it.git push -u origin mainuploads the first time; afterwards justgit push.- Sign in with the browser or GitHub CLI, never a password; status shows ahead/behind.
Key terms
- Remote
- Another copy of the repository, usually on a server such as GitHub.
- origin
- The usual name for the main remote of a repository.
- Push
- Upload local commits to a remote.
- origin/main
- Git's record of where main was on the remote last time it checked.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. What does `git push` do?
2. Why create the GitHub repository without a README when you already have a local project?
3. What does -u in `git push -u origin main` do?
4. git status says `ahead of 'origin/main' by 2 commits`. What does it mean?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.