Whether you’re contributing to an open-source project or managing your own codebase, knowing how to upload files to a GitHub repository via the command line is an essential skill. It’s efficient, powerful, and gives you full control over your version history.
In this blog post, we’ll walk through how to upload (add, commit, and push) files to a GitHub repository using Git on the command line — step by step.
✅ Prerequisites
Before you begin, make sure you have:
- A GitHub account
- Git installed on your computer (
git --version
) - A GitHub repository (public or private)
- Git configured with your username and email:
git config --global user.name "Your Name"
git config --global user.email "yo*@ex*****.com"
🛠️ Step-by-Step Guide
Step 1: Create or Navigate to Your Local Project
If you’re starting fresh:
mkdir my-project
cd my-project
If you already have a project folder:
cd path/to/your/project
Step 2: Initialize Git in the Folder
git init
This creates a .git
directory and turns your folder into a Git repository.
Step 3: Add Files to the Repository
You can now add your files (or create new ones):
echo "# My Project" > README.md
Then stage the files for commit:
git add .
git add .
adds all files in the folder. You can also add specific files:git add file1.py file2.txt
Step 4: Commit the Files
git commit -m "Initial commit"
A commit is like a snapshot of your files at that point in time.
Step 5: Connect to Your GitHub Repository
Now, add the remote origin. Replace the URL with your GitHub repository’s:
git remote add origin https://github.com/your-username/your-repo.git
To check that the remote is set:
git remote -v
Step 6: Push the Files to GitHub
If this is the first push to a new repository:
git push -u origin main
If your default branch is named
master
instead ofmain
, adjust accordingly:git push -u origin master
The -u
flag sets the upstream so future pushes can be done with just git push
.
🔁 Updating the Repository Later
Once your project is connected, future changes follow this pattern:
git add .
git commit -m "Describe your changes"
git push
📋 Summary
Task | Command |
---|---|
Initialize Git | git init |
Add files | git add . or git add <file> |
Commit changes | git commit -m "Your message" |
Add GitHub remote | git remote add origin <repo-URL> |
Push to GitHub | git push -u origin main |
🚨 Common Mistakes to Avoid
- ❌ Forgetting to commit before pushing
- ❌ Using the wrong remote URL (use HTTPS or SSH)
- ❌ Trying to push to a branch that doesn’t exist on GitHub
✅ Final Thoughts
Uploading files to GitHub from the command line gives you full control over your code, version history, and collaboration process. Once you’re comfortable with the basics, you can explore more advanced features like branching, merging, and pull requests.