How to Use Code Review in Git: Step-by-Step Guide
Use
git with a platform like GitHub or GitLab to create a pull request for your changes. Team members review the code by commenting and approving before merging it into the main branch.Syntax
Code review in Git is usually done through pull requests (PRs) on platforms like GitHub or GitLab. The main steps are:
- Create a branch: Make your changes in a separate branch.
- Push the branch: Upload your branch to the remote repository.
- Create a pull request: Request to merge your branch into the main branch.
- Review: Team members comment, suggest changes, and approve.
- Merge: After approval, merge the PR to update the main branch.
bash
git checkout -b feature-branch # Make changes git add . git commit -m "Add new feature" git push origin feature-branch # Then create a pull request on GitHub/GitLab UI
Example
This example shows how to create a branch, push it, and open a pull request for code review 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(+)
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)
To https://github.com/yourrepo/project.git
* [new branch] add-login-feature -> add-login-feature
Common Pitfalls
Common mistakes during code review in Git include:
- Not creating a separate branch for changes, which makes reviews and rollbacks harder.
- Skipping the pull request step and pushing directly to main branch.
- Ignoring review comments and merging without approval.
- Not updating the branch after feedback, causing conflicts.
bash
git checkout main # Wrong: committing directly on main branch # Right: create and use a feature branch # Wrong # git add . # git commit -m "Fix bug" # git push origin main # Right git checkout -b fix-bug git add . git commit -m "Fix bug" git push origin fix-bug # Create PR for review
Quick Reference
| Step | Command/Action | Description |
|---|---|---|
| 1 | git checkout -b | Create a new branch for your changes |
| 2 | git add . | Stage your changes |
| 3 | git commit -m "message" | Commit your changes with a message |
| 4 | git push origin | Push your branch to remote |
| 5 | Create Pull Request | Open a PR on GitHub/GitLab for review |
| 6 | Review & Approve | Team reviews and approves the PR |
| 7 | Merge PR | Merge changes into main branch after approval |
Key Takeaways
Always create a separate branch for your changes before starting a code review.
Use pull requests on platforms like GitHub or GitLab to facilitate code reviews.
Reviewers should comment and approve changes before merging to maintain code quality.
Avoid pushing directly to the main branch to keep history clean and reviewable.
Update your branch with feedback before merging to prevent conflicts.