Git (Version Control System)

Git Version Control System (VCS) 

Introduction

In modern software development, managing source code efficiently is essential. Whether you are working alone or in a team of hundreds of developers, Version Control Systems (VCS) help track changes, collaborate effectively, and maintain a complete history of your project.

One of the most widely used Version Control Systems today is Git.

Git is a free, open-source, distributed version control system originally developed by Linus Torvalds in 2005 for Linux kernel development. Today, Git powers millions of software projects worldwide and serves as the foundation for platforms like GitHub, GitLab, and Bitbucket.


What is Version Control System (VCS)?

A Version Control System is a tool that:

  • Tracks changes made to source code.
  • Maintains a history of modifications.
  • Enables multiple developers to work on the same project.
  • Allows rollback to previous versions when errors occur.
  • Facilitates collaboration and code review.

Without version control, developers often create multiple copies of files such as:

project_final.c
project_final_v2.c
project_final_v3.c
project_final_latest.c

This quickly becomes difficult to manage.

Git solves this problem by maintaining a structured history of every change.


Why Git?

Git offers several advantages:

✅ Fast and efficient

✅ Distributed architecture

✅ Supports branching and merging

✅ Open source and free

✅ Reliable and secure

✅ Large community support

✅ Integration with GitHub, GitLab, Azure DevOps, and Bitbucket


Centralized vs Distributed Version Control

Centralized VCS

Examples:

  • CVS
  • SVN (Subversion)

In centralized systems:

  • Only one central repository exists.
  • Developers must connect to the server to access history.

Drawbacks

  • Single point of failure
  • Internet dependency
  • Slower collaboration

Distributed VCS (Git)

In Git:

  • Every developer has a complete copy of the repository.
  • Entire project history is available locally.
  • Faster operations.
  • Better reliability.

Advantages

  • Offline work possible
  • Faster commits
  • Efficient branching
  • Better backup and redundancy

Git Workflow Overview

Working Directory
       ↓
   git add
       ↓
 Staging Area
       ↓
 git commit
       ↓
  Local Repository
       ↓
   git push
       ↓
 Remote Repository (GitHub)

Understanding this workflow is the key to mastering Git.


Installing Git

Windows

Download Git from:

https://git-scm.com

Verify installation:

git --version

Output:

git version 2.x.x


Initial Git Configuration

Before using Git, configure your username and email.

git config --global user.name "yourname"
git config --global user.email "yourmailexample.com"

Verify configuration:

git config --list

Check username:

git config user.name

Check email:

git config user.email


Creating a New Repository

Initialize Git inside your project directory.

git init

Output:

Initialized empty Git repository

This creates a hidden .git folder that stores the repository information.


Checking Repository Status

git status

This command displays:

  • Modified files
  • New files
  • Staged files
  • Untracked files

Example:

git status

Output:

Changes not staged for commit:
  modified: main.c


Adding Files to Staging Area

Add specific file:

git add main.c

Add multiple files:

git add file1.c file2.c

Add all files:

git add .


Committing Changes

Create a snapshot of the staged changes.

git commit -m "Added UART driver"

Example:

git commit -m "Implemented STM32 ADC configuration"


Viewing Commit History

Show all commits:

git log

Compact view:

git log --oneline

Graphical view:

git log --graph --oneline

Example:

git log --graph --oneline --all


Cloning an Existing Repository

Download a repository from GitHub.

Git creates a local copy of the repository.


Working with Branches

Branches allow developers to work independently without affecting the main code.

View Branches

git branch


Create Branch

git branch feature_uart


Switch Branch

git checkout feature_uart

Modern Git:

git switch feature_uart


Create and Switch Simultaneously

git checkout -b feature_adc

or

git switch -c feature_adc


Merging Branches

Merge feature branch into main branch.

git checkout main
git merge feature_adc

This combines branch changes into the main development line.


Connecting Local Repository to GitHub

