Git

Branching

Git · 10 entries

Create a Branch

syntax
git branch <name>
example
git branch feature/user-profile

Note This only creates the branch but does NOT switch to it. Use git switch or git checkout to move to the new branch. Branch names cannot contain spaces.

Switch Branches

syntax
git switch <branch>
git switch -c <new-branch>
example
git switch feature/user-profile

# Create and switch in one step:
git switch -c feature/payments
output
Switched to branch 'feature/user-profile'

Note git switch is the modern replacement for git checkout (for branch switching). The -c flag creates the branch if it doesn't exist. You'll get an error if you have uncommitted changes that conflict with the target branch.

Checkout a Branch (Classic)

syntax
git checkout <branch>
git checkout -b <new-branch>
example
git checkout main
git checkout -b bugfix/login-error
output
Switched to branch 'main'

Note git checkout does double duty: switching branches AND restoring files. The newer git switch (branches) and git restore (files) commands split this up for clarity. Both approaches still work.

Merge a Branch

syntax
git merge <branch>
example
# Switch to the target branch first:
git switch main
git merge feature/user-profile
output
Updating a1b2c3d..d4e5f6g
Fast-forward
 src/profile.js | 85 ++++++++++
 1 file changed, 85 insertions(+)

Note If there are no diverging commits, Git does a 'fast-forward' merge (no merge commit). Use --no-ff to force a merge commit even when fast-forward is possible, which keeps branch history visible in the log.

Resolve Merge Conflicts

syntax
# After a merge with conflicts:
git status
# Edit conflicted files, then:
git add <resolved-file>
git commit
example
git merge feature/payments
# CONFLICT in src/cart.js

# Open the file — look for conflict markers:
# <<<<<<< HEAD
# ... your changes ...
# =======
# ... their changes ...
# >>>>>>> feature/payments

# Manually resolve, then:
git add src/cart.js
git commit -m "Merge feature/payments, resolve cart conflict"

Note Never leave conflict markers (<<<, ===, >>>) in your code. Use git diff --check before committing to verify no markers remain. You can abort a conflicted merge with git merge --abort.

Delete a Branch

syntax
git branch -d <branch>
git branch -D <branch>
example
# Safe delete (only if fully merged):
git branch -d feature/user-profile

# Force delete (even if unmerged):
git branch -D experiment/failed-idea

# Delete remote branch:
git push origin --delete feature/user-profile
output
Deleted branch feature/user-profile (was d4e5f6g).

Note WARNING: -D (uppercase) force-deletes without checking if the branch is merged. You could lose unmerged work. Always use lowercase -d first, which will warn you if the branch has unmerged changes.

List Branches

syntax
git branch [-a] [-r]
example
# Local branches:
git branch

# All branches (local + remote):
git branch -a

# Remote branches only:
git branch -r
output
  bugfix/login-error
  feature/payments
* main
  remotes/origin/main
  remotes/origin/feature/payments

Note The asterisk (*) marks your current branch. Remote branches show as remotes/origin/<name>. Use git fetch first to update the remote branch list.

Rename a Branch

syntax
git branch -m <old-name> <new-name>
git branch -m <new-name>
example
# Rename the current branch:
git branch -m feature/user-settings

# Rename a different branch:
git branch -m old-name new-name

Note If the branch has been pushed, you'll also need to delete the old remote branch and push the new one: git push origin --delete old-name && git push -u origin new-name

Create Branch from a Specific Commit

syntax
git branch <name> <commit-hash>
git switch -c <name> <commit-hash>
example
git switch -c hotfix/urgent-patch a1b2c3d
output
Switched to a new branch 'hotfix/urgent-patch'

Note Useful when you need to branch from a point other than the current HEAD, such as creating a hotfix from a release tag or a known-good commit.

List Merged / Unmerged Branches

syntax
git branch --merged [branch]
git branch --no-merged [branch]
example
# Branches already merged into main:
git branch --merged main

# Branches NOT yet merged into main:
git branch --no-merged main
output
  feature/user-profile
  bugfix/login-error

Note Great for cleanup. Branches listed by --merged are safe to delete. Combine with xargs to bulk-delete: git branch --merged main | grep -v main | xargs git branch -d