git init - Initialize repogit add - Stage changesgit commit - Save snapshotsgit status - Check stategit log - View historygit remote add - Link remotegit push - Upload changesgit pull - Download updatesgit clone - Copy repo💡 Today: Advanced workflows with branching, merging, and more!
Branches allow parallel development without affecting the main codebase
# Create a new branch
git branch feature-login
# List all branches
git branch
# Switch to a branch
git checkout feature-login
# Create and switch in one command
git checkout -b feature-dashboard
# Modern way (Git 2.23+)
git switch feature-dashboard
git switch -c new-feature
# Delete a branch
git branch -d old-branch
🌿 Branches are lightweight pointers to commits
Combine changes from different branches
# Switch to the branch you want to merge INTO
git checkout main
# Merge feature branch into main
git merge feature-login
# View merged branches
git branch --merged
# Delete merged branch
git branch -d feature-login
Conflicts occur when the same lines are changed in both branches
# Git marks conflicts in files:
<<<<<<< HEAD (current branch)
let name = "Alice";
const age = 25;
=======
let name = "Bob";
const age = 30;
>>>>>>> feature-branch (incoming changes)
<<<, ===, >>>)git add <resolved-file> - Mark as resolvedgit commit - Complete the merge💡 Use git merge --abort to cancel merge
Switch branches, commits, or restore files
# Switch branches
git checkout main
git checkout feature-branch
# Create and switch to new branch
git checkout -b new-feature
# Checkout specific commit (detached HEAD)
git checkout abc1234
# Restore a file from another branch
git checkout main -- README.md
# Restore file to last commit state
git checkout -- file.txt
# Modern alternatives (Git 2.23+)
git switch main # for switching branches
git switch -c new-branch # create and switch
git restore README.md # for restoring files
git restore --source=main README.md
Rebase replays commits from one branch onto another, creating a linear history
# Update feature branch with latest main
git checkout feature-branch
git rebase main
# If conflicts occur:
# 1. Resolve conflicts in files
# 2. git add <resolved-files>
# 3. git rebase --continue
# Skip current commit
git rebase --skip
# Abort rebase and return to original state
git rebase --abort
💡 Always test after rebase before pushing!
Clean up commit history before pushing
# Edit last 3 commits interactively
git rebase -i HEAD~3
# Options in editor:
# pick = use commit
# squash = combine with previous
# reword = change commit message
# edit = stop to amend commit
# drop = remove commit
git reflog records every HEAD movement - even "lost" commits!
# View reflog history
git reflog
# Sample output:
# abc1234 HEAD@{0}: commit: Added login feature
# def5678 HEAD@{1}: rebase finished
# ghi9012 HEAD@{2}: checkout: moving to main
# jkl3456 HEAD@{3}: commit: Fixed bug
# ...
# Recover a "lost" commit
git checkout abc1234
# Or create a branch from it
git branch recovered-branch abc1234
# Reset to a previous state
git reset --hard HEAD@{2}
💡 Reflog is local only - expires after 90 days by default
git revert creates a new commit that undoes changes from a previous commit
# Revert a specific commit
git revert abc1234
# Revert without auto-commit (review first)
git revert -n abc1234
# Revert multiple commits
git revert abc1234 def5678
# Revert a merge commit (requires -m flag)
git revert -m 1 merge-commit-hash
# Continue after resolving conflicts
git revert --continue
# Abort revert
git revert --abort
💡 Revert is the safest way to undo changes on public branches
# Safe undo
git revert abc1234
# Destructive undo
git reset --hard HEAD~1
🛡️ Golden Rule: Use revert for public history, reset only for local changes
git reflog to view your historygit revert on a commitgit rebase -i) to squash commits💡 Good Git habits save time and prevent disasters!
git reflog to explore your historyQuestions?
Keep practicing and mastering Git workflows!