A complete beginner's guide covering Git, GitHub, version control, branching, merging, pull requests, and essential Git commands.

Keep reading
Software development isn't just about writing code - it's about managing change. Whether you're building a personal project, collaborating with a team, or contributing to open source, you'll constantly make changes to your codebase. Some changes improve your application, while others introduce bugs. Imagine losing weeks of work because you accidentally deleted a file or overwrote important code. This is exactly the problem Git was created to solve.
In this guide, you'll learn what Git and GitHub are, how they work, the essential commands every developer should know, and the best practices used by professional software engineers.
Version Control is a system that records changes made to files over time, allowing developers to track modifications, restore previous versions, and collaborate without overwriting each other's work.
Think of it as a time machine for your code.
Instead of saving files like:
Version control keeps a complete history of your project while maintaining only one organized project folder.
Version control solves all these issues by maintaining a structured history of every change.
Everything is stored on a single computer.
Advantages - Simple - Fast
Disadvantages - No collaboration - Data can be lost if the computer fails
A central server stores all project history.
Examples: - SVN: Subversion - CVS: Concurrent Versions System
Advantages - Easy collaboration - Central backup
Disadvantages -If the server goes down, work stops - Single point of failure
Every developer has a complete copy of the repository, including its history.
Git belongs to this category.
Advantages - Work offline - Faster operations - Better collaboration - Full project backup
Git is a free, open-source Distributed Version Control System created by Linus Torvalds in 2005.
Its primary purpose is to help developers track changes in source code while enabling multiple people to work on the same project efficiently.
Git keeps snapshots of your project over time rather than storing multiple copies of the same files.
Every snapshot is called a commit.
Each commit contains:
This allows you to travel back to any point in your project's history.
Many beginners confuse Git and GitHub, but they are different.
Git is the version control software that runs on your computer.
GitHub is a cloud platform that hosts Git repositories online.
GitHub provides features such as:
Think of Git as Microsoft Word and GitHub as Google Drive.
One helps you create and manage files, while the other stores and shares them online.
Although they're often mentioned together, Git and GitHub serve different purposes.
Think of it this way: Git is the engine that powers version control, and GitHub is the platform that lets you store, share, and collaborate on Git repositories.
Before writing commands, let's understand some important concepts.
A repository is your project along with its complete version history.
It contains:
A commit is a saved snapshot of your project.
Each commit records what changed and why it changed.
Good commit messages are descriptive.
Example:
git commit -m "Add authentication"
git commit -m "Fix login validation bug"
Avoid messges like:
git commit -m "Update"
git commit -m "Changes"
The Working Directory contains the files you're currently editing.
Nothing here is tracked until you tell Git to track it.
The staging area acts like a preparation zone.
It lets you decide which changes should be included in the next commit.
The local repository stores all commits on your computer.
Even without internet, Git continues working perfectly.
A remote repository lives on platforms like GitHub.
It allows multiple developers to collaborate.
A branch is an independent line of development.
Instead of modifying the main codebase directly, developers create branches for new features.
Merging combines changes from one branch into another.
Cloning creates a complete copy of a remote repository on your computer.
A fork creates your own copy of someone else's repository under your GitHub account.
This is common in open-source development.
Download Git for your operating system from the official website.
Install -> Git
After installation, verify it using:
git --version
Configure Git with your identity:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Verify your configuration:
git config --list
Navigate to your project folder:
cd my-project
Initialize Git:
git init
Git creates a hidden .git directory containing all version history.
Check the repository status:
git status
Git follows a simple workflow.
Working Directory
↓
git add
↓
Staging Area
↓
git commit
↓
Local Repository
↓
git push
↓
Remote Repository
This flow is the foundation of Git.
git status
Shows modified, staged, and untracked files.
Add one file:
git add index.js
Add everything:
git add .
git commit -m "Add login feature"
git log
Compact version:
git log --oneline
git diff
Discard changes:
git restore filename
git rm filename
git mv oldname newname
Create a repository on GitHub.
Connect your local repository:
git remote add origin https://github.com/username/project.git
Push your code:
git push -u origin main
Download updates:
git pull origin main
Fetch updates without merging:
git fetch origin
Professional developers rarely work directly on the main branch.
Instead, they create feature branches.
Create a branch:
git branch feature-login
Switch to it:
git checkout feature-login
Or using the newer command:
git switch feature-login
Create and switch simultaneously:
git switch -c feature-login
List branches:
git branch
Merge:
git merge feature-login
Delete branch:
git branch -d feature-login
Sometimes two developers edit the same section of a file.
Git cannot automatically decide which version is correct.
This results in a merge conflict.
To resolve it:
Merge conflicts are normal and become easier to handle with experience.
GitHub enables developers worldwide to work together.
Common collaboration workflow:
mainA Pull Request (PR) lets teammates review code, suggest improvements, detect bugs, and approve changes before merging.
It allows teammates to:
PRs are one of GitHub's most valuable collaboration features.
Git offers several ways to undo changes. Restore uncommitted changes:
git restore file.txt
Undo a commit while preserving changes:
git reset --soft HEAD~1
Undo commit permanently:
git reset --hard HEAD~1
Create a new commit that reverses an older commit:
git revert <commit-hash>
Use git reset --hard carefully because it permanently discards changes.
Temporarily save unfinished work.
git stash
Restore it:
git stash pop
Mark important versions such as releases.
git tag v1.0
Copy a specific commit from another branch.
git cherry-pick <commit-hash>
Rebase rewrites commit history by moving your branch onto another branch. It creates a cleaner, more linear history than frequent merge commits.
git rebase main
Use rebase with care, especially on branches that others are using.
Following good Git practices makes collaboration easier and project history cleaner.
.gitignore file to exclude unnecessary files.main branch stable.Let's see how a typical feature is developed.
git init — Initialize a repository.git status — Check repository status.git add . — Stage all changes.git commit -m "message" — Save a snapshot.git log — View commit history.git diff — Compare changes.git branch — List branches.git switch -c branch-name — Create and switch to a branch.git merge branch-name — Merge a branch.git pull — Download and merge updates.git push — Upload commits to GitHub.git fetch — Download updates without merging.git clone <repository-url> — Copy a remote repository.git stash — Temporarily save changes.git restore <file> — Discard local changes.git tag v1.0 — Create a version tag.git revert <commit-hash> — Reverse a commit safely.Git is more than just a tool - it's an essential skill for every software developer. From tracking changes and recovering lost work to enabling seamless collaboration across teams, Git forms the foundation of modern software development.
GitHub complements Git by providing a platform where developers can store repositories, review code, collaborate through Pull Requests, manage projects, and contribute to open-source software.
Learning Git may feel overwhelming at first because of the many commands and concepts, but the key is consistent practice. Start with the basics - creating repositories, making commits, working with branches, and pushing code to GitHub. As you gain confidence, explore advanced topics such as rebasing, cherry-picking, GitHub Actions, and continuous integration.
Mastering Git and GitHub won't just make you a better programmer - it will make you a more effective collaborator and prepare you for real-world software development.
Happy coding!