0
0
Gitdevops~5 mins

Code review in pull requests in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
Code review in pull requests helps teams check and improve code before adding it to the main project. It catches mistakes early and shares knowledge among team members.
When you want someone else to check your code changes before merging them.
When you want to discuss improvements or fixes with your team.
When you want to keep the main project stable by reviewing new code carefully.
When you want to track what changes are being added and why.
When you want to learn from feedback on your code.
Commands
Create and switch to a new branch named 'feature/add-login' to work on your code changes separately from the main branch.
Terminal
git checkout -b feature/add-login
Expected OutputExpected
No output (command runs silently)
Stage all your changed files to prepare them for committing.
Terminal
git add .
Expected OutputExpected
No output (command runs silently)
Save your staged changes with a clear message describing what you did.
Terminal
git commit -m "Add login feature with validation"
Expected OutputExpected
[feature/add-login abc1234] Add login feature with validation 3 files changed, 45 insertions(+), 2 deletions(-)
Upload your branch and commits to the remote repository so others can see and review your changes.
Terminal
git push origin feature/add-login
Expected OutputExpected
Enumerating objects: 10, done. Counting objects: 100% (10/10), done. Delta compression using up to 8 threads Compressing objects: 100% (7/7), done. Writing objects: 100% (7/7), 1.2 KiB | 1.2 MiB/s, done. Total 7 (delta 3), reused 0 (delta 0), pack-reused 0 remote: remote: Create a pull request for 'feature/add-login' on GitHub by visiting: remote: https://github.com/example/repo/pull/new/feature/add-login remote: To https://github.com/example/repo.git * [new branch] feature/add-login -> feature/add-login
Fetch the pull request number 42 from the remote repository and create a local branch 'pr-42' to review the code.
Terminal
git fetch origin pull/42/head:pr-42
Expected OutputExpected
From https://github.com/example/repo * [new ref] refs/pull/42/head -> pr-42
Switch to the local branch 'pr-42' to inspect and test the pull request code.
Terminal
git checkout pr-42
Expected OutputExpected
Switched to branch 'pr-42'
Key Concept

If you remember nothing else from this pattern, remember: use branches and pull requests to safely share and review code before merging.

Common Mistakes
Committing directly to the main branch without creating a feature branch.
This can cause unstable code in the main project and makes it hard to review changes separately.
Always create a new branch for your changes before committing.
Pushing changes without a clear commit message.
It makes it hard for reviewers to understand what was changed and why.
Write clear, concise commit messages describing your changes.
Not fetching and checking out the pull request branch locally before reviewing.
You miss the chance to test and inspect the code properly before approving.
Fetch the pull request branch and check it out locally to review and test.
Summary
Create a new branch to work on your feature or fix.
Commit your changes with clear messages and push the branch to the remote repository.
Fetch and check out pull request branches locally to review and test code before merging.