Back to posts

Getting Started with Git: Version Control Made Easy

Master Git basics with this beginner-friendly guide. Learn essential commands, workflows, and best practices for version control in modern software development.

Fernanda
10 min read
GitVersion ControlTutorialGitHubDevelopment
Getting Started with Git: Version Control Made Easy
Share:

What is Git?

Git is a distributed version control system that tracks changes in your code over time. It allows multiple developers to work on the same project simultaneously without conflicts, and provides a complete history of every change made to your codebase.

Why Learn Git?

  • Version Control: Track every change in your project
  • Collaboration: Work with teams seamlessly
  • Backup: Never lose your work
  • Experimentation: Try new features without risk
  • Industry Standard: Used by 95%+ of developers worldwide

Installing Git

Windows

Option 1: Git for Windows

  1. Download from git-scm.com
  2. Run the installer
  3. Use default settings (recommended)

Option 2: Via Package Manager

# Using Chocolatey
choco install git

# Using Winget
winget install Git.Git

macOS

Option 1: Using Homebrew (Recommended)

brew install git

Option 2: Xcode Command Line Tools

xcode-select --install

Linux (Ubuntu/Debian)

sudo apt-get update
sudo apt-get install git

Verify Installation

git --version
# Output: git version 2.40.0

Initial Configuration

Before using Git, configure your identity:

# Set your name
git config --global user.name "Your Name"

# Set your email
git config --global user.email "your.email@example.com"

# Set default branch name to main
git config --global init.defaultBranch main

# View your configuration
git config --list

Basic Git Workflow

1. Initialize a Repository

# Create a new directory
mkdir my-project
cd my-project

# Initialize Git repository
git init

This creates a hidden .git folder that tracks all changes.

2. Check Repository Status

git status

Shows which files are:

  • Untracked: New files Git doesn't know about
  • Modified: Files that have been changed
  • Staged: Files ready to be committed

3. Add Files to Staging Area

# Add specific file
git add filename.txt

# Add all files
git add .

# Add all JavaScript files
git add *.js

4. Commit Changes

# Commit with message
git commit -m "Add initial project files"

# Commit with detailed message
git commit -m "Add user authentication" -m "Implemented login and signup features with JWT tokens"

Good Commit Messages:

  • ✅ "Fix login button alignment on mobile"
  • ✅ "Add user profile page"
  • ✅ "Refactor database connection logic"

Bad Commit Messages:

  • ❌ "Fix stuff"
  • ❌ "Update"
  • ❌ "asdfasdf"

5. View Commit History

# View commit history
git log

# Compact view
git log --oneline

# See last 5 commits
git log -5

# View with graph
git log --graph --oneline --all

Essential Git Commands

Working with Changes

# See what changed (not staged)
git diff

# See what will be committed (staged)
git diff --staged

# Undo changes to a file (not staged)
git checkout -- filename.txt

# Unstage a file
git reset HEAD filename.txt

# Amend last commit (before pushing)
git commit --amend

Branching

Branches let you work on features independently:

# Create new branch
git branch feature-login

# Switch to branch
git checkout feature-login

# Create and switch (shortcut)
git checkout -b feature-login

# List all branches
git branch

# Delete branch
git branch -d feature-login

# Force delete (unmerged changes)
git branch -D feature-login

Merging

# Merge feature-login into current branch
git merge feature-login

# Abort merge if conflicts
git merge --abort

Working with Remote Repositories

Connect to GitHub

# Add remote repository
git remote add origin https://github.com/username/repo.git

# Verify remote
git remote -v

# Change remote URL
git remote set-url origin https://github.com/username/new-repo.git

Push Changes

# Push to main branch (first time)
git push -u origin main

# Push subsequent changes
git push

# Push specific branch
git push origin feature-login

Pull Changes

# Pull latest changes
git pull

# Pull from specific branch
git pull origin main

# Fetch without merging
git fetch

Clone Repository

# Clone a repository
git clone https://github.com/username/repo.git

# Clone into specific directory
git clone https://github.com/username/repo.git my-folder

