GitHub is the go-to platform for hosting and sharing code, whether you’re collaborating with a team, contributing to open-source, or building your personal portfolio. One of the most common tasks developers need to perform is pushing files to a GitHub repository. This guide will walk you through the process step-by-step.
What Does “Pushing to GitHub” Mean?
In Git, pushing refers to uploading your local repository content to a remote repository—in this case, GitHub. This is typically done after adding or changing files and committing those changes locally.
Prerequisites
Before you begin, make sure you have:
- A GitHub account
- Git installed on your computer
- A terminal or command line interface (e.g., Terminal on macOS, Command Prompt or Git Bash on Windows)
Option 1: Push Files to a New Repository
Step 1: Create a New Repository on GitHub
- Go to github.com
- Click + (top-right) → New repository
- Give your repository a name, description, and choose visibility
- Click Create repository
⚠️ Do not check “Initialize this repository with a README” if you’re pushing an existing local project.
Step 2: Initialize Git in Your Project Folder
Open your terminal and navigate to your project directory:
cd path/to/your-project
Initialize a Git repository:
git init
Step 3: Add and Commit Your Files
git add .
git commit -m "Initial commit"
Step 4: Link to the Remote GitHub Repository
Copy the remote URL from your new GitHub repo and run:
git remote add origin https://github.com/your-username/your-repo.git
Step 5: Push Your Code to GitHub
git push -u origin main
Replace
main
withmaster
if your branch is named differently.
Option 2: Push Updates to an Existing GitHub Repository
If your local repo is already connected to GitHub:
- Make changes or add new files
- Stage and commit:
git add .
git commit -m "Update files or features"
- Push to GitHub:
git push
Optional: Check Remote Connection
To verify your remote repository is set up:
git remote -v
Tips for a Clean Push
- Use
.gitignore
to exclude files likenode_modules/
,.env
, or temporary files. - Use clear and descriptive commit messages.
- Use branches for features or bug fixes to keep your
main
branch clean.
Common Errors and Fixes
- Authentication Failed: Make sure you have access to the repo and use GitHub CLI or a personal access token if using HTTPS.
- fatal: remote origin already exists: Run
git remote set-url origin <new-url>
to change it. - rejected – non-fast-forward: Pull changes first with
git pull --rebase origin main
, then push again.
Conclusion
Pushing files to GitHub is a core skill for every developer. Once you understand the flow—initialize, add, commit, link, and push—you can manage and share code with confidence.