Marcy Lab School Docs
  • Welcome
  • Student Guidelines & Policies
    • Student Handbook
    • AI Policy
    • Academic Calendar
  • Environment Setup
    • Local Environment Setup - Mac
    • Local Environment Setup - Windows
    • GitHub Setup
    • Postgres Setup
  • How-Tos
    • How To Code at Marcy: Code Style Guide
    • How to Do Short Response and Coding Assignments
    • How to Debug
    • How to PEDAC
    • How to Create A GitHub Organization and Scrumboard
    • How to Create Projects with Vite
    • How to Deploy on GitHub Pages
    • How to Deploy on Render
    • How to Test your API with Postman
  • Mod 0 - Command Line Interfaces, Git, and GitHub
    • Overview
    • 1. Command Line Interfaces
    • 2. Git & GitHub
    • 3. Git Pulling & Merging
    • 4. Git Branching & PRs
  • Mod 1 - JavaScript Fundamentals
    • Overview
    • 1. Intro to Programming
    • 2. Errors
    • 3. Node & Node Modules
    • 4. Variables, Functions & String Methods
    • 5. Control Flow, typeof, and Math
    • 6. Loops
    • 7. Arrays
    • 8. Objects
    • 9. Higher Order Functions: Callbacks
    • 10. Higher Order Functions: Array Methods
    • 11. Regex
  • Mod 2 - HTML, CSS & the DOM
    • Overview
    • 1. HTML
    • 2. CSS
    • 3. Accessibility (a11y)
    • 4. The Document Object Model (DOM) API
    • 5. Events
    • 6. Forms
    • 7. The Box Model and Positioning
    • 8. Flexbox
    • 9. Grid & Media Queries
    • 10. ESModules
    • 11. Vite
    • 12. LocalStorage
  • Mod 3 - Async & APIs
    • Overview
    • 1. Promises
    • 2. Fetch
    • 3. Building a Fetching App
    • 4. Async & Await
    • 5. A Generic Fetch Handler
  • Mod 4 - Project Week!
    • Important How Tos and Guides
      • How to Create a GitHub Organization and Scrum Board
      • How To Start a Project with Vite
      • How To Deploy a Project with GitHub Pages
    • Project Week Overview
    • Agile Methodologies
    • Deliverables & Milestones
    • Technical Requirements Checklist
    • Free API List
    • Collaborative GitHub
  • Mod 5 - Object-Oriented Programming
    • Overview
    • 1. Intro to OOP, Encapsulation, Factory Functions, and Closure
    • 2. Classes
    • 3. Private & Static
    • 4. UML Diagrams & Has Many/Belongs To Relationships
    • 5. Challenge: Implementing Has Many/Belongs To
    • 6. Inheritance
    • 7. Polymorphism
    • 8. Review and Practice
    • MDN: Object Prototypes
  • Mod 6 - Data Structures & Algorithms
    • Overview
    • Important How Tos and Guides
      • How to Debug
      • How to PEDAC
    • 1. Nodes & Linked Lists
    • 2. Singly & Doubly Linked Lists
    • 3. Stacks & Queues
    • 4. Recursion
    • 5. Trees
  • Mod 7 - React
    • Overview
    • Important How Tos and Guides
      • How to Create Projects with Vite
      • How to Deploy on GitHub Pages
    • 1. Intro to React
    • 2. Events, State, and Forms
    • 3. Fetching with useEffect
    • 4. React Router
    • 5. Building a Flashcards App
    • 6. React Context
    • 7. Global Context Pattern
  • Mod 8 - Backend
    • Overview
    • Important How Tos and Guides
      • How to Deploy on Render
      • How to Test your API with Postman
      • Postgres Setup
    • 1. Intro to Express
    • 2. Building a Static Web Server with Middleware
    • 3. Securing API Keys and Environment Variables
    • 4. RESTful CRUD API
    • 5. Model-View-Controller Architecture
    • 6. SQL and Databases
    • 7. JOIN (Association) SQL Queries
    • 8. Knex
    • 9. Your First Fullstack App!
    • 10. Migrations & Seeds
    • 11. Schema Design & Normalization
    • 12. Hashing Passwords with Bcrypt
    • 13. React Express Auth Template Overview
  • Mod 9 - Civic Tech Hackathon
    • Overview
    • Rubric
  • Mod 10 - Capstone
    • Overview
