Back to Portfolio
DevOps / Version Control

Git & GitHub Mastery

The complete guide to version control. Master Git commands, branching strategies, collaboration workflows, and GitHub features.

By Ramjee PrasadDecember 15, 202520 min read

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

Repository: Project folder tracked by Git
Commit: Snapshot of your changes
Branch: Parallel version of your code
Remote: Server-hosted repository (GitHub)
Clone: Copy of a remote repository
Pull/Push: Sync with remote
Initialize a New Repository
# 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.git

2. 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 --list

Configure 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.pub

Set 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 -s

View 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 -p

Add 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.txt

Compare 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-login

Create, 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 --abort

Combine 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 commit

Modify, 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 origin

Manage 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 main

Sync 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 -d

Download 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/main

Contribute 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-feature

Standard 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 123

8. 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.txt

Discard uncommitted changes to files.

Unstage Files

# Unstage file
git reset HEAD filename.txt

# Modern way
git restore --staged filename.txt

# Unstage all
git reset HEAD

Remove 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 abc1234

Move 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..HEAD

Safely 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       ●────●────●
  1. Create branch from main
  2. Make commits
  3. Open Pull Request
  4. Review & Merge
  5. Delete branch

Git Flow (Release-based)

Best for scheduled releases with multiple environments.

main    ─────●─────────────────●─────
              \               /
release        ●─────●───────●
                \     \     /
develop  ●──●───●──●───●───●───●──
          \    /  \       /
feature    ●──●    ●─────●
Branches: main, develop, feature/*, release/*, hotfix/*

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 queries

General 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

.gitignore
# IDE
.idea/
.vscode/
*.iml

# Build
target/
build/
dist/
node_modules/

# Environment
.env
.env.local
*.log

# OS files
.DS_Store
Thumbs.db

# Compiled
*.class
*.jar
*.war

Written by Ramjee Prasad • Backend Developer

← Back to Portfolio