10 minlesson

Initialize and Connect a Repository

Initialize and Connect a Repository

Every Git project starts with initialization. This lesson covers creating a local repository and connecting it to GitHub for remote collaboration.

Local Initialization

The git init command creates a new Git repository in the current directory.

bash
1$ mkdir my-project && cd my-project
2$ git init
3Initialized empty Git repository in .../my-project/.git/

This creates a hidden .git folder containing all repository metadata. Your project folder is now a Git repository.

What's Inside .git?

1.git/
2├── HEAD # Points to current branch
3├── config # Repository-specific settings
4├── objects/ # All file content and commits
5├── refs/ # Branch and tag pointers
6└── hooks/ # Scripts triggered by Git events

You rarely edit these directly, but understanding they exist helps debug issues.

Connecting to GitHub

A local repository needs a remote to collaborate with others or back up your work. GitHub is the most common remote host.

Option 1: GitHub CLI (Recommended)

The gh command creates a GitHub repo and wires up the remote in one step:

bash
1$ gh repo create my-org/my-project --private --source=. --remote=origin --push
2✓ Created repository my-org/my-project on GitHub
3✓ Added remote origin
4✓ Pushed commits to main

Flags explained:

  • --private - Create a private repository (use --public for open source)
  • --source=. - Use the current directory as the source
  • --remote=origin - Name the remote "origin" (convention)
  • --push - Push existing commits immediately

Option 2: Manual Setup

If you don't have gh or prefer manual control:

Step 1: Create the repository on GitHub (via web interface)

Step 2: Add the remote locally:

bash
1$ git remote add origin git@github.com:my-org/my-project.git

Step 3: Verify the remote:

bash
1$ git remote -v
2origin git@github.com:my-org/my-project.git (fetch)
3origin git@github.com:my-org/my-project.git (push)

Step 4: Push your first commit:

bash
1$ git push -u origin main

The -u flag sets up tracking so future git push commands know where to go.

Remote Naming Conventions

Remote NameTypical Use
originYour primary remote (usually your repo)
upstreamThe original repo you forked from
backupSecondary backup location

You can have multiple remotes, but origin is the standard default.

Verifying Your Setup

After connecting, verify everything is configured:

bash
1# Check remote URLs
2$ git remote -v
3origin git@github.com:my-org/my-project.git (fetch)
4origin git@github.com:my-org/my-project.git (push)
5
6# Check branch tracking
7$ git branch -vv
8* main 1a2b3c4 [origin/main] Initial commit

The [origin/main] indicates your local main branch tracks the remote.

Common Issues

"Remote origin already exists"

You tried to add a remote that's already configured:

bash
1# View current remote
2$ git remote -v
3
4# Remove it if needed
5$ git remote remove origin
6
7# Add the correct one
8$ git remote add origin git@github.com:correct/path.git

"Permission denied (publickey)"

Your SSH key isn't recognized by GitHub:

bash
1$ ssh -T git@github.com
2Permission denied (publickey).
3
4# Fix: Add your SSH key to GitHub
5# Settings → SSH and GPG keys → New SSH key

Using HTTPS instead of SSH

If SSH doesn't work, you can use HTTPS:

bash
1$ git remote add origin https://github.com/my-org/my-project.git

You'll need to enter credentials or use a credential helper.

Key Takeaway

git init creates a local repository. git remote add connects it to GitHub. The gh CLI combines these steps but isn't required. Always verify your remote configuration with git remote -v before pushing.