Introduction to Git and First Commits

Introduction to Git and First Commits

Why Git?

Git is the most widely used version control system in the world. Created by Linus Torvalds in 2005 (the creator of Linux), it allows you to:

  • Track every change to the source code
  • Collaborate efficiently with multiple people on the same project
  • Roll back at any point in time
  • Work in parallel on different features

Git vs the rest

Aspect Without Git With Git
History project-v1.zip, project-v2-final.zip, project-v2-FINAL-FINAL.zip Every change is a dated and described commit
Collaboration Sending files by email Everyone works on their own branch, then merges
Mistake Impossible to go back git revert or git checkout
Traceability Who did what? No idea git blame, git log

Installation and configuration

# Install Git
# Ubuntu/Debian
sudo apt install git

# macOS
brew install git

# Verify the installation
git --version

Initial configuration

# Identity (required)
git config --global user.name "Sacha Martin"
git config --global user.email "sacha@example.com"

# Default editor
git config --global core.editor "code --wait"    # VS Code

# Default branch
git config --global init.defaultBranch main

# View configuration
git config --list

Fundamental concepts

The three areas of Git

Working Directory  →  Staging Area  →  Local Repository
  (Working Directory)   (Index / Stage)  (Repository)

   Modified files       git add          git commit
   that you edit        ────────►        ────────────►

                        Ready to be      Saved
                        committed        in the history

Creating a repository

# Initialize a new repository
mkdir my-project
cd my-project
git init

# Or clone an existing repository
git clone https://github.com/user/projet.git
git clone git@github.com:user/projet.git     # Via SSH

First commits

The basic workflow

# 1. Check the repository status
git status

# 2. Add files to staging
git add file.txt                 # A specific file
git add src/                     # An entire directory
git add .                        # All modified files

# 3. Create a commit
git commit -m "Add the home page"

# 4. View the history
git log
git log --oneline                # Compact version
git log --oneline --graph        # With the branch graph

Hands-on example

# Create a project
mkdir website && cd website
git init

# Create the first file
echo "<!DOCTYPE html><html><body><h1>My website</h1></body></html>" > index.html

# First commit
git add index.html
git commit -m "Create the home page"

# Modify the file
echo "<p>Welcome to my website!</p>" >> index.html

# View the changes
git diff                         # Unstaged differences
git diff --staged                # Staged differences

# Second commit
git add index.html
git commit -m "Add a welcome paragraph"

Viewing the history in detail

# Full history
git log

# Compact history
git log --oneline

# History with changes
git log -p

# Last 5 commits
git log -5

# History of a specific file
git log -- src/app.js

# Who modified each line?
git blame src/app.js

# View a specific commit
git show abc1234

The .gitignore file

The .gitignore lists files that Git should ignore:

# Dependencies
node_modules/
vendor/
venv/

# Build
dist/
build/
*.o
*.class

# Environment
.env
.env.local
*.env

# IDE
.vscode/
.idea/
*.swp

# OS
.DS_Store
Thumbs.db

# Logs
*.log
logs/
# Create the .gitignore BEFORE the first commit
echo "node_modules/" > .gitignore
echo ".env" >> .gitignore
git add .gitignore
git commit -m "Add .gitignore"

Undoing and correcting

Undoing changes

# Undo changes to a file (unstaged)
git checkout -- file.txt
# Or (Git 2.23+)
git restore file.txt

# Remove a file from staging (without losing changes)
git reset HEAD file.txt
# Or (Git 2.23+)
git restore --staged file.txt

# Amend the last commit (message or forgotten files)
git add forgotten-file.txt
git commit --amend -m "New commit message"

Going back in time

# Create a new commit that undoes a previous commit
git revert abc1234

# Go back to a previous state (warning: subsequent commits are lost)
git reset --soft HEAD~1          # Keeps changes in staging
git reset --mixed HEAD~1         # Keeps changes unstaged
git reset --hard HEAD~1          # Deletes everything (dangerous!)

Stash — temporarily set aside changes

# Save current changes
git stash
git stash save "WIP: contact form"

# List stashes
git stash list

# Retrieve the latest stash
git stash pop                    # Apply and remove
git stash apply                  # Apply without removing

# Delete a stash
git stash drop stash@{0}

Best practices for commits

  1. One commit = one logical change — don't mix different concerns
  2. Clear and descriptive messages: prefer "Fix the VAT price calculation" over "fix bug"
  3. Commit often — small commits are easier to understand and revert
  4. Never commit secrets (.env, API keys, passwords)
  5. Use the imperative mood in messages: "Add" rather than "Added"
  6. Check with git status and git diff before each commit