To push a local folder to GitHub for the first time using SSH, you create a new repository on GitHub, connect your local folder to it with git init, git add, git commit, and git remote add, then push with git push -u origin main. This guide walks you through each step, including setting up an SSH key so you never need to enter a username or password again.
Why Use SSH Instead of HTTPS?
GitHub offers two main ways to authenticate when pushing code: HTTPS (with a personal access token) and SSH (with a key pair). SSH is preferred by many developers because:
- You generate a key once, and Git uses it automatically for every push and pull.
- No need to re-enter credentials or manage token expiry.
- More secure than password-based authentication, as the private key never leaves your machine.
HTTPS works fine, but it requires you to create a token in GitHub settings and paste it as your password. SSH is a one-time setup that pays off in convenience.
Prerequisites
Before you start, make sure you have:
- Git installed on your computer. Check with
git --version(if not, download from git-scm.com). - A GitHub account (free tier is fine).
- A local folder containing the project files you want to push.
- Terminal access (Command Prompt, PowerShell, or Terminal on Mac/Linux).
Step 1: Generate an SSH Key
Open your terminal and run the following command, replacing the email with the one you use for GitHub:
ssh-keygen -t ed25519 -C "your_email@example.com"
If your system doesn’t support ed25519 (older systems), use:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
You’ll be asked where to save the key. Press Enter to accept the default location (~/.ssh/id_ed25519 or ~/.ssh/id_rsa). Then you’ll be prompted for a passphrase—you can leave it empty, but adding one is more secure. The command creates two files: a private key and a public key with a .pub extension.
Step 2: Add the Public Key to GitHub
Now you need to copy the public key to GitHub. In your terminal, run:
cat ~/.ssh/id_ed25519.pub
This prints the key. Copy it entirely (it starts with ssh-ed25519 or ssh-rsa). Then:
- Log in to GitHub and go to Settings (click your profile picture, then Settings).
- In the left sidebar, click SSH and GPG keys.
- Click New SSH key.
- Give it a descriptive title (e.g., “My Laptop”).
- Paste the public key into the “Key” field.
- Click Add SSH key.
Now test the connection:
ssh -T git@github.com
If you see a message like “Hi username! You’ve successfully authenticated, but GitHub does not provide shell access,” everything is working.
Step 3: Create a New Repository on GitHub
In your browser, go to github.com and click the + icon in the top-right corner, then select New repository. Give it a name (it doesn’t have to match your local folder name, but it helps). Do not check any boxes like “Add a README” or “.gitignore”—you want an empty repository. Click Create repository.
You’ll see a page with instructions. Look for the section that says “…or push an existing repository from the command line”. It will show commands like:
git remote add origin git@github.com:username/repo-name.git
git branch -M main
git push -u origin main
Keep this page open—you’ll need the remote URL.
Step 4: Initialize Git in Your Local Folder
Open a terminal and navigate to your local project folder:
cd /path/to/your/folder
Now initialize a Git repository:
git init
This creates a hidden .git folder that tracks changes. Next, add all your files to the staging area:
git add .
The dot means “everything in this folder.” If you want to exclude certain files (like build artifacts or API keys), create a .gitignore file first. Now commit your files:
git commit -m "Initial commit"
If Git asks you to identify yourself, run these two commands first (replace with your GitHub username and email):
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
Step 5: Connect and Push
Now link your local repository to the GitHub one. Use the SSH URL from the page you kept open:
git remote add origin git@github.com:username/repo-name.git
Verify the remote is set correctly:
git remote -v
You should see the URL twice (fetch and push). Now push your code:
git push -u origin main
The -u flag sets the upstream, so future pushes can just be git push. If your default branch is called master instead of main, either rename it with git branch -m main before pushing, or push to master and then change the default branch in GitHub settings.
Refresh your GitHub page—your files should now be there.
Common Problems and Fixes
- Permission denied (publickey): Your SSH key isn’t added to GitHub, or the ssh-agent isn’t running. Run
eval "$(ssh-agent -s)"and thenssh-add ~/.ssh/id_ed25519. - Repository not found: The remote URL is wrong, or you don’t have access to that repo. Double-check the username and repo name in the URL.
- Failed to push some refs: The remote has commits you don’t have locally (e.g., you added a README on GitHub). Pull first with
git pull origin main --rebase, then push again. - File too large: GitHub blocks files over 100 MB. Use Git LFS or remove the file from the commit.
Frequently Asked Questions
Do I need to set up SSH every time I create a new repo?
No. Once your SSH key is added to GitHub, you can use it for any repository. For a new local folder, you just repeat steps 4 and 5 (init, add, commit, remote add, push).
Can I push a folder that already has a .git folder?
Yes, but if it’s already connected to another remote, you’ll need to change the remote URL with git remote set-url origin git@github.com:username/repo-name.git.
What if I accidentally push sensitive files like .env?
You can remove them from the repository with git rm --cached .env, then commit. However, the file remains in Git history. You’d need to rewrite history (e.g., with git filter-branch) or use a tool like BFG Repo-Cleaner, and then force-push. It’s easier to add a .gitignore before your first commit.
What’s the difference between git push and git push -u?
The -u (or --set-upstream) sets the remote branch as the default for the current local branch. After that, you can just type git push without specifying the remote or branch.
How do I switch an existing HTTPS repo to SSH?
Run git remote set-url origin git@github.com:username/repo-name.git. You can check the current URL with git remote -v.
Why does GitHub say my branch is ‘master’ but I want ‘main’?
Git’s default branch name depends on your configuration. You can rename it with git branch -m master main before pushing, or set git config --global init.defaultBranch main to change the default for future repositories.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Increase Internal Memory Xiaomi Phones: How-To
- Nvidia Display Settings Are Not Available: Fixes
- Gmod Keeps Crashing? Fixes That Actually Work
- Cloudflare cf CLI: How Coding Agents Discover & Run Commands
- Protect AI from Model Extraction & Distillation
- Net Code Analysis Workflow: A Reliable Setup Guide