Git is a free, open-source Distributed Version Control System (DVCS) created by Linus Torvalds in 2005 to manage Linux kernel development.
Instead of saving project copies as project_v1, project_v2_final, or project_v2_FINAL_fixed, Git tracks changes to your project over time as a timeline of snapshots called commits.
Unlike centralized version control systems (like SVN) that rely on a single central server, Git gives every developer a full copy of the repository's entire history on their local machine.
Working Directory: Your project folder where you physically create, edit, or delete files.
Staging Area (Index): A draft space where you collect specific modified files that you want to include in your next save point.
Repository (.git directory): The database where Git permanently stores the snapshot history as compressed objects.
Commits: A lightweight snapshot of your entire project at a specific point in time. Each commit has a unique SHA-1 (or SHA-256) hash ID, an author timestamp, and a commit message.
Branching: Isolated lines of development. You can branch off the main codebase, experiment or build a new feature safely, and merge it back without breaking working code.
HEAD: A pointer in Git that references your current active commit/branch.
GitHub is a cloud-based hosting platform built on top of Git. While Git tracks code changes locally on your machine, GitHub acts as the central hub where developers store, share, and collaborate on those code repositories online.
Acquired by Microsoft in 2018, it is the largest developer platform in the world, hosting over 100 million developers and hundreds of millions of public and private repositories.
Repositories (Repos): Cloud storage containers for your project's files, commit history, and revision logs.
Pull Requests (PRs): The core mechanism for proposed code changes. Developers push a branch to GitHub, open a PR, and invite teammates to review, comment on, and test the code before merging it into the main branch.
Forks: A complete copy of someone else's repository under your account, allowing you to freely experiment or contribute back to open-source projects.
GitHub Actions: Integrated Continuous Integration / Continuous Deployment (CI/CD) pipelines. Allows you to automate building, testing, and deploying applications directly from code events (e.g., automatically run unit tests whenever a PR is created).
GitHub Packages: Hosting service for software packages, npm modules, or Docker containers alongside your source code.
GitHub Issues & Projects: Built-in task tracking, Kanban boards, and milestone management to coordinate development workflows.
GitHub Copilot: AI pair programmer that provides real-time code completion, chat assistance, and context-aware suggestions directly inside your code editor.
GitHub Pages: Free static website hosting directly from a GitHub repository (ideal for personal portfolios, documentation, or project landing pages).
Collaboration: Multiple engineers can touch the same codebase simultaneously without overwriting each other's work.
GeeksforGeeks
Open Source Ecosystem: It hosts the vast majority of public libraries, frameworks, and tools powering modern tech.
Developer Portfolio: Profiles serve as living resumes, displaying public contributions, activity green graphs, and personal projects.
Before starting, install Git and the GitHub CLI (gh). Then configure your global settings in your terminal.
Setting your email and name ensures all your project commits are properly linked to your GitHub account.
git config --global user.email "your_email@example.com"
git config --global user.name "Your Name"
gh auth login
When developing Python applications, setting up an isolated virtual environment (.venv) is essential to prevent conflicting packages.
Create and enter your project folder:
mkdir my-python-app
cd my-python-app
Initialize Git repository:
git init
Set up and activate a Python Virtual Environment:
python3 -m venv .venv
source .venv/bin/activate
Create a .gitignore file:
Crucial Step: Never upload your .venv folder or binary cache files to GitHub!
echo ".venv/" >> .gitignore
echo "__pycache__/" >> .gitignore
Add sample files:
echo "# My Python App" >> README.md
echo 'print("Hello from Git and Python!")' >> app.py
Now that the initial environment and files are ready, publish them to GitHub.
# 1. Stage all tracked files
git add .
# 2. Rename the default branch to main
git branch -M main
# 3. Create your first commit
git commit -m "Initial commit: basic structure and app script"
# 4. Link your local project to your remote GitHub repo
git remote add origin https://github.com/your_username/your_repository.git
# 5. Push code and establish tracking
git push -u origin main
When adding new features, isolate your changes on a dedicated branch instead of committing directly to main.
# Create and switch to new branch
git checkout -b feature
# Update your Python file
echo 'print("Adding new feature!")' >> app.py
# Stage, commit, and push
git add app.py
git commit -m "Add new feature output to app"
git push -u origin feature
# Create a Pull Request via GitHub CLI
gh pr create
Once your feature is reviewed and ready:
# Switch to main branch
git checkout main
# Merge the feature branch
git merge feature
# Push updated main branch
git push -u origin main
Every commit creates a snapshot with a unique SHA-1 hash (e.g., e4d3a2b). Git allows you to view and move through this historical timeline seamlessly.
# One-line condensed summary (hash + commit message)
git log --oneline
# Graphical log showing branch splits and merges
git log --oneline --graph --all
# Show exact line-by-line code changes (diff) for the last 2 commits
git log -p -2
You can inspect old versions of your code without deleting any history. Moving the HEAD pointer takes you back to that specific point in time:
# Step back 1 commit relative to current position
git switch --detach HEAD~1
# Jump directly to a specific commit by its hash
git switch <commit-id>
[Commit A] ---> [Commit B] ---> [Commit C] (main)
^
HEAD
(Detached HEAD State)
What is Detached HEAD?
Your active workspace is temporarily detached from any branch. You can test older code or run scripts safely. To return to your latest work, run:
git switch main
When code on a shared branch needs to be rolled back, git revert is the golden standard because it never deletes project history.
Instead of erasing commits, git revert creates a brand-new commit containing the exact inverse changes of the target commit.
Initial State:
[Commit 1: Add app.py] ---> [Commit 2: Add bug] (main)
After `git revert HEAD`:
[Commit 1] ---> [Commit 2: Add bug] ---> [Commit 3: Revert "Add bug"] (main)
# 1. Undo the latest commit
git revert HEAD
# 2. Undo a specific commit from history
git revert a1b2c3d
A merge commit joins two branches together, giving it two parent commits:
Parent 1 (1): Main branch before the merge.
Parent 2 (2): Feature branch that was merged in.
To revert a merged feature while keeping the main branch intact:
git revert -m 1 <merge-commit-hash>
Why -m 1? It instructs Git to treat Parent 1 (the main line) as the baseline to retain, discarding the changes from Parent 2.