Mastering Git & Github: A Comprehensive Guide with Examples

Shanmukh KarraShanmukh Karra
5 min read

Git has revolutionized version control in software development, enabling teams to collaborate efficiently while maintaining code integrity. This comprehensive guide covers all essential Git concepts from basic to advanced, complete with practical examples to help you master this powerful tool.

Introduction to Git

Git is a distributed version control system that tracks changes in files, allowing multiple developers to work on the same project simultaneously. Created by Linus Torvalds in 2005, Git is:

  • Platform-independent: Works on Windows, macOS, and Linux

  • Open-source: Free to use and modify

  • Efficient: Handles large projects with ease

  • Reliable: Maintains complete history and enables easy recovery

Git Workflow Basics

Git operates through three main areas:

  1. Working Directory: Where you make changes to files

  2. Staging Area: Where you prepare changes for committing

  3. Repository: Where committed changes are permanently stored

Working Directory → (git add) → Staging Area → (git commit) → Repository

Getting Started with Git

Installation

# On Linux (RedHat/CentOS)
yum install git -y

# On Debian/Ubuntu
apt-get install git -y

# Verify installation
git --version

Initializing a Repository

# Create project directory
mkdir my-project
cd my-project

# Initialize Git repository
git init .

# The .git directory contains all repository metadata
ls -la .git

Basic File Operations

# Create a new file
touch index.html

# Check status
git status  # Shows untracked files

# Stage the file
git add index.html

# Commit the file
git commit -m "Initial commit - added index.html"

# View commit history
git log

Working with Multiple Files

# Create multiple files
touch style.css script.js README.md

# Stage all files at once
git add .

# Or stage specific files
git add style.css script.js

# Commit all staged files
git commit -m "Added CSS, JS, and documentation"

# View concise commit history
git log --oneline

Branching: Isolating Your Work

Branches allow you to work on features or fixes without affecting the main codebase.

Basic Branch Operations

# List all branches (asterisk shows current branch)
git branch

# Create new feature branch
git branch feature/login

# Switch to new branch
git checkout feature/login
# OR combine creation and switching
git checkout -b feature/login

# Make changes and commit
echo "Login form HTML" > login.html
git add login.html
git commit -m "Added login page"

# Switch back to main branch
git checkout main

# Merge feature branch
git merge feature/login

# Delete branch after merge
git branch -d feature/login

Branch Management

# Rename a branch
git branch -m old-name new-name

# Force delete unmerged branch
git branch -D experimental-feature

# Recover a deleted branch
git branch recovered-feature <last-commit-id>

Advanced Git Operations

Stashing Changes

# Working on a feature when urgent fix needed
git stash save "WIP: user profile updates"

# Switch to hotfix branch
git checkout -b hotfix/security-patch

# After fixing, return to feature
git checkout feature/user-profile
git stash apply

# Manage multiple stashes
git stash list
git stash drop stash@{1}  # Delete specific stash
git stash clear          # Delete all stashes

Merging and Conflict Resolution

# Create conflicting changes
git checkout -b feature/header
# Modify header.html
git add header.html
git commit -m "Updated header design"

git checkout main
# Modify same file
git add header.html
git commit -m "Changed header color"

# Attempt merge
git merge feature/header
# CONFLICT occurs in header.html

# Resolve conflict by editing file
# Then mark as resolved
git add header.html
git commit -m "Merged header changes"

🍒Cherry-Picking Specific Changes

# Identify commit to cherry-pick (from another branch)
git log feature/payment --oneline
# 123abc4 Added secure payment processing

# Apply to current branch
git cherry-pick 123abc4

Undoing Changes

# Revert a specific commit (creates new undo commit)
git revert 456def7

# Reset to previous commit (dangerous - rewrites history)
git reset --hard HEAD~1  # Discards last commit and changes
git reset --soft HEAD~1  # Keeps changes staged

# Restore a deleted file
git restore deleted-file.txt

Collaborating with Remote Repositories

GitHub Basics

# Link local repo to GitHub
git remote add origin https://github.com/user/repo.git

# Verify remote
git remote -v

# Push to remote
git push -u origin main

# Clone existing repository
git clone https://github.com/user/repo.git
cd repo

# Get latest changes
git pull origin main

Branch Management with Remotes

# Push new branch to remote
git push -u origin feature/dashboard

# Track remote branch
git checkout --track origin/feature/dashboard

# Delete remote branch
git push origin --delete old-feature

Best Practices and Workflows

  1. Commit Often: Small, focused commits are easier to understand and manage

  2. Write Good Messages: Use imperative mood ("Fix bug" not "Fixed bug")

  3. Branch Strategically: Use feature branches for new work

  4. Pull Before Push: Always sync with remote before pushing

  5. Review Changes: Use git diff before staging and committing

.gitignore Example

Create a .gitignore file to exclude files from version control:

# Ignore system files
.DS_Store
Thumbs.db

# Ignore build artifacts
/node_modules/
/build/
*.exe

# Ignore environment files
.env
config.ini

Real-World Example: Feature Development Workflow

# Start new feature
git checkout -b feature/user-settings

# Make changes
vim settings.html
vim settings.css

# Stage and commit
git add .
git commit -m "Create user settings page"

# Push to remote
git push -u origin feature/user-settings

# Create Pull Request on GitHub
# After review and approval:

# Merge to main
git checkout main
git pull origin main
git merge feature/user-settings
git push origin main

# Clean up
git branch -d feature/user-settings
git push origin --delete feature/user-settings

Conclusion

Mastering Git transforms how you collaborate on software projects. This guide covered:

  • Core Git concepts and workflow

  • Branching strategies for parallel development

  • Advanced operations like stashing and cherry-picking

  • Team collaboration using remote repositories

  • Best practices for efficient version control

Remember that Git is a powerful tool with many capabilities. Start with these fundamentals, practice regularly, and gradually explore more advanced features as your projects grow in complexity.

0
Subscribe to my newsletter

Read articles from Shanmukh Karra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shanmukh Karra
Shanmukh Karra

Hi there! 👋 I'm Shanmukh Karra, a passionate DevOps Engineer with a strong foundation in modern infrastructure automation, CI/CD, and cloud technologies. As a fresher, I am eager to apply my skills in Git, Linux, Jenkins, Maven, Docker, Kubernetes, Terraform, AWS Cloud, Prometheus, and Grafana to build scalable, efficient, and resilient systems. I thrive in environments that encourage automation, continuous improvement, and collaboration between development and operations teams. My goal is to streamline software delivery pipelines, optimize cloud infrastructure, and ensure high availability through Infrastructure as Code (IaC), monitoring, and DevOps best practices. What I Bring to the Table: ✅ Version Control & CI/CD: Proficient in Git, Jenkins, and Maven for seamless code integration and deployment. ✅ Containerization & Orchestration: Hands-on experience with Docker and Kubernetes for scalable microservices. ✅ Infrastructure as Code (IaC): Skilled in Terraform for provisioning and managing cloud resources. ✅ Cloud & Monitoring: Familiar with AWS Cloud, along with Prometheus & Grafana for observability and performance tracking. ✅ Linux & Scripting: Strong command over Linux environments and scripting for automation. I'm constantly learning and experimenting with new tools to stay ahead in the ever-evolving DevOps landscape. Let's connect and build something amazing together! 🚀