📝
marcyannotes
  • Welcome
  • Student Guidelines & Policies
    • Student Handbook
    • AI Policy
    • Academic Calendar
  • Environment Setup
    • Local Environment Setup - Mac
    • Local Environment Setup - Windows
    • GitHub Setup
    • Postgres Setup
  • Fullstack Software Engineering Curriculum
    • Overview
    • 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 Projects with Vite
      • How to Deploy on GitHub Pages
      • How to Deploy on Render
    • Mod 0 - Command Line Interfaces, Git, and GitHub
      • Mod 0 Overview
      • Command Line Interfaces
      • Git & GitHub
      • Git Pulling & Merging
      • Git Branching & PRs
      • Pair Programming: BONUS
    • Mod 1 - JavaScriptFundamentals
      • Mod 1 Overview
      • Intro to Programming
      • Errors
      • Node & Node Modules
      • Variables, Functions & String Methods
      • Control Flow, typeof, and Math
      • Loops
      • Arrays
      • Objects
      • Higher Order Functions: Callbacks
      • Higher Order Functions: Array Methods
      • Regex
    • Mod 2 - HTML, CSS & the DOM
      • Mod 2 Overview
      • HTML
      • CSS
      • Accessibility (a11y)
      • The DOM
      • Events
      • Forms
      • The Box Model and Positioning
      • Flexbox
      • Grid & Media Queries
      • ESModules
      • LocalStorage
    • Mod 3 - Async & APIs
      • Mod 3 Overview
      • Promises
      • Fetch
      • Building a Fetching App
      • Async & Await
    • Mod 4 - Project Week!
      • Project Week Overview
    • Mod 5 - Object-Oriented Programming
      • Mod 5 Overview
      • Intro to OOP, Encapsulation, Factory Functions, and Closure
      • Classes
      • Private & Static
      • Has Many/Belongs To
      • Polymorphism
    • Mod 6 - Data Structures & Algorithms
      • Mod 6 Overview
      • Stacks & Queues
      • Nodes & Linked Lists
      • Singly & Doubly Linked Lists
      • Recursion
      • Trees
    • Mod 7 - React
      • Mod 7 Overview
      • Intro to React
      • Events, State, and Forms
      • Fetching with useEffect
      • Building a Flashcards App
      • React Context
      • Global Context Pattern
      • React Router
    • Mod 8 - Backend
      • Mod 8 Overview
      • Intro to Express
      • Building a Static Web Server with Middleware
      • Securing API Keys with Environment Variables
      • Building a RESTful API with MVC
      • SQL and Databases
      • JOIN (Association) SQL Queries
      • Knex
      • Your First Fullstack App!
      • Migrations & Seeds
      • Schema Design & Normalization
      • Hashing Passwords with Bcrypt
  • Code Challenge Curriculum
    • Unit 0
      • Lecture: Functions in JS
      • CC-00: Functions and Console Logs
      • CC-01: Conditionals
      • CC-02: Conditionals 2
    • Unit 1
      • CC-03: For Loops
      • CC-04: For Loops and Conditionals
      • CC-05: For Loops and Conditionals 2
    • Unit 2
      • CC-06: String Mutations
      • CC-07: Array Iteration
      • CC-08: String Mutation and Array Iteration
      • CC-09: Array Mutations
      • CC-10: Reading Objects
      • CC-11: Objects
      • CC-12: Objects
      • Unit 2 Diagnostic
    • Unit 3
      • Intro to PEDAC (and Algorithms)
      • validTime
      • fizzBuzz (array)
      • digitSumDifference
      • firstNotRepeating
      • compareEvenAndOddSum
      • countVowelConsonants
      • finalHP
      • canMakeTriangle
    • Unit 4
    • Unit 5
    • Unit 6
    • Unit 7
    • Unit 8
    • Sorting
Powered by GitBook
On this page
  • Overview
  • Collaborating on a GitHub Repository
  • git pull synchronizes local repositories
  • Adding a Collaborator
  • Creating a Merge Conflict
  • Why Can't I Push?
  • Handling Merge Conflicts
  • Tips for success
  1. Fullstack Software Engineering Curriculum
  2. Mod 0 - Command Line Interfaces, Git, and GitHub

Git Pulling & Merging

PreviousGit & GitHubNextGit Branching & PRs

Last updated 8 months ago

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…

  • Pull changes from a repository

  • Resolve merge conflicts

Key Terms

  • Pull — to download changes from a remote repository

  • Merge - to combine two or more branches into one

  • Merge Conflict — a situation in which two or more branches need to be merged but have modified the same lines of code, causing the merge to fail. This happens all the time and can be resolved through the Github GUI or the CLI.

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 pull — download changes from a remote repository

Collaborating on a GitHub Repository

An app published to the App Store (or Play Store, I see you Android users) is essentially the same as a repository uploaded to GitHub.

Just like downloading an app, anyone can download a repo from GitHub using the git clone command.

