How to Use Pull Request Workflow in Git: Step-by-Step Guide
A
pull request workflow in Git lets you propose changes from a feature branch to the main branch by creating a pull request on a platform like GitHub. Team members review the changes, discuss, and approve before merging, ensuring safe collaboration.Syntax
The pull request workflow involves these main steps:
- Create a feature branch: Work on your changes isolated from the main branch.
- Push the branch to remote: Upload your branch to the shared repository.
- Create a pull request: Propose your changes for review and merging.
- Review and merge: Team reviews the code, then merges if approved.
bash
git checkout -b feature-branch # Make changes and commit git push origin feature-branch # On GitHub/GitLab/Bitbucket, create a pull request from 'feature-branch' to 'main' # After review, merge the pull request
Example
This example shows creating a feature branch, pushing it, and opening a pull request on GitHub.
bash
git checkout -b add-login-feature # Edit files to add login feature git add . git commit -m "Add login feature" git push origin add-login-feature # Then go to GitHub and create a pull request from 'add-login-feature' to 'main'
Output
Switched to a new branch 'add-login-feature'
[add-login-feature 1a2b3c4] Add login feature
3 files changed, 45 insertions(+), 2 deletions(-)
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 350 bytes | 350.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
Common Pitfalls
Common mistakes when using pull request workflow include:
- Working directly on the main branch instead of a feature branch.
- Not pulling the latest changes before creating a pull request, causing merge conflicts.
- Skipping code review or merging without approval.
- Not writing clear commit messages or pull request descriptions.
bash
## Wrong: working on main branch directly git checkout main # make changes git commit -m "Fix bug" git push origin main ## Right: use feature branch git checkout -b fix-bug # make changes git commit -m "Fix bug" git push origin fix-bug # create pull request from 'fix-bug' branch
Quick Reference
| Step | Command / Action | Description |
|---|---|---|
| 1 | git checkout -b feature-branch | Create and switch to a new feature branch |
| 2 | git add . && git commit -m "message" | Stage and commit your changes |
| 3 | git push origin feature-branch | Push branch to remote repository |
| 4 | Create pull request on GitHub/GitLab | Open a pull request for review |
| 5 | Review and merge | Team reviews and merges the pull request |
Key Takeaways
Always create a separate feature branch before making changes.
Push your branch to the remote repository before creating a pull request.
Use pull requests to get code reviewed and approved before merging.
Keep commit messages and pull request descriptions clear and descriptive.
Pull the latest main branch changes to avoid merge conflicts.