Powered by GitBook
On this page
  • Slides
  • Overview
  • Intro to Branches
  • Keep the Main Branch Stable
  • Commands for Working with Branches
  • Pull Requests and Code Review
  • Don't Forget to Pull!
  • How Teams Collaborate Using Branches
  • Summary
  1. Mod 0 - Command Line Interfaces, Git, and GitHub

4. Git Branching & PRs

Previous3. Git Pulling & MergingNextMod 1 - JavaScript Fundamentals

Last updated 8 months ago

Slides

Overview

GitHub enables developers across the world to collaborate on projects. In this lesson, we'll learn how to use GitHub to create and manage branches, merge branches, create pull requests, and resolve merge conflicts.

Objectives

You will be able to…

  • Define the terms "branch" as it relates to git

  • Create a branch through the Github GUI and the CLI.

  • Create a pull request.

  • Resolve merge conflicts through the Github GUI

  • Fork a repository.

Key Terms

  • Main Branch — The main branch of a repository. Whenever anyone visits a repository on GitHub or clones it down, this is what they will see.

  • Feature Branch — a copy of a repository at a point in time that allows developers to work on a feature without impacting the rest of the project.

  • Merge - to combine the commit history of two or more branches into one.

  • Pull Request — a request for another developer to pull down your branch and review your code. If they approve the changes, they will merge your branch into the main branch!

  • Fork — a copy of a repository that is disconnected from the main repository. Typically they include the entire commit history of the main repository at the time the fork was created.

Important Git commands

Note: In the commands below, argument placeholders will be written like this: <argument>. When using these commands, replace the <argument> with your desired inputs, making sure to leave out the <> as well.

git branch # see all branches in the local repository
git branch <branch_name> # create a new branch
git checkout <branch_name> # switch to a branch
git checkout main # switch to the main branch
git merge <branch_name> # merge a branch into the current branch
git branch -D <branch_name> # delete a branch 

Intro to Branches

Every repository's commit history has what is called a "main branch" or "trunk". Whenever anyone visits a repository on GitHub or clones it down, this is what they will see.

Up until now, we've been pushing our commits directly to the main branch. But as they say...

Q: Why might pushing to main be a bad idea?

As mentioned above, whenever someone views the repository or clones it down, they will see the main branch. If we were to push changes to the main branch and those changes included mistakes (bugs), other developers would see those mistakes! And, well, that just wouldn't look very good.

If instead we could save our changes somewhere else until were sure that everything worked, that would be much better!

Keep the Main Branch Stable

Rather than pushing our code to the main branch, we can create a separate feature branch!

A feature branch is a copy of the main branch at a point in time and allows developers to work on a feature without impacting the main branch. Branches can be saved on GitHub and are only merged into the main branch when a feature is completed.

Branching ensures that the main branch is always a "stable" version. That is, if someone were to look at the repo, they can look at the main branch and know that it is fully functional. Every merge represents a successful addition of a new feature.

Q: How is branching similar to cloning?

When the repository is cloned, the local clone can be edited without affecting the remote repository, just like a feature branch can be edited without affecting the main branch.

Pushing commits in the local repository to the remote repository is similar to merging a feature branch with the main branch.

Commands for Working with Branches

In order to use branches in a project, we will introduce the following commands, typically in this order:

git branch # see all branches in the local repository
git branch <branch_name> # create a new branch
git checkout <branch_name> # switch to a branch

# make changes, add, commit, push
# this will add your branch to GitHub
# you may need to set an "upstream" remote branch for your branch

Use the -b flag with git checkout to create a new branch and switch to it in one line!

git checkout -b <branch_name>

