Saltar al contenido principal

Your First Git Repository

Module 00 30 min

Section Objectives

  • Create a Git repository from scratch with git init
  • Clone an existing repository with git clone
  • Understand the .git folder structure
  • Make your first commit
  • Understand what .gitignore is for

Creating a Repository: git init

There are 2 ways to start with a Git repository:

Method 1: git init — Starting from Scratch

# Create and navigate to a new folder
mkdir my-first-project
cd my-first-project

# Initialize the Git repository
git init
# Output: Initialized empty Git repository in .../my-first-project/.git/

# Verify the .git folder was created
ls -la
# You should see: .git/

What does git init do?

It creates a hidden .git folder containing everything Git needs:

.git/
├── HEAD ← Pointer to current branch (contains: ref: refs/heads/main)
├── config ← Repository-specific configuration
├── description ← Repository description (used by GitWeb)
├── hooks/ ← Scripts triggered automatically (pre-commit, post-commit, etc.)
├── info/
│ └── exclude ← Local .gitignore (not shared)
├── objects/ ← All Git objects (blobs, trees, commits) — starts empty
│ ├── info/
│ └── pack/
└── refs/ ← Branch and tag references — starts empty
├── heads/
└── tags/
Never manually edit the .git folder

The .git folder is Git's "brain." Manually editing files in it can corrupt your repository. Always use Git commands.

Method 2: git clone — Copy an Existing Repository

# Clone via HTTPS
git clone https://github.com/username/repository.git

# Clone via SSH (recommended after SSH setup)
git clone git@github.com:username/repository.git

# Clone into a specific folder name
git clone git@github.com:username/repository.git my-folder-name

# Clone only the last N commits (shallow clone — faster for large repos)
git clone --depth 1 git@github.com:username/repository.git

Your First Commit — Step by Step

Let's create a complete project from scratch:

Step 1: Create some files

# We're in my-first-project/
echo "# My First Project" > README.md
echo "print('Hello, Git!')" > main.py

Step 2: Check the repository status

git status

Output:

On branch main

No commits yet

Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
main.py

nothing added to commit but untracked files present (use "git add" to track)

Step 3: Add files to the staging area

# Add a specific file
git add README.md

# Add all files
git add .

# Add files by pattern
git add *.py
git status

Output:

On branch main

No commits yet

Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
new file: main.py

Step 4: Make the commit

git commit -m "Initial commit: add README and main.py"

Output:

[main (root-commit) a3f4b2c] Initial commit: add README and main.py
2 files changed, 2 insertions(+)
create mode 100644 README.md
create mode 100644 main.py

Step 5: View the history

git log

Output:

commit a3f4b2c1d2e3f4a5b6c7d8e9f0a1b2c3d4e5f6a7 (HEAD -> main)
Author: Alice Dupont <alice@example.com>
Date: Wed Mar 18 14:30:00 2026 +0000

Initial commit: add README and main.py

The .gitignore File

The .gitignore file tells Git which files or folders to never track:

Common Things to Ignore

# ============================
# Operating System Files
# ============================
.DS_Store # macOS
Thumbs.db # Windows
desktop.ini # Windows

# ============================
# Editor Files
# ============================
.vscode/
.idea/
*.swp # Vim temp files
*.swo

# ============================
# Dependencies
# ============================
node_modules/ # JavaScript
__pycache__/ # Python
*.pyc # Python compiled files
venv/ # Python virtual env
.env # Environment variables (SECRETS!)

# ============================
# Build Outputs
# ============================
dist/
build/
*.egg-info/
target/ # Java/Maven

# ============================
# Logs
# ============================
*.log
logs/

# ============================
# Environment Variables (IMPORTANT!)
# ============================
.env
.env.local
.env.*.local
*.env
Never commit .env files!

Files containing passwords, API keys, and tokens must never be committed to Git. Once in the history, they're very difficult to remove completely, and if pushed to GitHub, they could be compromised within seconds by automated bots.

Useful .gitignore Resources

# GitHub provides ready-to-use templates:
# https://github.com/github/gitignore

# Create a .gitignore for multiple technologies:
# https://gitignore.io

Check if a File is Ignored

# Check if a file is ignored (and why)
git check-ignore -v .env
# Output: .gitignore:12:.env .env

# See all ignored files
git status --ignored

Complete Workflow Visualization


Summary of Key Commands

CommandDescription
git initInitialize a new repository
git clone <url>Clone an existing repository
git statusShow the state of files
git add <file>Add a file to the staging area
git add .Add all modified files
git commit -m "message"Create a commit
git logShow commit history
git log --onelineCompact history

Next Steps