■ Git Branch Explanation
■ What is a Git Branch?
- A branch is basically a pointer to a snapshot of your project’s history.
- `main` (or `master`) is the default branch.
- You create new branches to develop features, fix bugs, or experiment without affecting the main
branch.
■■ Common Git Branch Commands
1. Check current branch
git branch
- Lists all branches.
- The branch with a * is the one you’re currently on.
2. Create a new branch
git branch feature-xyz
- Creates a branch named 'feature-xyz'.
3. Switch to a branch
git checkout feature-xyz
or (newer command):
git switch feature-xyz
4. Create and switch in one step
git checkout -b feature-xyz
or
git switch -c feature-xyz
5. Push a branch to GitHub
git push origin feature-xyz
6. Merge a branch into main
git checkout main
git pull origin main
git merge feature-xyz
git push origin main
7. Delete a branch
- Locally: git branch -d feature-xyz
- On GitHub: git push origin --delete feature-xyz
■ Typical Workflow
1. Start from main:
git checkout main
git pull origin main
2. Create a feature branch:
git checkout -b feature-login
3. Work on your changes → commit:
git add .
git commit -m "Added login feature"
4. Push to GitHub:
git push origin feature-login
5. Open a Pull Request on GitHub to merge it into main.
■ Meaning of "feature-xyz"
- 'feature-xyz' is just the name of the branch.
- You can name it anything (e.g., login, bugfix-typo, experiment-ui).
- Common naming conventions:
- feature/... → for new features
- bugfix/... → for fixing bugs
- hotfix/... → for urgent fixes in production
- experiment/... → for trying out something
Purpose of branching:
- Keep main branch stable.
- Work on tasks/features separately.
- Merge back when ready.
■ Example: feature-login = branch for login feature.