Skip to main content
Development Tools 8 mins read Devs2.org

Git Basics for Beginners - Version Control Made Simple

Learn Git fundamentals - the version control system used by developers worldwide. Discover how to track changes, collaborate with others, and manage your code projects effectively. Perfect guide for coding beginners.

#Git #Version Control #Development #Programming #Tools

If you’ve spent any time around developers or coding communities, you’ve probably heard about Git. It’s mentioned in job descriptions, required in coding bootcamps, and used by virtually every professional developer. But what exactly is Git, and why does everyone seem to think it’s so important?

Git is a version control system—think of it as a time machine for your code. It lets you save snapshots of your project at different points, go back to previous versions if something breaks, and collaborate with others without stepping on each other’s toes. Whether you’re working on a solo project or part of a team, Git is an essential tool that will make your coding life much easier.

What is Git?

Git is a distributed version control system created by Linus Torvalds (the same person who created Linux) in 2005. It was designed to handle everything from small personal projects to massive projects with thousands of developers contributing code simultaneously.

At its core, Git tracks changes to files in your project. Every time you make a “commit” (save a snapshot), Git records what changed, when it changed, and who made the change. This creates a complete history of your project that you can navigate, review, and restore from at any time.

Think of Git like a sophisticated “Save As” feature. Instead of manually creating copies like “project_v1.html”, “project_v2.html”, “project_final.html”, and “project_final_really.html”, Git manages all these versions for you automatically.

Why Use Git?

1. Track Changes Over Time

Have you ever made changes to code, then realized you need to go back to how it was before? With Git, you can see exactly what changed and when, and easily revert to any previous version.

2. Collaborate Without Conflicts

When multiple people work on the same project, Git helps merge their changes together intelligently. It tracks who changed what and helps resolve conflicts when two people edit the same part of a file.

3. Experiment Safely

Want to try a new feature but worried about breaking your working code? Git lets you create “branches” to experiment in isolation. If it doesn’t work out, you can simply delete the branch. If it works, you can merge it back into your main code.

4. Backup Your Work

Every Git repository is a complete backup of your project history. Even if your computer crashes, you can recover everything from your Git repository.

5. Professional Standard

Git is used by virtually every software company and open-source project. Learning Git is essential for working in tech, contributing to open source, or collaborating on coding projects.

Installing Git

Before you can use Git, you need to install it on your computer.

Windows

  1. Download Git from git-scm.com
  2. Run the installer
  3. Use default settings (they’re fine for beginners)
  4. Git Bash will be installed, which gives you a command-line interface

macOS

Git might already be installed. Check by opening Terminal and typing:

git --version

If it’s not installed, you’ll be prompted to install Xcode Command Line Tools, or you can install Git using Homebrew:

brew install git

Linux

Install using your package manager:

# Ubuntu/Debian
sudo apt-get install git

# Fedora
sudo dnf install git

Your First Git Repository

Let’s create your first Git repository. This is simpler than you might think.

Step 1: Create a Project Folder

Create a new folder for your project. For this example, let’s call it “my-first-project”.

Step 2: Initialize Git

Open your terminal (or Git Bash on Windows), navigate to your project folder, and type:

git init

This command initializes a new Git repository in your current folder. You’ll see a message like “Initialized empty Git repository in /path/to/your/project/.git/”

That’s it! You now have a Git repository. The .git folder (which might be hidden) contains all of Git’s tracking information.

Step 3: Configure Git (First Time Only)

Before your first commit, tell Git who you are:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

This information is stored with every commit you make, so others know who made changes.

Basic Git Workflow

Git has a simple workflow that you’ll use constantly:

  1. Make changes to your files
  2. Stage the changes you want to save
  3. Commit the changes with a message

Let’s walk through this process.

Making Your First Commit

Let’s say you create a file called index.html in your project:

<!DOCTYPE html>
<html>
<head>
    <title>My First Project</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>

Now, here’s how to save this in Git:

