Managing Branches: Creation, Switching, and Deletion
Branches allow developers to work on new features or bug fixes in an isolated environment without affecting the main production codebase.
1. Creating and Switch Branches
To view existing local branches:
git branchCreating and Switching Methods
- Traditional Method:
# Create branch
git branch feature-login
# Switch branch
git checkout feature-login- Modern Method (Recommended):
# Create and switch to a new branch in one step
git switch -c feature-login2. Deleting Merged Branches
Once your feature branch is merged into the main codebase, delete it to keep your repository clean:
# Safe delete: Git checks if changes are merged first
git branch -d feature-login
# Force delete: Delete the branch even if it has unmerged work
git branch -D feature-login3. Best Practices for Branching
- Keep Branches Focused: Create one branch per feature or bug fix. Avoid combining unrelated changes into a single branch.
- Name Branches clearly: Use descriptive naming conventions, such as
feature/user-authorbugfix/issue-42.
Published on Last updated: