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.
bash1$ mkdir my-project && cd my-project2$ git init3Initialized 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 branch3├── config # Repository-specific settings4├── objects/ # All file content and commits5├── refs/ # Branch and tag pointers6└── 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:
bash1$ gh repo create my-org/my-project --private --source=. --remote=origin --push2✓ Created repository my-org/my-project on GitHub3✓ Added remote origin4✓ Pushed commits to main
Flags explained:
--private- Create a private repository (use--publicfor 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:
bash1$ git remote add origin git@github.com:my-org/my-project.git
Step 3: Verify the remote:
bash1$ git remote -v2origin 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:
bash1$ git push -u origin main
The -u flag sets up tracking so future git push commands know where to go.
Remote Naming Conventions
| Remote Name | Typical Use |
|---|---|
origin | Your primary remote (usually your repo) |
upstream | The original repo you forked from |
backup | Secondary backup location |
You can have multiple remotes, but origin is the standard default.
Verifying Your Setup
After connecting, verify everything is configured:
bash1# Check remote URLs2$ git remote -v3origin git@github.com:my-org/my-project.git (fetch)4origin git@github.com:my-org/my-project.git (push)56# Check branch tracking7$ git branch -vv8* 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:
bash1# View current remote2$ git remote -v34# Remove it if needed5$ git remote remove origin67# Add the correct one8$ git remote add origin git@github.com:correct/path.git
"Permission denied (publickey)"
Your SSH key isn't recognized by GitHub:
bash1$ ssh -T git@github.com2Permission denied (publickey).34# Fix: Add your SSH key to GitHub5# Settings → SSH and GPG keys → New SSH key
Using HTTPS instead of SSH
If SSH doesn't work, you can use HTTPS:
bash1$ 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.