Part 1: Introduction to Version Control and Git Fundamentals

Introduction

I still remember the first time I lost hours of work because I didn't have proper version control. I was building a TypeScript API service, made some "quick changes" to refactor the authentication logic, and suddenly nothing worked. I couldn't remember exactly what I had changed, and my only backup was a zip file from two days ago. That painful experience taught me why version control isn't optionalβ€”it's fundamental to professional software development.

Fast forward to today, and I manage multiple TypeScript microservices across different repositories on GitHub. Version control has become second nature, but the journey from "git add, commit, push" to understanding proper workflows took time and real-world experience.

What Is Version Control?

Version control (also called source control) is a system that records changes to files over time so you can recall specific versions later. Think of it as a time machine for your codeβ€”you can go back to any point in your project's history, see what changed, who changed it, and why.

Why Version Control Matters

From my experience building production systems, here's why version control is critical:

1. Safety Net for Mistakes

When I accidentally deleted a critical database configuration file in one of my microservices, Git saved me:

# Oh no, I deleted the config!
rm src/config/database.ts

# Wait, I can restore it!
git checkout HEAD -- src/config/database.ts

2. Collaboration Without Chaos

In my multi-tenant POS system project, three developers worked on different features simultaneously. Without version control, we would have been emailing code files back and forth. With Git and GitHub:

Everyone works independently, and we merge changes systematically through pull requests.

3. Understanding Project History

When debugging a production issue, I often need to know when a bug was introduced:

4. Experimentation Without Fear

I wanted to try a different database query optimization approach but wasn't sure if it would work:

No risk to the main codebase.

Git is a distributed version control system created by Linus Torvalds in 2005. It's the industry standard and powers platforms like GitHub, GitLab, and Bitbucket.

Core Concepts

1. Repository (Repo)

A repository is a directory that Git tracks. It contains your project files and the complete history of all changes.

2. Commit

A commit is a snapshot of your project at a specific point in time. Each commit has:

  • A unique identifier (SHA hash)

  • Author information

  • Timestamp

  • Commit message describing the changes

3. Branch

A branch is an independent line of development. Think of it as a parallel universe where you can make changes without affecting the main codebase.

4. Remote

A remote is a version of your repository hosted on a server (like GitHub). It enables collaboration and backup.

Setting Up Git and GitHub

Installing Git

macOS:

Linux (Ubuntu/Debian):

Windows: Download from git-scm.comarrow-up-right

Initial Configuration

After installing Git, configure your identity:

Setting Up GitHub

  1. Create a GitHub Account: Go to github.comarrow-up-right and sign up

  2. Set Up SSH Keys (recommended for security):

  1. Add SSH Key to GitHub:

    • Go to GitHub Settings β†’ SSH and GPG keys

    • Click "New SSH key"

    • Paste your public key

  2. Test Connection:

Essential Git Commands and Workflows

Daily Workflow

Here's my typical workflow when working on a TypeScript microservice:

1. Start a New Feature

2. Make Changes and Commit

3. Push to GitHub

Checking Status and History

Undoing Changes

Discard Changes in Working Directory:

Unstage Files:

Undo Last Commit (keeping changes):

Working with Branches

Merging Branches

Real-World Example: Adding a New Feature

Here's how I recently added email notifications to one of my microservices:

Understanding the Git Workflow

Git has three main states for files:

  1. Working Directory: Your actual files where you make changes

  2. Staging Area (Index): Files marked to be included in the next commit

  3. Repository (.git directory): Committed snapshots of your project

Example:

Common Git Workflows

Feature Branch Workflow

This is what I use daily for my TypeScript microservices:

Best Practices for Commits

Write Good Commit Messages

Bad:

Good:

Commit Message Format I Use:

Types:

  • feat: New feature

  • fix: Bug fix

  • refactor: Code restructuring

  • docs: Documentation changes

  • test: Test additions/changes

  • chore: Maintenance tasks

Example:

Make Atomic Commits

Each commit should represent one logical change:

.gitignore: What Not to Commit

Always create a .gitignore file to exclude files that shouldn't be version controlled:

Conclusion

Version control with Git and GitHub is fundamental to modern software development. In this part, we covered:

  • Why version control matters for backend development

  • Core Git concepts: repositories, commits, branches, remotes

  • Setting up Git and GitHub with SSH authentication

  • Essential Git commands for daily workflows

  • Best practices for commits and git ignore

From my experience, mastering these fundamentals makes everything elseβ€”branching strategies, monorepo vs polyrepo decisions, CI/CD integrationβ€”much easier to understand and implement.

In Part 2, we'll dive deep into branching strategies and workflows, exploring Git Flow, GitHub Flow, and Trunk-Based Development with real examples from production microservices.


Key Takeaways:

  • Always commit early and often with meaningful messages

  • Use branches for features, never commit directly to main

  • Keep your commits atomic and focused

  • Review your changes before committing (git diff)

  • Pull before you push to avoid conflicts

Remember: Git is a tool that gets better with practice. Don't worry about making mistakesβ€”that's what version control is for!

Last updated