ThisCalculator

Everyday

Send Local Project to GitHub with Git Bash and SSH

Updated · Standard formula, shown below

This guide walks you through pushing a local project to a new GitHub repository using Git Bash and SSH. By the end, you’ll have your code hosted on GitHub with a secure, password-less connection set up for future pushes.

What You’ll Need

  • Git installed on your computer (download from git-scm.com if not already installed).
  • A GitHub account (free tier works).
  • Git Bash (comes with Git for Windows; on macOS/Linux, use the terminal).
  • Your local project folder with files you want to push.

Step-by-Step

  1. Open Git Bash in your project folder. Right-click inside the folder (Windows) and select “Git Bash Here”, or navigate using cd in an already-open terminal.
  2. Initialize a local repository (if you haven’t already). Run git init. This creates a hidden .git folder that tracks your project.
  3. Check your Git username and email (they must match your GitHub account). Run git config user.name and git config user.email. If they’re not set, configure them globally:
    git config --global user.name "Your Name"
    git config --global user.email "your-email@example.com"
  4. Create a new repository on GitHub: Go to github.com, click the “+” icon (top right), then “New repository”. Give it a name (e.g., my-project), choose Public or Private, and do not check “Add a README”, “.gitignore”, or “license”—you’ll push an existing project. Click “Create repository”.
  5. Generate an SSH key (if you don’t have one). In Git Bash, run:
    ssh-keygen -t ed25519 -C "your-email@example.com"
    Press Enter to accept the default file location, then set a passphrase (optional but recommended). This creates a key pair: id_ed25519 (private) and id_ed25519.pub (public) in ~/.ssh/.
  6. Add the SSH key to your SSH agent (so you don’t have to enter the passphrase every time). Run:
    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_ed25519
    Enter your passphrase if prompted.
  7. Add the public key to GitHub: Copy the public key to your clipboard with clip < ~/.ssh/id_ed25519.pub (Windows) or cat ~/.ssh/id_ed25519.pub (then manually copy). On GitHub, go to Settings → SSH and GPG keys → New SSH key. Paste the key, give it a descriptive title (e.g., “My Laptop”), and click “Add SSH key”.
  8. Test the SSH connection: Run ssh -T git@github.com. You’ll see a warning about authenticity—type yes. If successful, you’ll see “Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.”
  9. Add your files to the local staging area: Run git add . (adds all files in the current folder). To add specific files, use git add filename.
  10. Commit the files: Run git commit -m "Initial commit". This creates a snapshot of your project.
  11. Link your local repo to the GitHub remote: Copy the SSH URL from your GitHub repo page (it looks like git@github.com:username/repo.git). Then run:
    git remote add origin git@github.com:username/repo.git
    To verify, run git remote -v—you should see the URL.
  12. Push to GitHub: Run git push -u origin main. If your default branch is master (older Git versions), use git push -u origin master. The -u sets the upstream, so future pushes can just be git push.
  13. Verify: Refresh your GitHub repo page—your files should now be there. From now on, after making changes, use git add ., git commit -m "message", and git push to update.

Common Mistakes

  • Using HTTPS instead of SSH: If you copied the HTTPS URL, you’ll be prompted for username/password each time. Make sure you use the SSH URL (starts with git@).
  • Wrong branch name: GitHub’s default branch is main, but older Git versions create master. Check with git branch and push accordingly.
  • Committing before adding: Forgetting git add means your commit has no changes. Always stage first.
  • Pushing to an existing repo with different history: If the GitHub repo already has a README or other files, your push will be rejected. Either create the repo empty (recommended) or pull first with git pull origin main --allow-unrelated-histories.
  • Ignoring .gitignore: If you have large files, secrets, or build artifacts, you should add a .gitignore file before committing. Otherwise, you’ll push unwanted files.

Tips

  • Use a passphrase for your SSH key and add it to the agent—this keeps your key secure without prompting every time.
  • Set your default branch name to main with git config --global init.defaultBranch main to avoid confusion.
  • Write meaningful commit messages (e.g., “Fix login bug”) to make history easier to read.
  • Add a README.md to explain what your project does—it shows up on the repo page.
  • Use git status frequently to see what’s staged, modified, or untracked.

Troubleshooting

  • Permission denied (publickey): Your SSH key isn’t added to GitHub or the agent. Re-check steps 5-7. Run ssh -T git@github.com to see the error. Ensure the key is added to your GitHub account and that you’re using the correct key file.
  • Push rejected (non-fast-forward): The remote has commits you don’t have locally. Run git pull origin main first, then push. If you intentionally want to overwrite, use git push -f (but be careful—it rewrites history).
  • Could not resolve host: github.com: Check your internet connection or proxy settings. If you’re behind a firewall, you may need to use HTTPS instead of SSH.
  • Git asks for username/password: You’re using HTTPS, not SSH. Change the remote URL with git remote set-url origin git@github.com:username/repo.git.
  • ssh: connect to host github.com port 22: Connection refused: Sometimes port 22 is blocked. Try SSH over port 443 by adding to ~/.ssh/config:
    Host github.com
    Hostname ssh.github.com
    Port 443
  • Nothing seems to happen after git push: Check if you’re on the correct branch and that the remote is set. Run git status and git remote -v.

Frequently Asked Questions

Q: Do I need to generate an SSH key every time I push?
A: No. You generate one key pair and add the public key to GitHub. That key is used for all future pushes from that machine.

Q: What’s the difference between SSH and HTTPS for GitHub?
A: SSH uses a key pair for authentication (no password prompts), while HTTPS uses a personal access token or password. SSH is generally more convenient for frequent pushes.

Q: Can I push an existing project that already has a .git folder?
A: Yes. Just add the remote and push. If the remote has different history, you may need to pull first or force push (with caution).

Q: What if I accidentally push sensitive data like passwords?
A: Remove the file from the repo and add it to .gitignore. However, the data remains in Git history—you’ll need to rewrite history (e.g., using filter-branch or BFG) and force push. It’s best to rotate any exposed credentials.

Q: Why is my push so slow?
A: Large files or a slow connection. Consider using Git LFS for large binaries, and ensure you’re not committing unnecessary files (like node_modules).

Q: How do I change the remote URL if I made a typo?
A: Run git remote set-url origin git@github.com:username/repo.git with the correct URL.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.