Step 1: Check Status

git status

This shows you which files Git is tracking and which changes haven’t been saved yet. You’ll see index.html listed as an “untracked file.”

Step 2: Stage the File

git add index.html

This tells Git, “I want to include this file in my next snapshot.” You can also stage all files at once:

git add .

Step 3: Commit

git commit -m "Add initial HTML file"

The -m flag lets you add a message describing what this commit does. Good commit messages are important—they help you (and others) understand what changed and why.

Congratulations! You’ve made your first commit. Git has now saved a snapshot of your project.

Understanding Git Status

The git status command is your best friend. It tells you:

  • Which files have been modified
  • Which files are staged (ready to commit)
  • Which files aren’t being tracked by Git

Here’s what you might see:

$ git status
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes)

        modified:   index.html

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

        style.css

This tells you:

  • index.html has been modified but not staged
  • style.css is a new file that Git isn’t tracking yet

Viewing Your History

One of Git’s most useful features is viewing your project’s history:

git log

This shows all your commits with messages, authors, and dates. You’ll see something like:

commit abc123def456...
Author: Your Name <your.email@example.com>
Date:   Mon Dec 20 2025 10:30:00

    Add initial HTML file

For a more compact view:

git log --oneline

This shows just the commit hash and message, one per line.

Making Changes and New Commits

Let’s say you modify index.html to add some CSS:

<!DOCTYPE html>
<html>
<head>
    <title>My First Project</title>
    <style>
        body { font-family: Arial; }
        h1 { color: blue; }
    </style>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>

Now follow the same workflow:

git status          # See what changed
git add index.html  # Stage the changes
git commit -m "Add basic styling to page"

You now have two commits in your history. Each commit is a snapshot you can return to.

Undoing Changes

One of Git’s superpowers is undoing mistakes. Here are common scenarios:

Discard Unstaged Changes

Made changes you don’t want? If you haven’t staged them yet:

git checkout -- filename

Or in newer Git versions:

git restore filename

This reverts the file to its last committed state. Warning: This permanently discards your uncommitted changes!

Unstage a File

Staged a file but changed your mind?

git reset HEAD filename

Or:

git restore --staged filename

The file stays modified, but it’s no longer staged for commit.

Undo the Last Commit (Keep Changes)

Made a commit but want to change the message or add more files?

git commit --amend

This lets you modify the last commit. If you’ve already pushed to a remote repository, be careful with this command.

Branches: Working on Different Versions

Branches are one of Git’s most powerful features. They let you work on different versions of your code simultaneously.

Think of branches like parallel universes. You can have:

  • A main branch with your stable, working code
  • A feature branch where you’re experimenting with a new feature
  • A bugfix branch where you’re fixing a specific issue

Creating and Switching Branches

# Create a new branch
git branch feature-new-design

# Switch to that branch
git checkout feature-new-design

Or in one command:

git checkout -b feature-new-design

In newer Git versions:

git switch -c feature-new-design

Viewing Branches

See all your branches:

git branch

The asterisk (*) shows which branch you’re currently on.

Merging Branches

When your feature is complete, merge it back into main:

git checkout main
git merge feature-new-design

This combines the changes from your feature branch into main.

Deleting Branches

Once merged, you can delete the branch:

git branch -d feature-new-design

Working with Remote Repositories

So far, we’ve worked with a local repository (on your computer). But Git really shines when you use remote repositories (like GitHub) to:

  • Backup your code online
  • Collaborate with others
  • Share your projects

Connecting to GitHub

  1. Create a repository on GitHub (don’t initialize with README)
  2. Connect your local repository:
git remote add origin https://github.com/yourusername/your-repo.git
  1. Push your code:
git push -u origin main

Now your code is backed up online and others can see it!

Common Remote Commands

Pull changes from remote:

git pull

Push your commits to remote:

git push

See remote repositories:

git remote -v

Essential Git Commands Cheat Sheet

