A
AiTechWorlds
AiTechWorlds

# Create and switch to new branch
git switch -c feature/login # modern syntax (Git 2.23+)
git checkout -b feature/login # classic equivalent
# List branches
git branch # local
git branch -r # remote
git branch -a # all
# Switch branches
git switch main
git checkout main # classic
# Delete branches
git branch -d feature/done # safe delete (checks merged)
git branch -D feature/abandon # force delete
git push origin --delete feature/done # delete remote branch| Prefix | Purpose | Example |
|---|---|---|
feature/ | New feature | feature/user auth |
bugfix/ | Bug fix | bugfix/login crash |
hotfix/ | Urgent production fix | hotfix/payment null |
release/ | Release preparation | release/2.1.0 |
chore/ | Maintenance, tooling | chore/update deps |
docs/ | Documentation only | docs/api reference |
| Aspect | Merge | Rebase |
|---|---|---|
| History | Preserves full history (merge commit) | Linear, rewritten |
| Commit SHA | Unchanged | New SHAs on rebased branch |
| Conflict resolution | Once (at merge) | Per commit |
| Collaboration | Safe for shared branches | Unsafe on shared branches |
| Debugging | git bisect works | Clean log, easier to read |
| When to use | Main/shared branches | Local cleanup, feature branches |
# Feature branch has no divergence from main
git switch main
git merge feature/login # just moves the pointer forward, no merge commit
git merge --no-ff feature/login # force merge commit even if FF possible# Both branches have new commits โ creates a merge commit
git switch main
git merge feature/login
# Result: M---* (merge commit combining both histories)# Condense all feature commits into one commit on main
git switch main
git merge --squash feature/login
git commit -m "feat: add user authentication"
# Feature branch commits disappear from main history# Rebase feature branch onto main (move feature's base to main's tip)
git switch feature/login
git rebase main
# Interactive rebase โ rewrite last 5 commits
git rebase -i HEAD~5
# Editor opens with pick/squash/edit/reword/drop commands| Command | Action |
|---|---|
pick | Keep commit as is |
reword | Keep commit, edit message |
edit | Stop to amend commit |
squash | Meld into previous commit, keep messages |
fixup | Meld into previous commit, discard message |
drop | Delete commit entirely |
# After conflict, Git marks files:
<<<<<<< HEAD
current branch content
=======
incoming branch content
>>>>>>> feature/login
# 1. Edit files to resolve
# 2. Stage resolved files
git add src/auth.js
# 3. Continue merge or rebase
git merge --continue # for merge
git rebase --continue # for rebase
# Abort if needed
git merge --abort
git rebase --abortgit mergetool # opens configured visual tool
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'# Fetch remote changes (no merge)
git fetch origin
# Pull = fetch + merge
git pull origin main
# Pull with rebase (keeps history linear)
git pull --rebase origin main
git config --global pull.rebase true # make this the default
# Push feature branch
git push -u origin feature/login # -u sets upstream tracking
git push # subsequent pushes
git push --force-with-lease # safe force push (rebase workflow)# Apply a specific commit from another branch
git cherry-pick abc1234
# Apply a range of commits
git cherry-pick abc1234..def5678
# Pick without committing (stage only)
git cherry-pick -n abc1234| Model | Best For | Branch Structure |
|---|---|---|
| GitHub Flow | Continuous delivery | main + short feature branches |
| Git Flow | Versioned releases | main, develop, feature/, release/, hotfix/* |
| Trunk Based | High velocity teams | All commits to main via short lived branches |
| Forking | Open source | Fork โ PR โ upstream merge |
# See which branches are merged into main
git branch --merged main
# Find commit that introduced a bug (binary search)
git bisect start
git bisect bad # current commit is bad
git bisect good v1.0 # v1.0 was good
# Git checks out midpoint; test and mark good/bad
git bisect good
git bisect bad
git bisect reset # when done
# Compare two branches
git diff main..feature/login
git log main..feature/login --onelinegit push --force instead of --force with lease can silently overwrite someone else's pushgit add . without verifying each file accidentally stages unrelated changesdevelop โ main loses traceability to individual feature PRsGet more notes like this daily on Telegram!
Free study notes, cheat sheets & AI tips
Last reviewed on June 13, 2026 by the AiTechWorlds Notes Team. Free cheat sheet no signup required.
Join AiTechWorlds on Telegram and get daily AI tips, prompt engineering templates, coding resources, and exclusive content 100% free!
No spam. Leave anytime.