Skip to main content

Command Palette

Search for a command to run...

Getting Started with Git

Git Basics: Your First Steps

Updated
9 min read
Getting Started with Git
M

I am an aspiring web developer on a mission to kick down the door into tech. Join me as I take the essential steps toward this goal and hopefully inspire others to do the same!

Why Version Control Matters

Have you ever found yourself with folders named "Project_Final," "Project_Final_v2," and the inevitable "Project_Final_FINAL"? If so, you're already familiar with the fundamental problem that version control systems solve. Version control is to code what time travel is to science fiction—a way to move through the history of your project with precision and purpose.

In today's software development landscape, version control isn't just a nice-to-have—it's essential. Modern applications are built by teams of developers working simultaneously on shared codebases, often spread across different time zones and continents. Without a robust way to track changes, merge work, and maintain a single source of truth, collaboration would quickly descend into chaos.

While several version control systems exist, Git has emerged as the industry standard. Created by Linus Torvalds (the founder of Linux) in 2005, Git has revolutionized how developers work together. Unlike its predecessors, Git is distributed, meaning each developer has a complete copy of the repository. This architecture brings unprecedented flexibility, allowing for offline work and complex branching strategies that make Git the backbone of modern development workflows.

As we explore Git's fundamentals in this article, you'll discover why it has become the tool of choice for everyone from solo hobbyists to massive enterprises managing millions of lines of code.

Initial Setup: Getting Git Ready

Before we can harness Git's power, we need to set it up on our machine.

Installing Git

The installation process varies slightly depending on your operating system:

Windows:

  • Download the installer from the official Git website

  • Run the installer, following the prompts

  • Opt for the default options unless you have specific preferences

macOS:

  • Option 1: Install using Homebrew with brew install git

  • Option 2: Download the installer from the Git website

  • Option 3: Xcode Command Line Tools includes Git (run xcode-select --install)

Linux (Ubuntu/Debian):

  • Run sudo apt-get update followed by sudo apt-get install git

  • Other distributions use their respective package managers (e.g., yum install git for Fedora)

Important Tip When Installing Git For The First Time

Avoid the default setting of using VIM as your text editor unless you specifically want it. VIM's keyboard shortcuts can be confusing for newcomers, potentially leading to frustration when writing commit messages.

If you find yourself trapped in VIM without knowing how to exit, press Esc, then type :wq and press Enter. This saves your changes and exits the editor.

To set a different editor (like VS Code), use:

git config --global core.editor "code --wait"

Configuring User Information

After installation, you need to tell Git who you are. This information is attached to every commit you make:

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

The --global flag means this configuration applies to all your repositories. You can omit it to configure settings for a single repository.

Setting Up SSH Keys (Optional)

While HTTPS works for repository access, SSH provides a more secure and convenient method, especially for frequent GitHub/GitLab users:

  1. Generate an SSH key pair:

     ssh-keygen -t ed25519 -C "your.email@example.com"
    
  2. Start the SSH agent:

     ssh-agent -s
    
  3. Add your private key to the SSH agent:

     ssh-add ~/.ssh/id_ed25519
    
  4. Copy your public key and add it to your GitHub/GitLab account:

     cat ~/.ssh/id_ed25519.pub
    

With these steps complete, you're ready to dive into Git's core concepts.

Core Concepts: Understanding Git's Foundation

Before we jump into commands, let's understand the fundamental concepts that make Git work.

Repositories Explained

A Git repository (or "repo") is essentially a database containing all the versions of your project's files, along with metadata about who changed what and when. The repository includes the entire history of changes, making it possible to revert to any previous state.

There are two types of repositories:

  • Local repository: Lives on your computer

  • Remote repository: Hosted on a server (like GitHub, GitLab, or Bitbucket)

Think of your local repository as your personal workspace, while the remote repository serves as the central collaboration hub for your team.

The Three States: Working Directory, Staging Area, Repository

Git manages your files through three distinct states:

  1. Working Directory: This is where you edit your files. These changes are local and haven't been saved to Git yet.

  2. Staging Area (Index): After making changes, you select which modifications should be included in your next commit. This intermediate step gives you fine-grained control over what changes get recorded.

  3. Repository (Commit History): Once changes are committed, they become a permanent part of your repository's history, each with a unique identifier.

This three-stage workflow is one of Git's most powerful features, allowing you to craft precise, meaningful commits instead of saving everything at once.

Understanding Commits as Snapshots

Unlike some version control systems that store differences between files, Git takes a snapshot of all your files at each commit. This approach makes operations like branching and merging much faster and more reliable.

Each commit contains:

  • A unique identifier (SHA-1 hash)

  • Author information

  • Timestamp

  • Commit message

  • Pointer to the previous commit (parent)

  • Snapshot of all tracked files

These commits form a chain, creating a complete history of your project. This design means Git can reconstruct any version of your project at any point in time.