Add remote repository:

Verify:

git remote -v

Output:


Pushing Code to GitHub

Push code:

git push origin main

First push:

git push -u origin main

The -u option sets the upstream branch.


Pulling Latest Changes

Download and merge updates:

git pull origin main


Fetching Changes Without Merging

git fetch

Useful when you want to inspect changes first.


Comparing Changes

Compare current modifications:

git diff

Compare staged changes:

git diff --staged

Compare branches:

git diff main feature_adc


Removing Files

Delete from Git and filesystem:

git rm file.c

Delete only from Git tracking:

git rm --cached file.c


Renaming Files

git mv old.c new.c


Undoing Changes

Restore file:

git restore file.c

Restore all files:

git restore .


Undo Staging

Remove file from staging area:

git restore --staged file.c


Viewing Remote Repositories

git remote -v


Creating Tags

Tags mark important releases.

Create tag:

git tag v1.0

View tags:

git tag

Push tags:

git push origin --tags


Git Stash

Temporarily save unfinished work.

Save changes:

git stash

View stash list:

git stash list

Restore changes:

git stash pop

Very useful during urgent bug fixes.


Reverting Commits

Safely undo a commit.

git revert commit_id

Example:

git revert a1b2c3d


Resetting Commits

Soft reset:

git reset --soft HEAD~1

Hard reset:

git reset --hard HEAD~1

⚠️ Use hard reset carefully because changes are permanently removed.


Viewing Specific Commit

git show commit_id

Example:

git show a1b2c3d


Ignoring Files with .gitignore

Create:

.gitignore

Example:

.exe
.obj
*.log
build/
Debug/

Useful for preventing temporary files from being committed.


GitHub Workflow Example

Step 1

Create project:

mkdir Embedded_Project
cd Embedded_Project
git init

Step 2

Create source file:

touch main.c

Step 3

Stage file:

git add .

Step 4

Commit changes:

git commit -m "Initial project setup"

Step 5

Connect GitHub:

Step 6

Push code:

git push -u origin main


Best Practices for Git

✔ Commit Frequently

Make small meaningful commits.

Bad:

git commit -m "Changes"

Good:

git commit -m "Added STM32 UART initialization driver"


✔ Use Branches

Create separate branches for:

  • Features
  • Bug fixes
  • Experiments

✔ Pull Before Push

git pull origin main

This minimizes merge conflicts.


✔ Write Meaningful Commit Messages

Examples:

git commit -m "Fixed ADC sampling issue"
git commit -m "Added CAN communication support"
git commit -m "Refactored GPIO driver"


✔ Keep Repository Clean

Use .gitignore to exclude:

  • Build files
  • Executables
  • Logs
  • Temporary files

Common Git Interview Questions

What is Git?

Git is a distributed version control system used to track source code changes and collaborate on software development.

What is a Repository?

A repository is a storage location containing project files and the history of changes.

What is Staging Area?

An intermediate area where changes are prepared before committing.

Difference Between Git Pull and Git Fetch?

  • git fetch downloads changes only.
  • git pull downloads and merges changes.

Difference Between Git Merge and Git Rebase?

  • Merge preserves history.
  • Rebase creates a cleaner linear history.

Conclusion

Git has become the industry standard for version control and software collaboration. Whether you are developing embedded firmware for STM32, Linux device drivers, Raspberry Pi applications, desktop software, or cloud applications, learning Git is an essential skill for every engineer.

By mastering commands such as git init, git add, git commit, git branch, git merge, git push, git pull, git stash, and git revert, developers can efficiently manage projects, collaborate with teams, and maintain a reliable software development workflow.

Git is not just a tool. It is a professional development practice that every software engineer should master.

Comments

Popular posts from this blog

Setting up a USB thermal printer with a Raspberry Pi 3B+

Create a autorun for a PyQt5 raspberry pi Application

Basic Input/Output C Program