Git & GitHub Mastery
The complete guide to version control. Master Git commands, branching strategies, collaboration workflows, and GitHub features.
Table of Contents
1. Git Fundamentals
Git is a distributed version control system that tracks changes in your code. Unlike centralized systems, every developer has a full copy of the repository.
Key Concepts
# Create new repository
git init
# Or clone existing repository
git clone https://github.com/username/repo.git
# Clone with specific branch
git clone -b develop https://github.com/username/repo.git2. Git Configuration
Set User Identity
# Set your name (required for commits)
git config --global user.name "Ramjee Prasad"
# Set your email
git config --global user.email "ashish23481@gmail.com"
# View all config
git config --listConfigure your identity for all repositories on this machine.
SSH Key Setup
# Generate SSH key
ssh-keygen -t ed25519 -C "your_email@example.com"
# Start SSH agent
eval "$(ssh-agent -s)"
# Add key to agent
ssh-add ~/.ssh/id_ed25519
# Copy public key to clipboard (add to GitHub)
cat ~/.ssh/id_ed25519.pubSet up SSH for secure authentication with GitHub.
Useful Aliases
# Create shortcuts for common commands
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --all"Save time with custom command shortcuts.
3. Essential Commands
Check Status
git status
# Short format
git status -sView the state of your working directory and staging area.
Stage Changes
# Stage specific file
git add filename.txt
# Stage all changes
git add .
# Stage all changes including deletions
git add -A
# Interactive staging
git add -pAdd changes to the staging area before committing.
Commit Changes
# Commit with message
git commit -m "Add user authentication feature"
# Commit all tracked changes
git commit -am "Quick fix for login bug"
# Amend last commit
git commit --amend -m "Updated commit message"
# Empty commit (useful for triggering CI)
git commit --allow-empty -m "Trigger build"Save your staged changes with a descriptive message.
View History
# View commit log
git log
# One line per commit
git log --oneline
# Graph view with branches
git log --oneline --graph --all
# Show last 5 commits
git log -5
# Show commits by author
git log --author="Ramjee"Browse the commit history of your repository.
View Differences
# Unstaged changes
git diff
# Staged changes
git diff --staged
# Between branches
git diff main..feature-branch
# Specific file
git diff filename.txtCompare changes between commits, branches, or files.
Stash Changes
# Save work in progress
git stash
# With description
git stash push -m "WIP: new feature"
# List stashes
git stash list
# Apply latest stash
git stash pop
# Apply specific stash
git stash apply stash@{2}
# Drop stash
git stash drop stash@{0}Temporarily save uncommitted changes.
4. Branching & Merging
Branch Operations
# List branches
git branch
# List all (including remote)
git branch -a
# Create new branch
git branch feature-login
# Switch to branch
git checkout feature-login
# Create and switch (shortcut)
git checkout -b feature-login
# Modern way (Git 2.23+)
git switch -c feature-login
# Delete branch
git branch -d feature-login
# Force delete
git branch -D feature-loginCreate, list, switch, and delete branches.
Merge Branches
# Switch to target branch
git checkout main
# Merge feature branch
git merge feature-login
# Merge with commit message
git merge feature-login -m "Merge feature-login into main"
# Merge without fast-forward
git merge --no-ff feature-login
# Abort merge
git merge --abortCombine changes from different branches.
5. Merge vs Rebase
Git Merge
Creates a merge commit, preserves history exactly as it happened.
git checkout main
git merge feature✓ Safe, non-destructive
✓ Shows complete history
Git Rebase
Re-applies commits on top of another branch, creates linear history.
git checkout feature
git rebase main✓ Clean, linear history
⚠ Don't rebase public branches
Interactive Rebase
# Rebase last 3 commits
git rebase -i HEAD~3
# Commands in interactive mode:
# pick = use commit
# reword = use commit, edit message
# squash = meld into previous commit
# drop = remove commitModify, combine, or remove commits in your history.
6. Remote Repositories
Remote Management
# View remotes
git remote -v
# Add remote
git remote add origin https://github.com/user/repo.git
# Change remote URL
git remote set-url origin git@github.com:user/repo.git
# Remove remote
git remote remove originManage connections to remote repositories.
Push & Pull
# Push to remote
git push origin main
# Push and set upstream
git push -u origin main
# Force push (use carefully!)
git push --force-with-lease
# Pull changes
git pull origin main
# Pull with rebase
git pull --rebase origin mainSync your local repository with the remote.
Fetch & Prune
# Fetch all branches
git fetch origin
# Fetch and prune deleted branches
git fetch --prune
# Delete local branches that are gone on remote
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -dDownload remote changes without merging.
7. GitHub Essentials
Fork & Clone Workflow
# 1. Fork repo on GitHub (web)
# 2. Clone your fork
git clone git@github.com:YOUR_USER/repo.git
# 3. Add upstream remote
git remote add upstream https://github.com/ORIGINAL_OWNER/repo.git
# 4. Sync with upstream
git fetch upstream
git checkout main
git merge upstream/mainContribute to open source projects.
Pull Request Workflow
# 1. Create feature branch
git checkout -b feature/new-feature
# 2. Make changes and commit
git add .
git commit -m "Add new feature"
# 3. Push to your fork
git push origin feature/new-feature
# 4. Create PR on GitHub (web)
# 5. After PR merge, cleanup
git checkout main
git pull origin main
git branch -d feature/new-featureStandard workflow for contributing via pull requests.
GitHub CLI (gh)
# Install: https://cli.github.com
# Login
gh auth login
# Create PR from terminal
gh pr create --title "Add feature" --body "Description"
# List PRs
gh pr list
# Checkout PR locally
gh pr checkout 123
# Merge PR
gh pr merge 1238. Undoing Changes
⚠️ Warning: Some of these commands rewrite history. Never rewrite history on shared/public branches!
Discard Local Changes
# Discard changes in working directory
git checkout -- filename.txt
# Discard all changes
git checkout -- .
# Modern way
git restore filename.txtDiscard uncommitted changes to files.
Unstage Files
# Unstage file
git reset HEAD filename.txt
# Modern way
git restore --staged filename.txt
# Unstage all
git reset HEADRemove files from staging area without losing changes.
Reset Commits
# Soft reset (keep changes staged)
git reset --soft HEAD~1
# Mixed reset (keep changes unstaged)
git reset HEAD~1
# Hard reset (discard everything!)
git reset --hard HEAD~1
# Reset to specific commit
git reset --hard abc1234Move HEAD to a different commit. Be careful with --hard!
Revert Commits
# Create new commit that undoes changes
git revert HEAD
# Revert specific commit
git revert abc1234
# Revert multiple commits
git revert HEAD~3..HEADSafely undo commits by creating a new commit (safe for shared branches).
9. Git Workflows
GitHub Flow (Simple)
Best for continuous deployment. Simple and effective.
main ──────●──────●──────●──────●──────
\ /
feature ●────●────●- Create branch from main
- Make commits
- Open Pull Request
- Review & Merge
- Delete branch
Git Flow (Release-based)
Best for scheduled releases with multiple environments.
main ─────●─────────────────●─────
\ /
release ●─────●───────●
\ \ /
develop ●──●───●──●───●───●───●──
\ / \ /
feature ●──● ●─────●10. Best Practices
Commit Messages
# Good commit message format
<type>(<scope>): <subject>
<body>
<footer>
# Examples:
feat(auth): add JWT authentication
fix(api): resolve null pointer in user service
docs(readme): update installation steps
refactor(core): simplify database queriesGeneral Tips
- ✓ Commit early, commit often
- ✓ Write meaningful commit messages
- ✓ Use feature branches
- ✓ Pull before you push
- ✓ Never commit secrets/passwords
- ✓ Use .gitignore for build files
- ✓ Review code before merging
- ✓ Keep commits atomic (one thing per commit)
.gitignore Template
# IDE
.idea/
.vscode/
*.iml
# Build
target/
build/
dist/
node_modules/
# Environment
.env
.env.local
*.log
# OS files
.DS_Store
Thumbs.db
# Compiled
*.class
*.jar
*.warWritten by Ramjee Prasad • Backend Developer
← Back to Portfolio