Introduction
In this tutorial, you will learn how to push your locally hosted project code to GitHub.
Prerequisites
To initialize your local repository and push it to GitHub, you will need:
Create a new GitHub repo
Sign into GitHub and create a new empty repo.
Git Commands
Run the following commands after navigating to the project folder you want to add and push to GitHub.
Initialize the Git repo
Make sure you are in the root directory of the project you want to push to GitHub and run:
git init -b main
This step creates a hidden .git
directory in your local project folder used to store all version history and metadata for the project.
Add and commit files
git add . && git commit -m "initial commit"
The git add
command tells git which files to include in the commit. The -A
or --all
argument means "include all" files in that project folder.
The git commit
command creates a new commit with all the files that have been added from the previous step. The -m
or --message
sets the commit message that will be included with the commit.
Check status
git status
The git status
command displays what changes have been staged. In this case, you want to check that all files from your local project directory have been added.
Add new remote repo
git remote add origin <GitHub repo link>
# sets the new remote
git remote -v
# verifies the new remote URL
In git, "remote" refers to a remote version of the same repository, typically hosted on GitHub. "origin" is the default name git gives to a remote repo. git remote add origin
is telling git to add the URL of the default remote server for this repo.
You can run the
git remote -v
command to check that your GitHub repo was added as the remote repo that you will be pushing your local changes to.
Push to GitHub
git push -u -f origin main
The -u
flag sets the remote origin
as the upstream reference that allows you to perform git push
and git pull
commands without specifying an origin
.
The -f
flag stands for force and will automatically overwrite everything in the remote directory.
All steps
1. git init
2. git add . && git commit -m "initial commit"
3. git remote add origin <GitHub repo link>
4. git push -u -f origin main
Conclusion
Now you are all set to track your local code changes remotely in GitHub and understand the basics on how to add project files, commit changes, and push your code. The steps listed above are best for personal projects.
Follow steps 2 and 4 to push your local code changes to your GitHub a couple of times an hour to avoid large commits.
Where to Find Me
Feel free to reach out to me with any questions on Twitter and follow me on TikTok.
Happy building 🧱
Top comments (3)
Short and concise guide, thanks for sharing!
no problem, hope it was helpful!
Or simply use the gh repo create with the - - push option cli.github.com/manual/gh_repo_create