Create a Branch
git branch <name>git branch feature/user-profileNote 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.
Git · 10 entries
git branch <name>git branch feature/user-profileNote 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.
git switch <branch>
git switch -c <new-branch>git switch feature/user-profile
# Create and switch in one step:
git switch -c feature/paymentsSwitched 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.
git checkout <branch>
git checkout -b <new-branch>git checkout main
git checkout -b bugfix/login-errorSwitched 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.
git merge <branch># Switch to the target branch first:
git switch main
git merge feature/user-profileUpdating 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.
# After a merge with conflicts:
git status
# Edit conflicted files, then:
git add <resolved-file>
git commitgit 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.
git branch -d <branch>
git branch -D <branch># 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-profileDeleted 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.
git branch [-a] [-r]# Local branches:
git branch
# All branches (local + remote):
git branch -a
# Remote branches only:
git branch -r bugfix/login-error
feature/payments
* main
remotes/origin/main
remotes/origin/feature/paymentsNote The asterisk (*) marks your current branch. Remote branches show as remotes/origin/<name>. Use git fetch first to update the remote branch list.
git branch -m <old-name> <new-name>
git branch -m <new-name># Rename the current branch:
git branch -m feature/user-settings
# Rename a different branch:
git branch -m old-name new-nameNote 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
git branch <name> <commit-hash>
git switch -c <name> <commit-hash>git switch -c hotfix/urgent-patch a1b2c3dSwitched 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.
git branch --merged [branch]
git branch --no-merged [branch]# Branches already merged into main:
git branch --merged main
# Branches NOT yet merged into main:
git branch --no-merged main feature/user-profile
bugfix/login-errorNote 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