Git
remote repository
local repository
version control
command line

How to create a remote Git repository from a local one?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

A common workflow is building a project locally first, then publishing it to a remote Git host. The command sequence is short, but first-push failures often come from branch naming, authentication mismatch, or remote initialization choices. A repeatable setup process prevents these avoidable errors.

Prepare Local Repository Correctly

First verify your local repository state and make sure you have at least one commit.

bash
1cd /path/to/project
2git init
3git add .
4git commit -m "Initial commit"

If repository already exists, inspect status before publishing.

bash
git status
git branch --show-current
git log --oneline -n 3

This check prevents pushing from wrong branch or with unfinished changes.

Create an Empty Remote Repository

On GitHub, GitLab, or Bitbucket, create repository without README, license, or ignore file when local history already exists. Empty remote simplifies first push and avoids non-fast-forward conflicts.

Copy remote URL in your chosen protocol:

  • HTTPS URL style.
  • SSH URL style.

Keep protocol consistent with your credential setup.

Connect Local Repository to Remote

Add remote as origin and verify.

bash
git remote add origin [email protected]:user/repo.git
git remote -v

Then push your default branch and set upstream tracking.

bash
git push -u origin main

If your local branch is named differently, push that branch explicitly or rename locally to team convention.

Verify Tracking and Publish Additional Branches

After first push, confirm upstream linkage.

bash
git branch -vv

Publish feature branches deliberately:

bash
git switch feature/api-cleanup
git push -u origin feature/api-cleanup

Push tags only when intended:

bash
git push origin --tags

Avoid broad mirror pushes unless full ref replication is required.

Authentication Setup and Protocol Choice

Most first-push issues are auth problems.

For SSH verify access:

For HTTPS ensure personal access token or credential manager is configured. Mixing SSH URL with HTTPS-only credentials causes confusing failures.

Handling Remote With Existing Commits

If remote was initialized with files, your push may be rejected. Inspect divergence first:

bash
git fetch origin
git log --oneline --graph --decorate --all

Then choose a clear strategy:

  • Merge remote initial commit.
  • Rebase local history on remote main.
  • Replace remote history only with explicit team approval.

Do not force push to shared repositories casually during bootstrap.

GitHub CLI Shortcut Option

GitHub CLI can create remote and push in one step.

bash
gh repo create user/new-project --private --source=. --remote=origin --push

Even with automation, still verify branch protection and repository visibility settings after creation.

Post-Publish Hardening

Once remote is live, apply governance settings immediately:

  1. Branch protection for mainline branch.
  2. Required status checks.
  3. Access control and team permissions.
  4. Repository metadata and contribution guide.

These controls reduce accidental direct pushes and improve collaboration quality.

Troubleshooting Checklist

If first push fails:

  • Confirm current branch name.
  • Confirm remote URL points to correct repository.
  • Confirm auth method matches URL protocol.
  • Check whether remote has pre-existing commits.
  • Retry push with explicit remote and branch names.

Explicit diagnostics are faster than retrying random command variants.

Common Pitfalls

  • Creating remote with initial files and then hitting avoidable push conflicts.
  • Pushing from wrong local branch.
  • Forgetting -u on first push and losing tracking convenience.
  • Mixing SSH and HTTPS credentials unintentionally.
  • Force-pushing bootstrap history without team coordination.

Summary

  • Initialize and commit locally before remote publication.
  • Prefer empty remote repository for clean initial push.
  • Add origin, push explicitly, and set upstream tracking.
  • Validate protocol and credentials before troubleshooting deeper.
  • Apply branch protection and access rules immediately after first publish.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.