Try these commands out for yourself! After you push your branch to GitHub, you should be able to see the new commit on the main branch as well as the new branch in the list of branches on GitHub.

Pull Requests and Code Review

Now we have our code on GitHub in two branches: main and our new feature branch.

In the professional world, before we merge ANYTHING into the main branch, there is an additional step called Code Review in which another developer (typically an Engineering Manager) reviews new code and either accepts or rejects the changes.

After pushing an updated branch to GitHub, a notification will appear on the repository page with a call to action: "Compare and pull request".

A pull request (PR) is a request for another developer to review their branch and either merge the branch or request modifications.

When creating a PR, include a description of the changes so that reviewers can quickly provide feedback.

The Files Changed tab lets you see the lines of each file that were added, removed, or modified. You can also leave comments and provide a detailed review.

If everything looks good, you can merge the pull request directly on GitHub!

After merging, it is up to you if you want to delete the branch or keep working on it.

Don't Forget to Pull!

A common step that developers of all levels forget is to return to their local repository and pull down the latest changes from GitHub.

git checkout main # switch to the main branch
git pull # download the latest commits from GitHub

This is a good practice to perform whenever you start a new programming session to ensure your main branch is always up to date.

How Teams Collaborate Using Branches

When working on a project on your own, using branches is a good idea, but not always required. For example, if a project is just a personal project and you're not concerned about other people seeing a broken main branch, you can get away with pushing to main.

However, when working on teams, branching is a must. It does add some complexity to the process but the protection of the main branch is of the utmost importance.

Check out the process here:

  • We're now dealing with two local repositories, each with their own branch (we'll call them feature-x and feature-y).

  • The process for the first developer is mostly the same as if they were working solo!

    1. git checkout -b feature-x to create and switch to a new feature branch.

    2. Make changes, add, commit, and push their branch to GitHub.

    3. Make a PR and merge it!

    4. Return to your local repository and git pull.

  • The second developer needs to do a bit more. They will perform the same first two steps. Then, they will need to:

    1. git checkout main and git pull to download the latest commits from GitHub.

    2. git checkout feature-y and git merge main to update their feature branch's commit history.

    3. It is possible that merge conflicts occurred so resolve them and add, commit, and push!

    4. Make a PR and merge it!

    5. Return to your local repository and git pull.

  • The first developer can now git checkout main and git pull to download the latest commits from GitHub.

Q: Why does Developer 2 first merge the main branch into the feature-y?

This is all for the sake of ensuring our managers approve our PR! A PR with merge conflicts is unlikely to be approved.

When we merge the main branch into the feature-y branch, we deal with any merge conflicts in the feature branch before pushing our code to GitHub and making a PR

This way, our PR will not have any merge conflicts and can be seamlessly merged!

Summary

In this lesson, we learned how to create and manage branches in a project, and how to create a pull request!

The diagram below illustrates the entire process:

There are 10 steps in this process:

  1. Clone or Pull from GitHub

  2. Create a new local branch

  3. Make changes

  4. Stage/Add those changes

  5. Commit

  6. Pull and handle merge conflicts

  7. Push the branch to GitHub

  8. Open a Pull Request (PR)

  9. Get the PR reviewed and approved

  10. Merge the PR!

Slides
Overview
Intro to Branches
Keep the Main Branch Stable
Commands for Working with Branches
Pull Requests and Code Review
Don't Forget to Pull!
How Teams Collaborate Using Branches
Summary
When you clone a repository, you will clone the main branch
One does not simply push to main
A view of your local repository with a main branch and a feature branch. In this example, the feature branch is for creating a lock screen gallery.
You can see your branches listed on GitHub
When a branch is pushed to GitHub, a notification will appear asking you to create a pull request
Descriptions of the key changes help reviewers quickly provide feedback
The Files Changed tab lets you see the exact lines of each file that were added, removed, or modified.
If everything looks good, you can merge the pull request directly on GitHub!
A diagram showing the 6 steps to working collaboratively on git: Clone/Pull; Create Branch; Stage/Add; Commit; Push and Make a Pull Request; Resolve and Merge