Understanding: Git

What version control is, how it works, and why no modern project can do without it.

What is Git?

Created by Linus Torvalds in 2005

Git is a distributed version control system (VCS) created by Linus Torvalds in 2005 for Linux kernel development. It tracks changes in your code, allows you to revert to any previous version, and enables multiple developers to collaborate without conflicts.

Why You Need Git
Distributed

Every developer has a full copy of the repository. Work offline, sync when ready.

Fast

Almost all operations are local. Branching and merging are nearly instantaneous.

Reliable

Cryptographic integrity ensures your history is never corrupted or lost.

Key Concepts

The Building Blocks of Git

Before diving into commands, understand these fundamental concepts:

Repository

A folder containing your project and its entire history. The hidden .git folder stores all version data.

Commit

A snapshot of your project at a specific point in time. Each commit has a unique ID (hash) and a message describing the change.

Branch

An independent line of development. Create branches to work on features without affecting the main code.

Merge

Combining changes from one branch into another. Git automatically resolves most conflicts.

Remote

A version of your repository hosted on a server (GitHub, GitLab, etc.). Used to share and backup code.

Clone

Creating a local copy of a remote repository, including all files, branches, and history.

Essential Git Commands

The Commands You Will Use Daily

Master these commands to handle most Git workflows:

Getting Started
# Initialize a new repository
git init

# Clone an existing repository
git clone https://github.com/user/repo.git
Basic Workflow
# Check the status of your files
git status

# Add files to staging area
git add filename.js       # Add specific file
git add .                 # Add all changes

# Commit your changes
git commit -m "Add login feature"
Branching
# Create a new branch
git branch feature/login

# Switch to a branch
git checkout feature/login

# Create and switch in one command
git checkout -b feature/login

# Merge a branch into current branch
git merge feature/login
Working with Remotes
# Push changes to remote
git push origin main

# Pull changes from remote
git pull origin main

# Fetch changes without merging
git fetch origin

Practical Workflow

A Day in the Life with Git

Here is a typical workflow when contributing to a project on GitHub or GitLab:

  1. Pull - Get the latest changes from the remote
  2. Branch - Create a new branch for your feature
  3. Code - Make your changes
  4. Add - Stage your changes
  5. Commit - Save a snapshot with a descriptive message
  6. Push - Upload your branch to the remote
  7. Pull Request - Request to merge your changes into main
# Complete workflow example
git pull origin main
git checkout -b feature/user-profile
# ... make your changes ...
git add .
git commit -m "Add user profile page"
git push -u origin feature/user-profile
# Then create a Pull Request on GitHub/GitLab

Common Problems & Solutions

How to Fix Typical Git Issues

Merge Conflicts

When two branches modify the same lines, Git cannot automatically merge. Open the conflicting file, look for <<<<<<< markers, choose which changes to keep, then stage and commit.

# After resolving conflicts
git add .
git commit -m "Resolve merge conflicts"
Undo Last Commit

Made a mistake in your last commit? Use reset to undo it while keeping your changes.

# Undo commit, keep changes staged
git reset --soft HEAD~1

# Undo commit, unstage changes
git reset HEAD~1
Forgot to Add a File

Add the missing file and amend your last commit.

git add forgotten-file.js
git commit --amend --no-edit
Save Work for Later (Stash)

Need to switch branches but not ready to commit? Stash your changes.

# Save current changes
git stash

# Apply stashed changes later
git stash pop

Best Practices

Write Better Commits and Stay Organized

Commit Message Conventions
BadGood
fixFix login button not responding on mobile
updateUpdate user validation to require email format
WIPAdd password reset email template
Branch Naming Conventions
main / master

Production-ready code

feature/*

New features (e.g., feature/user-auth)

bugfix/*

Bug fixes (e.g., bugfix/login-crash)

Using .gitignore

Create a .gitignore file to exclude files that should not be tracked:

# Dependencies
node_modules/

# Build output
dist/
build/

# Environment files (contain secrets!)
.env
.env.local

# IDE settings
.vscode/
.idea/

# OS files
.DS_Store
Thumbs.db
Back to HomeView Workshops
What is Git | Academy by Potato Battery Games