To demonstrate this, find a partner (or simply play the role of two developers). Then do the following:

  1. Have one developer create a new repo called git-pulling-practice. Make sure it includes a README.md file. Then, share the URL with their partner.

  2. Have both developers use their VS Code Terminal to navigate to their unit-0 folder and clone the repo onto their computers using git clone

    • If working solo, clone the repo a second time but rename the second copy. You can do this by adding an additional input to the git clone command for the new name, resulting in something like this: git clone <git_repo_url> git-pulling-practice-copy.

  3. Have both developers navigate into the repo using the cd command.

    • If working solo, open two VS Code Terminal windows for this, navigating each terminal to one of the copies of the repo so that you can easily switch back and forth between "developer 1" (the first clone) and "developer 2" (the second clone).

Tada! Now you have the same repository on two computers!

git pull synchronizes local repositories

In the last lesson, you learned about the git push command which uploads a local repository's commits to the remote repository (GitHub).

git pull does the opposite. If the local repository (on your computer) is missing commits that are on the remote repository (on GitHub), git pull will download those commits.

Let's put this into practice. With your partner do the following:

  1. Have the developer who first created the repo on GitHub make some edits to README.md.

  2. That same developer should then stage their changes (git add README.md), commit (git commit -m "description of commit") and then push that commit (git push)!

  3. Double check on GitHub that the commit shows up on the remote repository's commit history.

  4. Now, the second developer should run the command git pull (if working solo, switch over to the location of the second copy of the repo and run git pull)

By storing repositories remotely on GitHub, any number of developers can have access to a single repository. This push and pull mechanism is essential for collaboration.

Adding a Collaborator

So far only one developer was making changes, while the other developer simply "pulled down" those changes. But what if both developers want to work simultaneously?

Before moving on, the owner of the shared repo should add their partner as a collaborator:

  1. Go into the settings of the repo, go to Collaborators, and click on "Add people".

  2. Add their partner as a collaborator using their GitHub username or Email and have their partner accept the invitation.

Creating a Merge Conflict

For this next part, it is going to be a race, so get your typing fingers ready! Both developers should:

  1. Run git pull to make sure that their local repository is in sync with the remote.

  2. Open up the README.md file and replace the content on line 1 with their own name. This is the most important step. It is essential that both developers edit the same line of code

  3. Stage their changes (git add README.md)

  4. Commit (git commit -m "description of commit")

  5. Push (git push)!

  6. Chaos

Whichever developer managed to push their changes first (or has a better internet connection) will have no problem pushing their code.

The developer who pushed second will be given this message:

In case you got an error about configuring pull.rebase, run the following in your CLI:

git config --global pull.rebase false

This makes it so that all conflicts will have to be merged (and reviewed) instead of rebased. The --global flag also sets this config on all other git repos you will/have on your machine!

Why Can't I Push?

You are only allowed to push to the remote repository if your local repository has the exact same commit history as the remote repository.

They could try to "force" their push through by running git push -f but this would delete their partner's commit! Use this command with caution.

In this case, developer 2 is missing the commit from developer 1 in their local repo and needs to pull down the changes.

So, the developer who pushed last should run git pull

However, in this situation, the conflict will cause a Merge Conflict like this:

Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

Handling Merge Conflicts

Okay take a deep breath. Merge conflicts are often scary when they are first encountered, but they happen all the time and are an entirely expected part of the development process!

Merge conflicts occur when two developers edit the same lines of code in the same repo and unsuccessfully merge those changes together, as happened here.

VS Code provides a nice UI to help us resolve the conflict:

Let's break down what we see here:

  • The three "markers" outline the two conflicting pieces of code:

    • <<<<<<< HEAD on line 1

    • ======= on line 3

    • >>>>>>> b737ff... on line 5

  • The "current change" is the code already in my local repo.

  • The "incoming change" is the code that I'm trying to merge into my local repo (from the remote repository).

To resolve this conflict we can:

  1. Use the buttons that VS Code provides:

    • "Accept Current Change" — replace the code from the remote with your code.

    • "Accept Incoming Change" — replace your code with the code from the remote.

    • "Accept Both Changes" — keep both!

    • "Compare Changes" — see the changes side by side

  2. Alternatively, we can just delete the markers and keep the code you want to keep!

  3. Once you've made your choice, save the file, stage the changes, commit them, and push the changes.

    • Typically, you can use the commit message "resolving merge conflicts"

Hurrayy!!! You've solved the merge conflict! Piece of cake.

Tips for success

  • First, check out our cheat sheet on Git

  • Pay attention to the details and take notes! Do not shy away from the messages in your terminal. Seek to understand them and you'll gain so much more confidence.

Overview
Collaborating on a GitHub Repository
git pull synchronizes local repositories
Adding a Collaborator
Creating a Merge Conflict
Why Can't I Push?
Handling Merge Conflicts
Tips for success
Add collaborators on GitHub
The remote repository has changes that are not in the local repository. Git pull to download those changes
alt text