Introduction
Why We Should Not Push Directly to the main Branch?
Pushing code directly to the main branch is not recommended due to the following reasons:
โ ๏ธ Risk of Bugs and Instability
- The
mainbranch is typically the stable branch, often used for production or shared across the team. - Directly pushing untested code increases the risk of breaking the codebase.
๐ No Code Review
- Direct pushes bypass the code review process, which is crucial for:
- Catching errors
- Improving code quality
- Ensuring adherence to standards
๐ Merge Conflicts
- When multiple developers push to the
mainbranch, it increases the chance of merge conflicts.
๐ซ Lack of Testing
- Direct pushes skip automated tests (if configured for pull requests).
๐ ๏ธ Difficulty in Debugging
- Without isolated feature branches, tracking the source of issues becomes more challenging.
๐ No Documentation of Changes
- Pull Requests (PRs) provide a space to document and discuss changes.
๐ฅ Risk of Breaking Deployments
- If
mainis tied to CI/CD pipelines, direct pushes can introduce unstable code into production.
๐ฅ No Accountability
- The PR review process ensures peer-reviewed changes, increasing confidence in the codebase.
โ Recommended Git Merge Workflow
๐ฆ Step 1: Create a Development Branch
- Use a feature branch to isolate your changes:
git checkout -b feature/branch-name
- Naming convention: Use meaningful names like:
feature/add-loginbugfix/fix-typo
๐ ๏ธ Step 2: Work on the Feature
- Make changes and commit frequently with clear messages:
git add .
git commit -m "Add login functionality"
๐ค Step 3: Push the Branch to Remote
- Push the branch to remote for collaboration:
git push origin feature/branch-name
๐ Step 4: Open a Pull Request (PR)
- Create a PR from the feature branch to the
mainbranch in your Git hosting service. - Add a detailed description of the changes, reasons, and context.
- Ensure automated tests pass before requesting a review.
๐ง Step 5: Wait for Code Review
- Team members will review your PR and suggest changes.
- Make requested changes and commit them:
git add .
git commit -m "Address PR comments"
git push origin feature/branch-name
โ Step 6: Merge the Pull Request
- After approval and passing all tests, merge the PR into
main. - Optional: Use a squash merge or rebase for a cleaner commit history.
๐ฅ Step 7: Pull the Updated main Branch Locally
- Update your local
mainbranch:
git checkout main
git pull origin main
๐๏ธ Step 8: Delete the Development Branch
- Remove the branch from the remote repository:
git push origin --delete feature/branch-name
- Delete the branch locally:
git branch -d feature/branch-name
๐งน Step 9: Prune Deleted Branches
- Clean up references to deleted branches:
git fetch -p
๐ฏ Conclusion
By following this structured Git merge workflow, you:
- Enhance code quality
- Promote collaboration
- Prevent breaking changes
- Maintain a clean commit history