Skip to content

Latest commit

 

History

History
141 lines (96 loc) · 3.04 KB

File metadata and controls

141 lines (96 loc) · 3.04 KB

Lab 5: Cherry-Picking Commits

In this lab, you're going to use git cherry-pick to move commits between branches.

Scenario

Let's "accidentally" commit some changes to main, then cherry-pick them into a feature branch and remove the commits from the default branch.

Task 1: Create a Feature Branch

  1. Ensure you are on the main branch

    git checkout main
  2. Create a new branch called feature/new-game

    git branch feature/new-game

Note

The difference between git branch and git checkout -b is that the former only creates the branch, while the latter creates the branch and checks it out.

Task 2: Add a Feature

  1. Open src/keyboard_input_manager.ts

  2. Locate the comment // Lab 5: New Game Button

  3. Add the following code below the comment

    // Lab 5: New Game Button
    KeyboardInputManager.bindButtonPress(
      '.restart-button',
      KeyboardInputManager.restart
    )
  4. Save the file

Task 3: Commit the Changes

  1. Add the changes to the staging area

    git add src/keyboard_input_manager.ts
  2. Commit the changes

    git commit -m "Add new game button listener"

At this point, the main branch has a new commit that is meant for the feature/new-game branch!

Task 4: Cherry-Pick the Commit

  1. Get the commit SHA of the most recent commit on the main branch

    git log --oneline -n 1

    Copy the SHA of the commit.

  2. Switch to the feature/new-game branch

    git checkout feature/new-game
  3. Cherry-pick the commit from the main branch

    Replace COMMIT_SHA with the SHA of the commit you copied in the first step.

    git cherry-pick COMMIT_SHA

Task 5: Remove the Commit from the Default Branch

  1. Switch back to the main branch

    git checkout main
  2. Reset main to the commit before the new game button listener

    git reset --hard HEAD~1
  3. Verify that latest commit on main is no longer the new game button listener update

    git log --oneline -n 1

Task 7: Merge the feature/new-game Branch

  1. Merge the feature/new-game branch into the main branch

    git merge feature/new-game
  2. Push your changes to GitHub

    git push
  3. Navigate to your repository on GitHub.com

  4. Click the Actions tab

  5. Click the running Deploy to GitHub Pages workflow

  6. Wait for the workflow run to complete

  7. Click the Code tab

  8. Click the link to your game

  9. Verify the New Game button now restarts the game

Need Help?

If you're having trouble with any of the steps, you can ask for help in the meeting chat.

The code changes for this lab can be found in the solutions directory.