Overview
This issue explains how to create and manage branches in Git and gives concrete examples for the following commands: git branch, git checkout (and git switch), git merge, and git rebase.
- git branch — list, create, delete, rename
-
List branches:
git branch
git branch -a # include remote branches
-
Create a new branch (local only):
git branch feature/my-feature
-
Delete a local branch:
git branch -d feature/my-feature # safe delete (refuses if not merged)
git branch -D feature/my-feature # force delete
-
Rename a branch (current branch):
git branch -m old-name new-name
Example workflow:
create and then list
git branch feature/login
git branch
- git checkout / git switch — switch branches and create+switch
-
Switch to an existing branch:
git checkout feature/login
modern alternative
git switch feature/login
-
Create and switch in one step:
git checkout -b feature/login
modern alternative
git switch -c feature/login
-
Checkout a specific commit (detached HEAD):
git checkout
Example:
git checkout -b feature/user-auth
start working and commit
git add .
git commit -m "Add user auth"
- git merge — combine branches
-
Merge a branch (e.g., feature into main):
git checkout main
git pull origin main
git merge --no-ff feature/user-auth
git push origin main
-
Fast-forward merge (no merge commit created) happens if main has not diverged. To force a merge commit even when fast-forward is possible, use --no-ff.
Example (resolve conflicts if any):
git checkout main
git merge feature/user-auth
if conflicts, edit files, then:
git add
git commit
git push origin main
- git rebase — replay commits to create a linear history
-
Rebase your feature branch onto main to incorporate upstream changes:
git checkout feature/user-auth
git fetch origin
git rebase origin/main
-
Interactive rebase (edit, squash, reorder commits):
git rebase -i HEAD~3
-
After rebasing a branch that was pushed to a remote, you will need to force-push:
git push --force-with-lease origin feature/user-auth
Example workflow to keep a clean feature branch before merging:
git checkout feature/user-auth
git fetch origin
git rebase origin/main
resolve conflicts if any, continue rebase
git rebase --continue
push the rebased branch (force push required if branch was pushed earlier)
git push --force-with-lease origin feature/user-auth
When to use merge vs rebase
- Use merge to preserve the true history of merges (common for shared/public branches).
- Use rebase to keep a linear, cleaner history on private/feature branches before integrating.
- Never rebase public/shared commits that others might have based work on (unless you coordinate).
Best practices
- Use feature branches: branch names like feature/, fix/, chore/ help organization.
- Keep branch scope small and focused.
- Pull and rebase (or merge main) frequently to reduce conflicts.
- Prefer --force-with-lease over --force to avoid overwriting others' work.
- Use pull requests for code review and to merge into main/master.
Short cheat-sheet
- Create & switch: git checkout -b OR git switch -c
- Switch: git checkout OR git switch
- List: git branch OR git branch -a
- Delete: git branch -d OR git branch -D
- Merge: git checkout main; git merge
- Rebase: git checkout ; git rebase origin/main
- Force push after rebase: git push --force-with-lease
If you want, I can expand this into a step-by-step tutorial for a specific repo or include examples showing conflict resolution.
files.zip
Overview
This issue explains how to create and manage branches in Git and gives concrete examples for the following commands: git branch, git checkout (and git switch), git merge, and git rebase.
List branches:
git branch
git branch -a # include remote branches
Create a new branch (local only):
git branch feature/my-feature
Delete a local branch:
git branch -d feature/my-feature # safe delete (refuses if not merged)
git branch -D feature/my-feature # force delete
Rename a branch (current branch):
git branch -m old-name new-name
Example workflow:
create and then list
git branch feature/login
git branch
Switch to an existing branch:
git checkout feature/login
modern alternative
git switch feature/login
Create and switch in one step:
git checkout -b feature/login
modern alternative
git switch -c feature/login
Checkout a specific commit (detached HEAD):
git checkout
Example:
git checkout -b feature/user-auth
start working and commit
git add .
git commit -m "Add user auth"
Merge a branch (e.g., feature into main):
git checkout main
git pull origin main
git merge --no-ff feature/user-auth
git push origin main
Fast-forward merge (no merge commit created) happens if main has not diverged. To force a merge commit even when fast-forward is possible, use --no-ff.
Example (resolve conflicts if any):
git checkout main
git merge feature/user-auth
if conflicts, edit files, then:
git add
git commit
git push origin main
Rebase your feature branch onto main to incorporate upstream changes:
git checkout feature/user-auth
git fetch origin
git rebase origin/main
Interactive rebase (edit, squash, reorder commits):
git rebase -i HEAD~3
After rebasing a branch that was pushed to a remote, you will need to force-push:
git push --force-with-lease origin feature/user-auth
Example workflow to keep a clean feature branch before merging:
git checkout feature/user-auth
git fetch origin
git rebase origin/main
resolve conflicts if any, continue rebase
git rebase --continue
push the rebased branch (force push required if branch was pushed earlier)
git push --force-with-lease origin feature/user-auth
When to use merge vs rebase
Best practices
Short cheat-sheet
If you want, I can expand this into a step-by-step tutorial for a specific repo or include examples showing conflict resolution.
files.zip