10 minworkshop

Create and Connect a Repository

Create and Connect a Repository

Practice initializing a Git repository and connecting it to GitHub. You'll use both the automated and manual approaches.

Exercise 1: Initialize a Local Repository

Create a new project directory and initialize Git:

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

Verify Initialization

bash
1$ ls -la
2total 0
3drwxr-xr-x 3 user staff 96 Nov 30 10:00 .
4drwxr-xr-x 10 user staff 320 Nov 30 10:00 ..
5drwxr-xr-x 9 user staff 288 Nov 30 10:00 .git
6
7$ git status
8On branch main
9
10No commits yet
11
12nothing to commit (create/copy files and use "git add" to track)

Exercise 2: Create a Seed File

Create a README so you have something to commit:

bash
1$ echo "# Git Workshop Practice" > README.md
2$ git add README.md
3$ git commit -m "docs: add initial README"
4[main (root-commit) 1a2b3c4] docs: add initial README
5 1 file changed, 1 insertion(+)
6 create mode 100644 README.md

Exercise 3A: Connect Using GitHub CLI

If you have gh installed and authenticated:

bash
1$ gh repo create your-username/git-workshop-practice --private --source=. --remote=origin --push
2✓ Created repository your-username/git-workshop-practice on GitHub
3✓ Added remote origin
4✓ Pushed commits to main

Verify

bash
1$ git remote -v
2origin git@github.com:your-username/git-workshop-practice.git (fetch)
3origin git@github.com:your-username/git-workshop-practice.git (push)
4
5$ git branch -vv
6* main 1a2b3c4 [origin/main] docs: add initial README

Skip to Exercise 4 if this worked.

Exercise 3B: Connect Manually

If you don't have gh, follow these steps:

Step 1: Create Repository on GitHub

  1. Go to github.com/new
  2. Name it git-workshop-practice
  3. Keep it private
  4. Don't initialize with README (you already have one)
  5. Click "Create repository"

Step 2: Add Remote

Copy the SSH URL from GitHub and add it:

bash
1$ git remote add origin git@github.com:your-username/git-workshop-practice.git

Step 3: Verify Remote

bash
1$ git remote -v
2origin git@github.com:your-username/git-workshop-practice.git (fetch)
3origin git@github.com:your-username/git-workshop-practice.git (push)

Step 4: Push and Set Upstream

bash
1$ git push -u origin main
2Enumerating objects: 3, done.
3Counting objects: 100% (3/3), done.
4Writing objects: 100% (3/3), 234 bytes | 234.00 KiB/s, done.
5Total 3 (delta 0), reused 0 (delta 0)
6To github.com:your-username/git-workshop-practice.git
7 * [new branch] main -> main
8Branch 'main' set up to track remote branch 'main' from 'origin'.

Exercise 4: Verify Everything

Run these commands to confirm your setup:

bash
1# Check remote
2$ git remote -v
3origin git@github.com:your-username/git-workshop-practice.git (fetch)
4origin git@github.com:your-username/git-workshop-practice.git (push)
5
6# Check tracking
7$ git branch -vv
8* main 1a2b3c4 [origin/main] docs: add initial README
9
10# Check status
11$ git status
12On branch main
13Your branch is up to date with 'origin/main'.
14
15nothing to commit, working tree clean

Exercise 5: View on GitHub

Open your browser and navigate to your repository:

https://github.com/your-username/git-workshop-practice

You should see:

  • Your README.md rendered
  • One commit in the history
  • The main branch

Cleanup (Optional)

If you want to delete this practice repository:

GitHub CLI:

bash
1$ gh repo delete your-username/git-workshop-practice --yes

Manual:

  1. Go to repository Settings on GitHub
  2. Scroll to "Danger Zone"
  3. Click "Delete this repository"

Local:

bash
1$ cd ..
2$ rm -rf git-workshop-practice

Checklist

Before moving on, verify:

  • git init created a .git directory
  • You committed at least one file
  • git remote -v shows origin pointing to GitHub
  • git branch -vv shows tracking relationship
  • You can view the repository on GitHub

Troubleshooting

"Repository not found"

The remote URL is wrong. Check spelling and your username:

bash
1$ git remote set-url origin git@github.com:correct-username/correct-repo.git

"Updates were rejected"

The remote has commits you don't have locally (e.g., GitHub added a README):

bash
1$ git pull --rebase origin main
2$ git push origin main

"Permission denied"

SSH key issue. Verify your key:

bash
1$ ssh -T git@github.com

Key Takeaway

You've initialized a repository and connected it to GitHub. This workflow—init, commit, connect remote, push—is the foundation for every new project.