The Professional Git Workflow
In a professional setting, we don't just commit code to the main branch and hope for the best. We follow a specific Workflow to ensure the code is tested, reviewed, and safe.
At CodeHarborHub, we teach the Feature Branch Workflow, which is used by top tech companies globally.
1. The Standard Cycle
A typical day in the life of a developer follows these 5 steps:
- Pull: Get the latest code from the team (
git pull origin main). - Branch: Create a new "workspace" for your specific task.
- Work: Write code and make small, frequent commits.
- Push: Upload your branch to GitHub.
- Merge: Open a Pull Request to bring your changes into the main project.
2. Step-by-Step Breakdown
Step 1: Start from the "Truth"
Before starting new work, always make sure your local main branch matches the one on GitHub.
git checkout main
git pull origin main
Step 2: The Feature Branch
Never work directly on main. If you break something on a branch, the main website stays safe.
git checkout -b feature/add-login-button
Step 3: Atomic Commits
An "Atomic Commit" means saving one small, logical change at a time.
- Bad:
git commit -m "Fixed 10 bugs and added a logo" - Good:
git commit -m "Fix: corrected padding on mobile navbar"
Step 4: Syncing & Pushing
Once your feature is ready, send it to the cloud.
git push origin feature/add-login-button
3. The Pull Request (PR)
The Pull Request is a "request" for your teammates to look at your code before it becomes permanent. On GitHub, this is where:
- Teammates leave comments or suggestions.
- Automated tests check if your code breaks anything.
- The Project Manager (or you) eventually clicks "Merge".
4. Resolving Conflicts
Sometimes, two developers change the same line of the same file. When you try to merge, Git gets confused and asks: "Which version should I keep?"
This is called a Merge Conflict.
- Identify
- Solve
- Finish
Git will mark the file with <<<<<<< HEAD and >>>>>>> branch-name.
Open the file in VS Code. Choose the "Current Change," the "Incoming Change," or keep both!
After fixing the file, git add and git commit to tell Git the conflict is resolved.
Practice: The Team Simulation
Try this workflow even if you are working alone:
- Create a branch:
git checkout -b feature/update-readme. - Add a new line to your
README.mdabout CodeHarborHub. - Commit it:
git commit -m "Docs: added project description". - Push it:
git push origin feature/update-readme. - Go to GitHub and open a Pull Request.
- Merge it into
mainand delete your feature branch!
Use prefixes for your branch names to keep things organized:
feature/for new things.fix/for bug fixes.docs/for documentation changes.refactor/for cleaning up code without changing what it does.
Once you merge a Pull Request on GitHub, your local main is now outdated. Remember to git pull origin main again before starting your next feature!