Common Git Workflows

Feature Branch Workflow

  1. Create branch for new feature
git checkout -b feature-user-profile
  1. Make changes and commit
git add .
git commit -m "Add user profile page"
  1. Push branch to remote
git push -u origin feature-user-profile
  1. Create Pull Request on GitHub

  2. After approval, merge and delete branch

git checkout main
git pull
git branch -d feature-user-profile

Fixing Mistakes

Undo Last Commit (Keep Changes)

git reset --soft HEAD~1

Undo Last Commit (Discard Changes)

git reset --hard HEAD~1

Revert a Commit (Safe for Shared Repos)

# Creates new commit that undoes changes
git revert abc123

Useful .gitignore Patterns

Create a .gitignore file to exclude files from Git:

# Node modules
node_modules/

# Environment variables
.env
.env.local

# Build outputs
dist/
build/
.next/

# IDE files
.vscode/
.idea/
*.swp

# OS files
.DS_Store
Thumbs.db

# Logs
*.log
logs/

# Dependencies
vendor/

Git Best Practices

1. Commit Often

Make small, focused commits rather than large, monolithic ones.

# Good: Small, focused commits
git commit -m "Add login form validation"
git commit -m "Style login button"
git commit -m "Add error messages"

# Bad: Everything at once
git commit -m "Add entire login feature"

2. Write Clear Commit Messages

Follow the conventional commits format:

feat: Add user authentication
fix: Resolve login redirect issue
docs: Update README with setup instructions
style: Format code with Prettier
refactor: Simplify database queries
test: Add unit tests for auth module

3. Pull Before Push

Always pull latest changes before pushing:

git pull
# Resolve any conflicts
git push

4. Use Branches

Never commit directly to main in team projects:

# Create feature branch
git checkout -b feature-name

# Work on feature
git add .
git commit -m "Your changes"

# Push and create PR
git push -u origin feature-name

5. Review Before Committing

# Check what will be committed
git status
git diff

# Then commit
git add .
git commit -m "Your message"

Handling Merge Conflicts

When Git can't automatically merge changes:

# Pull latest changes
git pull
# CONFLICT: Merge conflict in file.txt

Resolve Conflict:

  1. Open conflicted file:
<<<<<<< HEAD
const greeting = "Hello"
=======
const greeting = "Hi"
>>>>>>> feature-branch
  1. Choose which version to keep (or combine):
const greeting = "Hello"
  1. Stage and commit:
git add file.txt
git commit -m "Resolve merge conflict"

Git Cheat Sheet

# Configuration
git config --global user.name "Name"
git config --global user.email "email@example.com"

# Repository
git init                      # Initialize repo
git clone <url>              # Clone repo

# Changes
git status                    # Check status
git add <file>               # Stage file
git add .                    # Stage all
git commit -m "message"      # Commit changes

# Branching
git branch                    # List branches
git branch <name>            # Create branch
git checkout <name>          # Switch branch
git checkout -b <name>       # Create and switch

# Remote
git remote add origin <url>  # Add remote
git push -u origin main      # Push to remote
git pull                      # Pull changes
git fetch                     # Fetch (no merge)

# History
git log                       # View commits
git log --oneline            # Compact view
git diff                      # See changes

# Undo
git reset --soft HEAD~1      # Undo commit (keep changes)
git reset --hard HEAD~1      # Undo commit (discard changes)
git revert <commit>          # Revert commit (safe)

Next Steps

Now that you know Git basics:

  1. Create a GitHub account at github.com
  2. Practice with a project: Initialize a repo and make commits
  3. Learn GitHub: Pull requests, issues, and collaboration
  4. Explore advanced topics: Rebasing, cherry-picking, stashing
  5. Try Git GUIs: GitKraken, Sourcetree, or GitHub Desktop

Useful Resources


Conclusion

Git is an essential tool for modern software development. With these basics, you're ready to start tracking your projects, collaborating with others, and building your developer portfolio. Remember: the best way to learn Git is by using it daily!

Happy coding! 🚀


Have Git questions? Leave a comment and I'll help you out!