Skip
Arish's avatar

10. Exercise - init Command


Exercise: Using the init Command

Let's practice initializing Git repositories with hands-on exercises.

Exercise 1: Create Your First Repository

Task

Create a new Git repository for a personal website project.

Steps

bash
1# Step 1: Create the project folder
2mkdir my-website
3cd my-website
4
5# Step 2: Initialize Git
6git init
7
8# Step 3: Verify the repository was created
9ls -la
10
11# You should see the .git folder

Expected Output

Initialized empty Git repository in /path/to/my-website/.git/

Verify

bash
1# Check that .git exists
2ls -la .git
3
4# Check Git status
5git status

Exercise 2: Create Project Structure

Task

Create a basic website structure and make your first commit.

Steps

bash
1# Step 1: Create files
2echo "<!DOCTYPE html><html><body><h1>Hello World</h1></body></html>" > index.html
3echo "body { font-family: Arial; }" > style.css
4echo "# My Website" > README.md
5
6# Step 2: Check status
7git status
8
9# Step 3: Stage all files
10git add .
11
12# Step 4: Commit
13git commit -m "Initial commit: Add basic website structure"

Expected Output

[main (root-commit) abc1234] Initial commit: Add basic website structure 3 files changed, 3 insertions(+) create mode 100644 README.md create mode 100644 index.html create mode 100644 style.css

Exercise 3: Create a .gitignore

Task

Add a .gitignore file to exclude unnecessary files.

Steps

bash
1# Step 1: Create .gitignore
2cat > .gitignore << EOF
3# OS files
4.DS_Store
5Thumbs.db
6
7# Editor files
8.vscode/
9.idea/
10
11# Dependencies
12node_modules/
13
14# Environment files
15.env
16.env.local
17EOF
18
19# Step 2: Check status
20git status
21
22# Step 3: Stage and commit
23git add .gitignore
24git commit -m "Add .gitignore file"

Exercise 4: View Repository Info

Task

Explore the repository you created.

Steps

bash
1# View commit history
2git log
3
4# View commit history (compact)
5git log --oneline
6
7# View current branch
8git branch
9
10# View Git configuration
11git config --list --local

Expected Output

# git log --oneline abc1234 Add .gitignore file def5678 Initial commit: Add basic website structure # git branch * main

Exercise 5: Initialize with Custom Branch

Task

Create a new repository with a custom default branch name.

Steps

bash
1# Create new project
2mkdir new-project
3cd new-project
4
5# Initialize with 'main' branch
6git init -b main
7
8# Verify branch name
9git branch
10
11# Should output: * main

Challenge Exercise

Task

Create a complete project structure for a Node.js application.

Requirements

  1. Create folder structure:

    • src/ for source code
    • tests/ for test files
    • docs/ for documentation
  2. Create initial files:

    • package.json
    • README.md
    • .gitignore
    • src/index.js
  3. Initialize Git and make first commit

Solution

bash
1# Create project
2mkdir node-app
3cd node-app
4
5# Initialize Git
6git init
7
8# Create directories
9mkdir src tests docs
10
11# Create package.json
12cat > package.json << EOF
13{
14  "name": "node-app",
15  "version": "1.0.0",
16  "main": "src/index.js"
17}
18EOF
19
20# Create README
21echo "# Node App" > README.md
22
23# Create .gitignore
24cat > .gitignore << EOF
25node_modules/
26.env
27EOF
28
29# Create main file
30echo "console.log('Hello World');" > src/index.js
31
32# Keep empty directories
33touch tests/.gitkeep
34touch docs/.gitkeep
35
36# Stage all files
37git add .
38
39# Commit
40git commit -m "Initial commit: Set up Node.js project structure"
41
42# Verify
43git log --oneline
44git status

Self-Check Questions

  1. What command initializes a Git repository?
  2. What folder does Git create when initialized?
  3. Can you run git init in an already initialized repository?
  4. How do you initialize with a specific branch name?

Summary

You've practiced:

  • ✅ Initializing new repositories
  • ✅ Creating project structures
  • ✅ Making initial commits
  • ✅ Using .gitignore
  • ✅ Viewing repository information

Great job! You're ready to learn about SSH keys next.