Git and GitHub course menu
How to Push a Project to GitHub for the First Time
Create a GitHub account and repository, connect a local project with git remote add, push it with git push -u origin main, and understand remotes, origin and upstream branches.
On this page
So far your history exists only on your computer. GitHub keeps a copy online, which gives you a backup, a way to share code, and a public record of your work.
What is a remote?#
A remote is a named link to another copy of your repository. By convention, the main one is called origin.
Your computer GitHub
notes (main) -- git push --> your-name/notes (main)
<-- git pull --Create a repository on GitHub#
- Sign up at github.com. Your username becomes part of your profile address, so choose something professional.
- Click + at the top right, then New repository.
- Enter a name such as
notes. - Choose Public or Private.
- Leave Add a README, .gitignore and license unset, because you already have a local project.
- Click Create repository.
GitHub then shows the address of your new, empty repository.
Connect and push#
In these commands, your-name stands for your GitHub username. Connect your local repository:
git remote add origin https://github.com/your-name/notes.git
git remote -vorigin https://github.com/your-name/notes.git (fetch)
origin https://github.com/your-name/notes.git (push)Make sure your branch is called main, then push it:
git branch -M main
git push -u origin mainTo https://github.com/your-name/notes.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.Refresh the page on GitHub, and your files and commits are there. The first time, Git asks you to sign in. GitHub no longer accepts your account password here, so see SSH Keys and Access Tokens if that step fails.
What -u does#
-u (short for --set-upstream) links your local main to origin/main. You only need it once per branch. After that, plain git push and git pull know where to go, and git status compares the two:
git statusOn branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree cleanThe everyday flow#
Now the cycle has one more step:
echo "Call the bank" >> notes.txt
git add notes.txt
git commit -q -m "Add bank reminder"
git statusOn 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 cleanahead of 'origin/main' by 1 commit means you have a commit that GitHub does not. Push it:
git push
git statusTo https://github.com/your-name/notes.git
3b1b8d5..528d325 main -> main
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree cleanorigin/main is called a remote-tracking branch. It is your local record of where main was on GitHub the last time you communicated with it.
Push a new branch#
git switch -q -c add-readme
echo "# Notes" > README.md
git add .
git commit -q -m "Add README"
git push -u origin add-readme
git branch -aTo https://github.com/your-name/notes.git
* [new branch] add-readme -> add-readme
branch 'add-readme' set up to track 'origin/add-readme'.
* add-readme
main
remotes/origin/add-readme
remotes/origin/mainManaging remotes#
Common mistakes#
Practice#
Practice exercise
Print the remote's name and address, and then print which remote branch your current branch tracks, using git rev-parse --abbrev-ref @{upstream}.
Show solution
git remote -v
git rev-parse --abbrev-ref @{upstream}origin https://github.com/your-name/notes.git (fetch)
origin https://github.com/your-name/notes.git (push)
origin/add-readmeKey takeaway#
Next: SSH Keys and Access Tokens.
Finished this lesson?