How To Use GitHub Branches & Pull Request

Corbin BrownAbout 4 min readMay 27, 2025Watch original
THE SUMMARYAI-generated

Key Concepts

  • Branches: Independent lines of development within a Git repository.
  • Main Branch: The primary branch representing the production-ready code.
  • Commits: Snapshots of changes to files in a repository at a specific point in time.
  • Pull Request (PR): A request to merge changes from a branch into another branch (typically main).
  • Merging: Integrating changes from one branch into another.
  • Resetting (Hard Reset): Reverting a branch to a specific commit, discarding subsequent changes.
  • Origin: A remote repository (e.g., on GitHub) that the local repository is connected to.
  • IDE (Integrated Development Environment): A software application that provides comprehensive facilities to computer programmers for software development.
  • Production: The live, deployed version of a website or application.

Branches: Purpose and Creation

  • Purpose: Branches allow developers to work on new features or bug fixes in isolation without affecting the main codebase.
  • Main Branch Protection: Avoid committing directly to the main branch to prevent introducing bugs into the live production code.
  • Creating a Branch:
    • Open a terminal in the code directory (using cd <directory_name> or the IDE's built-in terminal).
    • git branch: Lists existing branches.
    • git checkout -b <branch_name>: Creates a new branch named <branch_name> and switches to it. Example: git checkout -b footer-change.
  • Branch Naming Convention: Use descriptive names that reflect the changes being made (e.g., footer-change for footer modifications).
  • Local vs. Cloud: The new branch is initially created locally.

Committing and Pushing Changes

  • Making Changes: Modify files within the new branch. VS Code indicates local changes with an asterisk.
  • Committing Changes:
    • git add .: Stages all modified files for commit.
    • git commit -m "<commit_message>": Creates a commit with a descriptive message. Example: git commit -m "Add a comment".
  • Pushing to GitHub:
    • git push origin <branch_name>: Pushes the local branch to the remote repository (origin) on GitHub. Example: git push origin footer-change.
  • Pull Request Creation: Pushing a new branch to GitHub automatically creates a pull request.

Pull Requests: Review and Merging

  • Pull Request Purpose: A pull request allows for code review and discussion before merging changes into the main branch.
  • Reviewing Changes: Examine the changes introduced in the pull request.
  • Merging:
    • Click "Merge pull request" on GitHub.
    • Confirm the merge.
    • Optionally delete the branch after merging (can be restored if needed).
  • Verification: After merging, the changes should be reflected in the main branch.

Best Practices for Commits

  • Frequent Commits: Commit changes when you reach a stable and significant point in your work.
  • Descriptive Commit Messages: Write clear and concise commit messages that explain the purpose of the changes. Example: git commit -m "Remove all links in footer".
  • Commits as Checkpoints: Treat commits as save points, allowing you to roll back to previous states if needed.

Rolling Back to Previous Commits

  • Identifying the Commit:
    • Go to the pull request on GitHub.
    • Click on "Commits".
    • Copy the SHA hash of the commit you want to revert to.
  • Hard Reset:
    • git reset --hard <commit_hash>: Resets the branch to the specified commit, discarding all subsequent changes.
    • Example: git reset --hard <SHA hash>.
  • Force Push (if necessary): If you've already pushed the branch to GitHub, you may need to force push after a hard reset: git push --force origin <branch_name>. This overwrites the remote branch with your local version.

Resolving Push Errors After Reset

  • "Failed to push some refs" Error: This can occur after a hard reset because the local branch history diverges from the remote branch.
  • Force Push Solution: Use git push --force origin <branch_name> to overwrite the remote branch with your local branch.

Benefits of Branching and Committing

  • Risk Mitigation: Allows for experimentation and taking risks with code changes, knowing you can easily revert to a previous state.
  • Collaboration: Enables multiple developers to work on different features simultaneously without interfering with each other's work.
  • Scalability: Supports scalability within a workplace environment.

Conclusion

Branches are essential for managing code changes, enabling collaboration, and mitigating risks in software development. By creating branches for new features or bug fixes, developers can isolate their work and prevent introducing errors into the main codebase. Frequent commits with descriptive messages act as checkpoints, allowing for easy rollback to previous states if needed. Pull requests facilitate code review and discussion before merging changes into the main branch, ensuring code quality and stability.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.