Nishaglobal Education logo

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.

Lesson 8 of 1140 minQuiz: 4 questions

🎯 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
  1. What you'll learn and why it matters
  2. Create a GitHub account
  3. Create an empty repository on GitHub
  4. Connect your project: git remote
  5. Push your commits
  6. Signing in from the terminal
  7. Pushing after that
  8. Practice
  9. Common mistakes
  10. Recap
  11. Quiz
  12. Mark lesson complete

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

  1. Go to github.com and choose Sign up. The free plan is all you need.
  2. Pick a professional username: employers will see it in links like github.com/your-name. Asha chose asha-kumar.
  3. Verify your email address when GitHub asks.
  4. 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

  1. Click + at the top right, then New repository.
  2. Enter the Repository name: my-website.
  3. Choose Public (anyone can see it, ideal for a portfolio) or Private.
  4. Leave Add a README, .gitignore and license unticked: your project already has history, and the repository must start empty.
  5. 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:

Terminal
git remote add origin https://github.com/asha-kumar/my-website.git
git remote -v

Output

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.

GitHub: origingithub.com/asha-kumar/my-websitegit pushgit pullYour computer: my-websitelocal repository with all commitsCommit locally any time; push and pull to stay in step.
Your computer holds the local repository with its commits. git push uploads new commits to the remote repository called origin on GitHub. git pull downloads new commits from GitHub into your local repository.

Push your commits

Terminal
git push -u origin main

Output

To https://github.com/asha-kumar/my-website.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.
PartMeaning
pushUpload commits that GitHub does not have yet
originTo which remote
mainWhich branch
-uRemember 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:

ComputerWhat 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 LinuxThe 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.
Terminal (macOS or Linux, once)
gh auth login

Pushing 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.

Terminal
printf '<footer>Made with HTML and CSS</footer>\n' >> index.html
git commit -q -am "Add footer"
git push
git status

Output

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.

  1. git remote add origin followed by the repository address.
  2. git push -u origin main (sign in if asked).
  3. git status should say the branch is up to date with origin/main.
Show the solution
Solution
git remote add origin https://github.com/asha-kumar/my-website.git
git push -u origin main
git status

Output

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
Solution
printf 'p { line-height: 1.6; }\n' >> styles.css
git commit -q -am "Improve line spacing"
git status
git push -q
git status

Output

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 -v checks it.
  • git push -u origin main uploads the first time; afterwards just git 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. 1. What does `git push` do?

  2. 2. Why create the GitHub repository without a README when you already have a local project?

  3. 3. What does -u in `git push -u origin main` do?

  4. 4. git status says `ahead of 'origin/main' by 2 commits`. What does it mean?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.