DEV Community

Cover image for A Beginner's Guide to Git : Understanding Version Control
Bancy Njeru
Bancy Njeru

Posted on

A Beginner's Guide to Git : Understanding Version Control

When you first hear about Git, it sounds intimidating, but once you understand why Git exists and how the basic commands work, everything starts to make sense.

This article will walk you through:

  • What version control is (in layman’s language)
  • How Git tracks changes
  • How to push and pull code
  • The most common Git commands

What Is Version Control?

Remember working on assignments and saving your file as:

  • project_final.docx
  • project_final_v2.docx
  • project_final_really_final.docx

Now imagine doing this with code, across months or years, with several people.

That’s where version control comes in.

Version control helps you:

  • Track changes over time
  • Go back to older versions if something breaks
  • Collaborate with others without tampering with each other’s work
  • Know who changed what and when

Git is the most popular version control system.


What Is Git?

Git is a tool that:

  • Runs on your computer
  • Tracks changes in your project files
  • Lets you save “snapshots” of your work
  • Connects your local project to online platforms like GitHub or GitLab

Key Git Concepts

1. Repository (Repo)

A repository is your project folder that Git is tracking.

  • Local repository → on your computer
  • Remote repository → online (e.g. GitHub)

2. Working Directory

This is where you write and edit your code.

  • Changes exist here but they are not saved yet

3. Staging

Before saving changes, Git asks:

“Which changes do you want to include?”

The staging area is where you prepare changes before saving them.


4. Commit

A commit is a saved snapshot of your project.

Each commit:

  • Has an ID (identifier)
  • Has a description of the change
  • Can be restored later

Installing Git

Check if Git is installed:

git --version
Enter fullscreen mode Exit fullscreen mode

If not:

  • Linux
sudo apt install git
Enter fullscreen mode Exit fullscreen mode
  • macOS
brew install git
Enter fullscreen mode Exit fullscreen mode
  • Windows

Download from https://git-scm.com


Starting a Git Project

Initialize Git

Inside your project folder:

git init
Enter fullscreen mode Exit fullscreen mode

This tells Git to start tracking your project.


Check Repository Status

git status
Enter fullscreen mode Exit fullscreen mode

This command shows:

  • Modified files
  • Staged files
  • What Git is waiting for

Tracking Changes in Git 📝

Step 1: Make Changes

Edit or create a file:

project1.py
Enter fullscreen mode Exit fullscreen mode

Git detects the change but hasn’t saved it yet.


Step 2: Stage Changes

git add project1.py
Enter fullscreen mode Exit fullscreen mode

Or stage all changes:

git add .
Enter fullscreen mode Exit fullscreen mode

Step 3: Commit Changes

git commit -m "Add python file"
Enter fullscreen mode Exit fullscreen mode

This saves a snapshot of your work.

💡 Tip: Write commit messages as clear explanations for humans.


Understanding Push and Pull 🔄

Local vs Remote Repositories

  • Local → your computer
  • Remote → GitHub / GitLab

Pushing Code (Upload Your Work)

Push sends your commits to the remote repository:

git push origin main
Enter fullscreen mode Exit fullscreen mode

After pushing:

  • Your code is online
  • Others can access it
  • Your work is backed up

Pulling Code (Download Updates)

Pull gets the latest changes from the remote repository:

git pull origin main
Enter fullscreen mode Exit fullscreen mode

You should pull everytime before starting work


A Typical Git Workflow 🔁

git pull
# make changes
git status
git add .
git commit -m "Describe what you changed"
git push
Enter fullscreen mode Exit fullscreen mode

This is the basic Git loop you'll use daily.


Viewing Change History

git log
Enter fullscreen mode Exit fullscreen mode

This shows:

  • Commit history
  • Authors
  • Timestamps
  • Commit messages

Final Thoughts 🌱

Git might feel overwhelming at first, but you get a hold of it.

Focus on these commands:

  • git status
  • git add
  • git commit
  • git push
  • git pull

With practice, you'll become a Git Guru.

Happy coding! 🚀

Top comments (0)