Hey there, fellow coder! If you’ve just created a fresh GitHub repository and have an existing Python project sitting on your local machine (with the folder name matching your repo for simplicity), pushing it up can feel tricky—especially with GitHub’s authentication changes. But don’t worry; I’ve got you covered. This guide walks you through the process from start to finish, including handling that pesky login prompt during git push. By the end, your code will be safely on GitHub, ready for collaboration or backups.
Whether you’re a newbie or just need a refresher, follow these steps in your terminal (Command Prompt, PowerShell, Git Bash, or whatever you prefer). I’ll assume your repo is empty (no README or other files added during creation) and you’re using HTTPS for the remote URL initially.
Step 1: Prepare Your Local Project
Before anything, make sure Git is installed on your machine. If not, download it from git-scm.com.
- Open your terminal and navigate to your project folder:
cd path/to/your-project-folder
Example: cd ~/projects/my-python-app (on macOS/Linux) or cd C:\Users\YourName\Projects\my-python-app (on Windows).
- If your folder isn’t already a Git repository, initialize it:
git init
This sets up Git tracking in your folder.
Step 2: Stage and Commit Your Files
Now, add your files to Git and create an initial commit.
- Stage all files (this includes your Python scripts, requirements.txt, etc.):
git add .
Tip: If you have files you don’t want to upload (like sensitive data or virtual environments), create a .gitignore file first and add patterns like venv/ or *.pyc.
- Commit the changes with a meaningful message:
git commit -m "Initial commit: Added existing Python project"
Step 3: Connect to Your GitHub Repository
Link your local repo to the remote one on GitHub.
- Add the remote URL (replace with your actual details):
git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPO-NAME.git
Example: git remote add origin https://github.com/kosh/my-python-project.git.
- Verify the remote:
git remote -v
This should list your origin URL for fetch and push.
- Rename your branch to ‘main’ (GitHub’s default):
git branch -M main
Step 4: Push Your Code (and Handle Authentication)
This is where the magic happens—but also where authentication trips people up.
- Run the push command:
git push -u origin main
When prompted for credentials:
GitHub no longer accepts your regular account password for git push (this change happened years ago, around 2021). When it prompts for username and password during git push -u origin main, it actually expects:
- Username: your GitHub username (e.g.
kosh123— just type it normally) - Password: not your GitHub password → instead paste a Personal Access Token (PAT)
Quick & Recommended Way (One-time setup with credential helper)
Modern Git installations (especially on Windows/macOS/Linux in 2026) usually include Git Credential Manager (GCM). This is the easiest long-term solution — it pops up a browser window to log in (supports 2FA/SAML) and securely stores the token for you.
- Check if you already have it:
git config --global credential.helper
- If it shows
manager,manager-core, or something similar → you already have GCM.
- If not installed or not working → install/update Git Credential Manager:
- Windows: usually comes with recent Git for Windows
- macOS:
brew install git-credential-manager(if you have Homebrew) - Linux: follow https://github.com/git-ecosystem/git-credential-manager (often via package manager)
- Run the push again:
git push -u origin main
→ A browser window / popup should appear → log in to GitHub → authorize → done.
Future pushes/pulls will happen silently (no more prompts).
Manual Way: Create and Use a Personal Access Token (PAT)
If the browser login doesn’t appear or you prefer manual control:
- Go to GitHub → click your profile picture (top-right) → Settings
- Left sidebar → Developer settings
- Personal access tokens → Tokens (classic) (or try fine-grained first — see note below)
- Generate new token (classic)
- Note: e.g. “push access 2026”
- Expiration: 90 days or “No expiration” (but shorter = safer)
- Scopes: at minimum check repo (gives full repo read/write access)
- Click Generate token
- Copy the token immediately (it starts with
ghp_or similar — you won’t see it again) - Back in terminal — when
git pushasks:
- Username: your GitHub username
- Password: paste the token (nothing will show as you type/paste — normal)
After one successful push, most systems cache it (via credential helper) so you won’t be asked again.
Fine-grained token (more secure, but sometimes trickier)
GitHub prefers these now:
- In step 3 above choose Fine-grained tokens → Generate new token
- Under “Repository access” → select Only select repositories → pick your new repo
- Permissions → under Repository permissions give Contents → Read and write
- Generate & use exactly like above
(Note: fine-grained tokens sometimes don’t work for public repos you don’t own or in certain org setups — fall back to classic if it fails.)
Quick one-liner alternative (not recommended long-term)
You can embed the token in the remote URL (visible in .git/config — only do this on personal trusted machines):
git remote set-url origin https://YOUR_USERNAME:YOUR_TOKEN@github.com/YOUR_USERNAME/YOUR-REPO.git
Then git push works without prompting (but token lives in plain text — security risk).
Quick Recap
- Username: Enter your GitHub username (e.g.,
kosh). - Password: Don’t use your actual GitHub password! GitHub phased that out in 2021. Instead, use a Personal Access Token (PAT). How to Create a PAT (if you don’t have one):
- Log in to GitHub and go to your profile > Settings > Developer settings > Personal access tokens > Tokens (classic).
- Click “Generate new token.”
- Give it a name (e.g., “Repo Push Token”).
- Set expiration (90 days recommended for security).
- Select scopes: At minimum, check “repo” for full repository access.
- Generate and copy the token (it starts with
ghp_—you won’t see it again!). Paste this token as the “password” in the terminal prompt. Nothing will appear as you type/paste—that’s normal for security.
Pro Tip: For easier future pushes, set up Git Credential Manager (GCM). It often comes with Git and can handle logins via browser (supports 2FA). Check with git config --global credential.helper. If it’s set to “manager” or similar, the next push might prompt a browser login instead.
Step 5: Troubleshoot Common Issues
If things go wrong, here’s a quick fix table:
| Issue | Solution |
|---|---|
| “Authentication failed” | Double-check your PAT. If expired or wrong scopes, generate a new one. |
| Repo already has files (e.g., README) | Run git pull origin main --allow-unrelated-histories, resolve conflicts if any, then commit and push again. |
| “src refspec main does not match” | Ensure you ran git branch -M main. |
| No files appear on GitHub | Refresh the page, confirm you’re on the ‘main’ branch, or check git status. |
| Want password-less pushes? | Switch to SSH: Generate SSH keys (ssh-keygen), add the public key to GitHub settings, then change remote with git remote set-url origin git@github.com:YOUR-USERNAME/YOUR-REPO-NAME.git. |
Step 6: Verify and Next Steps
- Head to your GitHub repo in a browser and refresh. Your Python files should be there!
- For ongoing work: Use
git add .,git commit -m "Update", andgit pushto keep things synced. - Bonus: Add a README.md with project details, or set up a .gitignore for Python-specific ignores (e.g., via
git ignore pythontemplates online).
That’s it! You’ve successfully pushed your local Python project to GitHub. This process not only backs up your work but also opens doors to version control magic. If you run into snags, drop a comment below or check GitHub’s docs. Happy coding! 🚀
Published on January 19, 2026
Leave a Reply