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:
bash1$ mkdir git-workshop-practice && cd git-workshop-practice2$ git init3Initialized empty Git repository in .../git-workshop-practice/.git/
Verify Initialization
bash1$ ls -la2total 03drwxr-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 .git67$ git status8On branch main910No commits yet1112nothing 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:
bash1$ echo "# Git Workshop Practice" > README.md2$ git add README.md3$ git commit -m "docs: add initial README"4[main (root-commit) 1a2b3c4] docs: add initial README5 1 file changed, 1 insertion(+)6 create mode 100644 README.md
Exercise 3A: Connect Using GitHub CLI
If you have gh installed and authenticated:
bash1$ gh repo create your-username/git-workshop-practice --private --source=. --remote=origin --push2✓ Created repository your-username/git-workshop-practice on GitHub3✓ Added remote origin4✓ Pushed commits to main
Verify
bash1$ git remote -v2origin git@github.com:your-username/git-workshop-practice.git (fetch)3origin git@github.com:your-username/git-workshop-practice.git (push)45$ git branch -vv6* 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
- Go to github.com/new
- Name it
git-workshop-practice - Keep it private
- Don't initialize with README (you already have one)
- Click "Create repository"
Step 2: Add Remote
Copy the SSH URL from GitHub and add it:
bash1$ git remote add origin git@github.com:your-username/git-workshop-practice.git
Step 3: Verify Remote
bash1$ git remote -v2origin 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
bash1$ git push -u origin main2Enumerating 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.git7 * [new branch] main -> main8Branch 'main' set up to track remote branch 'main' from 'origin'.
Exercise 4: Verify Everything
Run these commands to confirm your setup:
bash1# Check remote2$ git remote -v3origin git@github.com:your-username/git-workshop-practice.git (fetch)4origin git@github.com:your-username/git-workshop-practice.git (push)56# Check tracking7$ git branch -vv8* main 1a2b3c4 [origin/main] docs: add initial README910# Check status11$ git status12On branch main13Your branch is up to date with 'origin/main'.1415nothing 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:
bash1$ gh repo delete your-username/git-workshop-practice --yes
Manual:
- Go to repository Settings on GitHub
- Scroll to "Danger Zone"
- Click "Delete this repository"
Local:
bash1$ cd ..2$ rm -rf git-workshop-practice
Checklist
Before moving on, verify:
-
git initcreated a .git directory - You committed at least one file
-
git remote -vshows origin pointing to GitHub -
git branch -vvshows tracking relationship - You can view the repository on GitHub
Troubleshooting
"Repository not found"
The remote URL is wrong. Check spelling and your username:
bash1$ 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):
bash1$ git pull --rebase origin main2$ git push origin main
"Permission denied"
SSH key issue. Verify your key:
bash1$ 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.