Here are the Git commands you’ll use most often:

# Setup
git init                    # Initialize a new repository
git config --global user.name "Name"
git config --global user.email "email"

# Daily workflow
git status                  # Check what's changed
git add <file>              # Stage a file
git add .                   # Stage all changes
git commit -m "Message"     # Save a snapshot
git log                     # View history
git log --oneline           # Compact history

# Branches
git branch                  # List branches
git branch <name>           # Create branch
git checkout <branch>       # Switch branch
git checkout -b <name>      # Create and switch
git merge <branch>          # Merge branch

# Remote
git remote add origin <url> # Add remote
git push                    # Upload commits
git pull                    # Download commits
git clone <url>             # Copy repository

# Undoing
git restore <file>          # Discard changes
git restore --staged <file> # Unstage file

Common Git Workflows

Solo Project Workflow

For personal projects:

  1. Make changes to files
  2. git add .
  3. git commit -m "Description"
  4. Repeat

Feature Development Workflow

For adding new features:

  1. git checkout -b feature-name
  2. Make changes and commit
  3. git checkout main
  4. git merge feature-name
  5. git push

Collaboration Workflow

When working with others:

  1. git pull (get latest changes)
  2. Make your changes
  3. git add . and git commit
  4. git push (share your changes)

Best Practices

1. Commit Often

Make small, frequent commits rather than one huge commit at the end. Each commit should represent a logical unit of work.

2. Write Good Commit Messages

Good messages explain what and why, not just what:

# Bad
git commit -m "fix"

# Good
git commit -m "Fix login button not responding to clicks"

3. Don’t Commit Generated Files

Don’t commit files that are automatically generated (like compiled code, node_modules, etc.). Use a .gitignore file to exclude them.

4. Pull Before Push

Always pull the latest changes before pushing your own:

git pull
git push

5. Use Branches

Don’t work directly on main/master. Create branches for features, experiments, and fixes.

The .gitignore File

Create a .gitignore file in your project root to tell Git which files to ignore:

# Dependencies
node_modules/
vendor/

# Build outputs
dist/
build/
*.min.js

# Environment files
.env
.env.local

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

# OS files
.DS_Store
Thumbs.db

Common Mistakes and How to Avoid Them

1. Committing to Wrong Branch

Mistake: Making commits on main when you meant to work on a feature branch.

Solution: Always check which branch you’re on with git branch before starting work.

2. Forgetting to Pull

Mistake: Pushing changes without pulling first, causing conflicts.

Solution: Make git pull a habit before git push.

3. Vague Commit Messages

Mistake: Commit messages like “update” or “fix stuff.”

Solution: Be specific about what changed and why.

4. Not Using Branches

Mistake: Working directly on main and breaking working code.

Solution: Create a branch for every feature or fix.

Learning Resources

Official Documentation

Interactive Learning

  • Learn Git Branching: learngitbranching.js.org - Visual, interactive Git tutorial
  • GitHub Learning Lab: Hands-on Git and GitHub training

Practice

  • Create a GitHub account and start using it
  • Contribute to open source projects
  • Use Git for all your coding projects, even small ones

Conclusion

Git might seem intimidating at first, but like any tool, it becomes second nature with practice. Start with the basics: init, add, commit, status, and log. As you become comfortable, gradually learn about branches, merging, and working with remotes.

Remember, you don’t need to master every Git command to be productive. Most developers use the same small set of commands for 90% of their work. Focus on understanding the concepts—staging, committing, branching—and the commands will make more sense.

The best way to learn Git is to use it. Start using Git for your next project, even if it’s just a small personal one. Make commits, create branches, experiment. The more you use it, the more natural it becomes.

Git is a tool that will serve you throughout your coding journey. Whether you’re building solo projects or working on a team, version control is essential. And once you’re comfortable with Git, you’ll wonder how you ever managed without it.

Happy coding, and remember: commit early, commit often!

Recently Used Tools