How to Connect a New CodeBase with a Private GitHub Repository

When you create a new project, one of the first steps is to push it to GitHub for version control and collaboration. But sometimes, you run into a common conflict if your GitHub repo was initialized with a README.md.

Here’s a step-by-step guide to connect your project with a private GitHub repo and handle conflicts like a pro.

Step 1: Initialize Git in Your Project

Open the terminal in your project folder:

cd YourProjectName
git init

This creates a local Git repository.

Step 2: Create a Private Repo on GitHub

  1. Go to GitHub → click New Repository.
  2. Make it Private.
  3. If you checked “Initialize this repository with a README”, remember — this can cause conflicts later (don’t worry, we’ll fix it).
  4. Copy the repo URL (HTTPS or SSH).

Example:

  • HTTPS → https://github.com/username/repo-name.git
  • SSH → git@github.com:username/repo-name.git

Step 3: Connect Local Repo to GitHub

Inside your project run the below command with your repo link:

git remote add origin https://github.com/username/repo-name.git

Step 4: Add and Commit Code

Now run the below command

git add .
git commit -m “Initial commit – React Native app”

Step 5: Handle README Conflict

Now, if your GitHub repo already has a README (and your local doesn’t), pushing directly will throw an error like:

! [rejected] main -> main (fetch first)

✅ Solution: Pull and Merge First

git pull origin main --allow-unrelated-histories

This pulls the remote README into your local repo and merges it. If prompted with a merge message, just save and exit.

Then push again:

git push origin main

Step 6: Verify on GitHub

Now both your app code and the README will exist in the repo.

Refresh your private repo — you should now see both:

  • Your code
  • The README.md from GitHub

🎉 You’re done!

Tripti

Leave a Reply

Your email address will not be published. Required fields are marked *