A comprehensive guide for developers and students learning version control
"Version control is the foundation of modern software development. Whether you're working on a personal project or collaborating in a large team, understanding Git and GitHub helps you track, manage, and share your code effectively."
๐น Introduction
This quick revision guide covers the most essential Git and GitHub commands every developer should know.
I originally compiled these commands for my own use while learning version control โ written in simple, natural language to ensure clarity. Over time, they've become my go-to reference whenever I need a quick refresher.
Let's dive in ๐
โ๏ธ Git Basics
โ Check Git Version
Make sure Git is installed and check its version:
git --version
๐งฉ Configure Git
Set up your username and email (these appear in your commits):
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
View your configuration:
git config --list
๐ Local Repository Management
๐๏ธ Initialize a Repository
Create a new Git repository in your project folder:
git init
๐ Check Repository Status
View changes, staged files, and untracked files:
git status
โ Add Files
Add specific files or all files to the staging area:
git add filename
git add .
๐พ Commit Changes
Save staged changes with a descriptive message:
git commit -m "Your message here"
๐ฟ Branch Information
View all branches or see which branch you're on:
git branch
๐ View Commit History
List all commits:
git log
๐งฎ Working with File Versions
๐ง Compare Changes
Compare modified files before committing:
git diff
Compare staged files:
git diff --cached
๐ View Previous File Versions
See the content of a file in a previous commit:
git show commit-id:filename
๐ Switch Between Versions
Checkout an older version and return to current:
git checkout commit-id -- filename
git checkout branch-name -- filename
๐ง Undoing Changes
๐งน Restore Unstaged Changes
Undo changes before staging:
git restore filename
๐งฑ Restore Staged Changes
Undo files added with git add:
git restore --staged filename
โช Reset Committed Changes
Undo commits:
git reset --soft HEAD^ # Uncommit but keep changes
git reset --hard HEAD^ # Uncommit and delete changes
๐พ Stash Temporary Changes
Save changes temporarily without committing:
git stash # Save current changes
git stash push -m "message" # Save with message
Retrieve stashed changes:
git stash pop # Apply and remove latest stash
git stash apply # Apply but keep stash
Manage stashes:
git stash list # View all stashes
git stash drop # Delete latest stash
๐ฐ๏ธ Git Logs & History
View your project's commit history in different formats:
git log -p -2 # Last two commits with diff
git log --stat # Summary of changes
git log --pretty=oneline
git log --pretty=format:"%h - %an, %ar : %s"
๐ Search Within Logs
Find specific commits:
git log -S function_name
git log --grep="keyword"
๐๏ธ Filter by Time or Author
git log --since="2024-01-01"
git log --author="Sameen"
git log --no-merges
โ๏ธ Remote Repository Management (GitHub)
๐ Link Local Repo to Remote
git remote add origin remote_URL
git branch -M main
git push -u origin main
๐ Push & Pull
Push your commits:
git push
Pull updates from the remote repo:
git pull
๐ฆ Clone a Repository
git clone repo_URL
This downloads the repo and full commit history to your local machine.
๐ฟ Branching & Merging
๐ง Branch Management
Create and switch to a new branch (modern way):
git switch -c new-branch
Or the traditional way:
git branch new-branch
git checkout new-branch
Switch between existing branches:
git switch branch-name
git checkout branch-name
๐ Merge Branches
git checkout main
git merge feature-branch
๐ Rebase (Alternative to Merge)
Rebase replays commits on top of another branch (creates linear history):
git checkout feature-branch
git rebase main
Interactive rebase (edit commit history):
git rebase -i HEAD~3 # Rebase last 3 commits
Rebase vs Merge: Rebase creates cleaner history but rewrites commit history. Use merge for shared branches.
โ ๏ธ Handling Merge Conflicts
If the same file is edited differently in two branches, Git will flag a merge conflict. You'll need to manually resolve it, save, and commit again.
๐ Cherry-Pick Commits
Apply specific commits from one branch to another:
git cherry-pick commit-hash
Cherry-pick multiple commits:
git cherry-pick commit1 commit2 commit3
Cherry-pick a range of commits:
git cherry-pick start-commit^..end-commit
Useful for applying hotfixes or specific features to different branches.
๐งญ GitHub Workflow
๐ด Forking and Pull Requests
On GitHub:
- Click Fork to create your own copy of a repo.
- Make your changes locally.
- Push your updates to your forked repo.
- Open a Pull Request (PR) to suggest merging your changes into the original repository.
This is the core of open-source collaboration.
๐งน Git Utilities
๐ช .gitignore
Exclude files or folders from version control:
# Example .gitignore
node_modules/
.env
__pycache__/
๐งฝ Clean Untracked Files
git clean -n # Preview
git clean -f # Delete untracked files
๐ท๏ธ Tags (Versioning)
Create and manage version tags:
git tag -a v1.0 -m "Initial release"
git push origin --tags
Delete tags:
git tag -d tag_name
git push origin --delete tag_name