Basic Operations: Essential Git Commands

With the concepts clear, let's explore the commands you'll use daily.

Initializing a Repository (git init)

To start tracking a project with Git, navigate to your project folder and run:

git init

This creates a hidden .git directory that stores all the version control information. Your project is now a Git repository, ready for tracking changes.

Cloning Existing Repositories (git clone)

When working with an existing project, you'll clone it to your local machine:

git clone <repository-url>

This creates a local copy of the remote repository, including all its history. You can specify HTTPS or SSH URLs:

  • HTTPS: https://github.com/username/repository.git

  • SSH: git@github.com:username/repository.git

Checking Status (git status)

To see which files have been modified, which are staged, and which are untracked:

git status

This command is your best friend when working with Git, providing a clear overview of your repository's current state.

Staging Changes (git add)

After making changes, you need to stage them before committing:

git add <filename>    # Stage a specific file
git add .             # Stage all changes
git add *.js          # Stage all JavaScript files

The staging area lets you commit only some changes while keeping others for a later commit, helping you create logically grouped commits.

Committing Changes (git commit)

Once your changes are staged, commit them to the repository:

git commit -m "Brief description of changes"

The -m flag allows you to provide a commit message directly in the command. Without it, Git opens your configured text editor for a more detailed message.

For small changes, you can skip the staging area with:

git commit -am "Message here"

This stages and commits all modified tracked files in one step (but doesn't include new files).

Viewing History (git log)

To explore your project's history:

git log

This shows all commits, with the most recent at the top. Each entry includes the commit hash, author, date, and message.

For a more compact view:

git log --oneline

To see a graphical representation of branch history:

git log --graph --oneline --all

As we learned from the first article, the git log command will open the terminal pager program called less if the output extends beyond the height of your terminal. Press q to exit this view.

Good Commit Practices: The Art of Meaningful History

A well-maintained Git history is like a well-documented story of your project. Here's how to write that story effectively.

Writing Meaningful Commit Messages

Good commit messages are crucial for maintaining a useful history:

  • Use the imperative mood: "Add feature" not "Added feature"

  • Keep the first line under 50 characters

  • Provide more details in the body if needed (separated by a blank line)

  • Explain why you made the change, not just what changed

A well-structured commit message looks like:

Add user authentication system

- Implement login form with validation
- Set up JWT token generation
- Create protected routes middleware

This addresses security requirements outlined in ticket #123

Atomic Commits

An "atomic" commit contains changes related to a single task or fix. Benefits include:

  • Easier code reviews

  • Simpler debugging (bisect can identify problematic commits)

  • Cleaner history

  • Ability to revert specific changes without affecting others

Instead of one massive commit containing multiple features, break your work into logical, self-contained pieces.

When to Commit

Finding the right frequency for commits is a balance:

  • Too infrequent: Large, unwieldy commits that mix unrelated changes

  • Too frequent: Fragmented history with incomplete implementations

Good times to commit include:

  • After completing a logical unit of work

  • Before trying something experimental

  • When implementation of a feature component is complete

  • After fixing a bug

  • Before making major changes to existing code

Your First Git Workflow: Putting It All Together

Let's walk through a practical example of using Git in a typical development scenario.

Step 1: Initialize a new project

mkdir my-project
cd my-project
git init

Step 2: Create some files

echo "# My Awesome Project" > README.md

Step 3: Check status

git status

You'll see README.md listed as an untracked file.

Step 4: Stage your changes

git add README.md

Step 5: Commit your changes

git commit -m "Initial commit with README"

Step 6: Make more changes

echo "This project demonstrates how to use Git." >> README.md

Step 7: Review what's changed

git diff

This shows the new line you added to README.md.

Step 8: Stage and commit again

git add README.md
git commit -m "Update README with project description"

Step 9: View your history

git log

You'll see your two commits, with the most recent at the top.

This simple workflow—making changes, staging, and committing—forms the foundation of your daily interactions with Git.

Conclusion: Building on the Basics

We've covered the essential concepts and commands that form the foundation of Git:

  • Setting up Git on your machine

  • Understanding repositories, commits, and Git's three-state architecture

  • Mastering basic commands: init, clone, status, add, commit, and log

  • Developing good commit practices

  • Walking through a practical Git workflow

These fundamentals give you the tools to start tracking your projects with Git, creating a reliable history of your work that you can navigate, share, and build upon.


In the next article, we'll explore one of Git's most powerful features: branching. You'll learn how to create parallel lines of development, allowing you to work on multiple features or fixes simultaneously without them interfering with each other. We'll also cover merging strategies to bring these branches back together seamlessly.

Commit to Success: Mastering Git From Init to Push

Part 6 of 8

Master Git from basic commands to advanced workflows in 8 parts. Your complete journey to version control expertise.

Up next

Git Ready to Branch Out

From Commits to Collaboration: Mastering Git Step by Step