Branching in Git

Why is branching important?

Branching allows developers to work on new features, bug fixes, or experiments without affecting the main project. It promotes collaboration, parallel development, and safer code integration.

  1. Keeps the main branch (often called main or master ) clean and stable.
  2. Reduces conflicts when multiple developers are working simultaneously.
  3. Makes collaboration easier by isolating untested or experimental code.
  4. Developers can merge their work back into the main branch only after review and testing.

Git Commands for Branching

Git Commands for Branching

Create a new branch:

git checkout -b branch-name

Switch to an existing branch:

git checkout branch-name

Merge your branch into main:

git checkout main
git pull
git merge your-branch
git push

Close (delete) a branch locally:

git branch -d branch-name

Delete a branch from the remote (e.g., GitHub):

git push origin --delete branch-name