Git (Version Control System)
Git Version Control System (VCS)
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.
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.
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
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.
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
- 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.
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
- 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.
Understanding this workflow is the key to mastering Git.
Installing Git
Windows
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 --listCheck username:
git config user.nameCheck email:
git config user.email
Before using Git, configure your username and email.
Verify configuration:
Check username:
Check email:
Creating a New Repository
Initialize Git inside your project directory.
git initOutput:
Initialized empty Git repositoryThis creates a hidden .git folder that stores the repository information.
Initialize Git inside your project directory.
Output:
This creates a hidden .git folder that stores the repository information.
Checking Repository Status
git statusThis command displays:
- Modified files
- New files
- Staged files
- Untracked files
Example:
git statusOutput:
Changes not staged for commit: modified: main.c
This command displays:
- Modified files
- New files
- Staged files
- Untracked files
Example:
Output:
Adding Files to Staging Area
Add specific file:
git add main.cAdd multiple files:
git add file1.c file2.cAdd all files:
git add .
Add specific file:
Add multiple files:
Add all files:
Committing Changes
Create a snapshot of the staged changes.
git commit -m "Added UART driver"Example:
git commit -m "Implemented STM32 ADC configuration"
Create a snapshot of the staged changes.
Example:
Viewing Commit History
Show all commits:
git logCompact view:
git log --onelineGraphical view:
git log --graph --onelineExample:
git log --graph --oneline --all
Show all commits:
Compact view:
Graphical view:
Example:
Cloning an Existing Repository
Download a repository from GitHub.
git clone https://github.com/user/project.gitGit creates a local copy of the 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.
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_uartModern Git:
git switch feature_uart
Modern Git:
Create and Switch Simultaneously
git checkout -b feature_adcor
git switch -c feature_adc
or
Merging Branches
Merge feature branch into main branch.
git checkout maingit merge feature_adcThis combines branch changes into the main development line.
Merge feature branch into main branch.
This combines branch changes into the main development line.
Connecting Local Repository to GitHub
Add remote repository:
git remote add origin https://github.com/user/project.gitVerify:
git remote -vOutput:
Add remote repository:
Verify:
Output:
Pushing Code to GitHub
Push code:
git push origin mainFirst push:
git push -u origin mainThe -u option sets the upstream branch.
Push code:
First push:
The -u option sets the upstream branch.
Pulling Latest Changes
Download and merge updates:
git pull origin main
Download and merge updates:
Fetching Changes Without Merging
git fetchUseful when you want to inspect changes first.
Useful when you want to inspect changes first.
Comparing Changes
Compare current modifications:
git diffCompare staged changes:
git diff --stagedCompare branches:
git diff main feature_adc
Compare current modifications:
Compare staged changes:
Compare branches:
Removing Files
Delete from Git and filesystem:
git rm file.cDelete only from Git tracking:
git rm --cached file.c
Delete from Git and filesystem:
Delete only from Git tracking:
Renaming Files
git mv old.c new.c
Undoing Changes
Restore file:
git restore file.cRestore all files:
git restore .
Restore file:
Restore all files:
Undo Staging
Remove file from staging area:
git restore --staged file.c
Remove file from staging area:
Viewing Remote Repositories
git remote -v
Creating Tags
Tags mark important releases.
Create tag:
git tag v1.0View tags:
git tagPush tags:
git push origin --tags
Tags mark important releases.
Create tag:
View tags:
Push tags:
Git Stash
Temporarily save unfinished work.
Save changes:
git stashView stash list:
git stash listRestore changes:
git stash popVery useful during urgent bug fixes.
Temporarily save unfinished work.
Save changes:
View stash list:
Restore changes:
Very useful during urgent bug fixes.
Reverting Commits
Safely undo a commit.
git revert commit_idExample:
git revert a1b2c3d
Safely undo a commit.
Example:
Resetting Commits
Soft reset:
git reset --soft HEAD~1Hard reset:
git reset --hard HEAD~1⚠️ Use hard reset carefully because changes are permanently removed.
Soft reset:
Hard reset:
⚠️ Use hard reset carefully because changes are permanently removed.
Viewing Specific Commit
git show commit_idExample:
git show a1b2c3d
Example:
Ignoring Files with .gitignore
Create:
.gitignoreExample:
.exe.obj*.logbuild/Debug/Useful for preventing temporary files from being committed.
Create:
Example:
Useful for preventing temporary files from being committed.
GitHub Workflow Example
Step 1
Create project:
mkdir Embedded_Projectcd Embedded_Projectgit init
Create project:
Step 2
Create source file:
touch main.c
Create source file:
Step 3
Stage file:
git add .
Stage file:
Step 4
Commit changes:
git commit -m "Initial project setup"
Commit changes:
Step 5
Step 6
Push code:
git push -u origin main
Push code:
Best Practices for Git
✔ Commit Frequently
Make small meaningful commits.
Bad:
git commit -m "Changes"Good:
git commit -m "Added STM32 UART initialization driver"
Make small meaningful commits.
Bad:
Good:
✔ Use Branches
Create separate branches for:
- Features
- Bug fixes
- Experiments
Create separate branches for:
- Features
- Bug fixes
- Experiments
✔ Pull Before Push
git pull origin mainThis minimizes merge conflicts.
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"
Examples:
✔ Keep Repository Clean
Use .gitignore to exclude:
- Build files
- Executables
- Logs
- Temporary files
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.
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.
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.
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.
git fetchdownloads changes only.git pulldownloads and merges changes.
Difference Between Git Merge and Git Rebase?
- Merge preserves history.
- Rebase creates a cleaner linear history.
- 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.
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
Post a Comment