Review a Github pull request locally

Objective: Learn how to effectively review and manage pull requests on your local machine

Initial Setup and Review

  • Make sure you have direct repository access before doing the following
    git fetch origin                    # Download information about all remote branches 
    git switch origin/branch-name     # Switch to the specific PR branch (Detached HEAD state)
  • If you want to make changes while reviewing, create a local branch
    git switch -c my-review-branch origin/branch-name
  • To go back to your previous work
    git switch main                   # Or whatever branch you were on before

After the PR is Merged

  • Update your local main branch with the merged changes
    git switch main                   # Switch to main branch
    git fetch origin                    # Get latest remote information
    git pull origin main                # Update local main with remote changes
                    
  • Clean up by removing your review branch if you created one
    git branch -d my-review-branch      # Delete the local review branch safely

Managing Local and Remote Branches

  • View all your branches
    git branch                          # List local branches
    git branch -r                       # List remote branches
    git branch -a                       # List both local and remote branches
    git branch -av                      # Show detailed information for all branches
  • Find out which branches have been merged
    git branch --merged                 # Show branches merged into current branch
    git branch --no-merged              # Show unmerged branches
  • Clean up remote branch references
    git fetch --prune                   # Remove references to deleted remote branches

Keeping Your Working Branch Updated

  • For shared branches in PRs, use merge. It preserves history but adds complexity.
    git switch main
    git pull origin main
    git switch your-branch-name
    git merge main
  • For personal branches, used rebase. It creates a cleaner history by rewriting it.
    git switch main
    git pull origin main
    git switch your-branch-name
    git rebase main

Key Points to Remember

When reviewing pull requests locally, remember these important practices:

  • Always start with a fetch to ensure you have the latest remote information
  • Don't worry about being in a detached HEAD state - it's normal when checking out remote branches directly
  • Create a local branch if you need to make changes during review
  • Clean up your local branches after PRs are merged to keep your repository tidy
  • Use merge for shared branches and rebase for personal work
  • Run git fetch --prune periodically to clean up references to deleted remote branches