- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Guided: Branching and Merging in Git
In this Guided Code Lab, you will master the use of branches in Git, crucial for managing features and collaborative development. You'll work within a cloned-repo on a simple Python word-guessing game called PyWords. During the lab, you will create a new branch to develop a feature and then merge your changes back into the main branch.
Lab Info
Table of Contents
-
Challenge
Introduction
Introduction
Welcome to the Guided: Branching and Merging in Git lab 🌿.
In this Guided Code Lab, you will master the use of branches in Git, crucial for managing features and collaborative development. You'll work within a
cloned-repoon a simple Python word-guessing game called PyWords. During the lab, you will create a new branch to develop a feature and then merge your changes back into the main branch.Pre-requisites
- Understanding of Python programming
- Familiarity with command-line operations and basic Git commands
Learning Objectives
- Create and switch between branches in a Git repository.
- Implement a new feature in a separate branch.
- Merge the new feature into the main branch without conflicts.
Click the arrow to move to the next step.
-
Challenge
Basic Setup and Configuration
Before you dive into the core activities of this lab, it's essential to ensure that your Git environment is correctly set up. This involves configuring Git with your personal information, which will be used in your commits. Each commit in Git is associated with a name and an email address, and this information is crucial for collaborative work and maintaining a clear history of who did what.
Setting Up Your Git Identity
Configure Your Email Address
First, you'll need to set the email address that will be associated with your Git commits. This should be the same email address that you use for your Git repository service (like GitHub, GitLab, etc.). To set your email address globally in Git, use the following command:
git config --global user.email "[email protected]"Replace
[email protected]with your actual email address.Configure Your Username
Next, set your name. This name will appear as the author in each commit you make. To set your name globally in Git, use the command:
git config --global user.name "Your Name"Replace
Your Namewith the name you want to appear in your commits.Why is this Important?
- Commit Tracking: Git tracks who makes each commit by using the name and email address. This is crucial in a collaborative environment where multiple people are working on the same project.
- Accountability and Credit: Your name and email in commits help in attributing the work to you, ensuring you get credit for your contributions.
- Consistency Across Environments: By setting these configurations globally, your Git identity remains consistent across all the repositories you work on your machine.
After completing these steps, you'll be ready to start using Git more effectively, with your identity properly established for all your contributions. This is a fundamental step in setting up a professional and organized coding environment.
Click the arrow to move to the next step.
-
Challenge
Hands-On: Working with Git
Let's start by cloning the
remote-repowhich is where you will be adding your new code.In the Terminal to your right execute:
git clone remote-repo cloned-repoThis command clones the
remote-repointo your working directory and names itcloned-repo.Change into your new repo with the following command:
cd cloned-repoCheck the status of your new repo with:
git statusYou should see the following message:
On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree cleanClick the arrow to move to the next step.
-
Challenge
Branching in Git
Branches in Git allow you to diverge from the main development line and work on new features or fixes in isolation.
Creating a New Branch in cloned-repo
Inside
cloned-repo, create a new branch namedfeature-hint-system:git branch feature-hint-systemSwitch to the new branch:
git checkout feature-hint-systemConfirm you have switched to the new branch:
git branchYou should see the following:
cloned-repo $ git branch * feature-hint-system masterClick the arrow to move to the next step.
-
Challenge
Implementing a New Feature in cloned-repo
With your new branch checked out, you will add a feature to PyWords that provides a hint after the third failed attempt.
Adding the Feature
Replace the code inside
main.pywith the following, simulating the coding of a new feature:import random words = ["apple", "bloom", "crane", "dwell", "eagle", "frost", "glade", "haste", "ideal", "jolly", "kneel", "light", "mound", "noble", "ocean", "piano", "quilt", "rally", "scale", "trace", "ultra", "vivid", "whale", "xenon", "yacht", "zesty"] def get_feedback(secret_word, guess_word): feedback = "" for i, c in enumerate(guess_word): if c == secret_word[i]: feedback += c.upper() elif c in secret_word: feedback += c.lower() else: feedback += "*" return feedback def provide_hint(secret_word, guesses): # Find letters that haven't been guessed correctly unguessed = [c for c in secret_word if c not in guesses or secret_word.index(c) != guesses.index(c)] if unguessed: return f"Hint: One of the letters is '{random.choice(unguessed)}'." return "No hints available." secret_word = random.choice(words) guessed_letters = "" print("Welcome to PyWords!") print("Guess the 5-letter word. You have 6 attempts.") for attempt in range(1, 7): guess = input(f"Attempt {attempt}/6: Enter your guess: ").lower() while len(guess) != 5 or not guess.isalpha(): print("Invalid input. Please make sure you enter a 5-letter word.") guess = input(f"Attempt {attempt}/6: Enter your guess: ").lower() guessed_letters += guess if guess == secret_word: print(f"Congratulations! You've guessed the word: {secret_word.upper()}") break else: feedback = get_feedback(secret_word, guess) print(f"Feedback: {feedback}") if attempt == 3: print(provide_hint(secret_word, guessed_letters)) if attempt == 6: print(f"Sorry, you've run out of attempts. The word was: {secret_word.upper()}")Make sure to save the
main.py.Committing Your Feature
Inside
cloned-repo, stage your changes:git add .Commit your changes with a descriptive message:
git commit -m "Implement hint feature after third failed attempt"You should see the following:
[feature-hint-system 8d2545f] Implement hint feature after the third failed attempt 1 file changed, 17 insertions(+), 3 deletions(-)Click the arrow to move to the next step.
-
Challenge
Merging Your Changes in cloned-repo
Now that your feature is ready, it's time to merge your changes back into the master branch.
Preparing to Merge
Inside
cloned-repo, switch back to the master branch:git checkout masterYou should see:
Switched to branch 'master' Your branch is up to date with 'origin/master'.Merge the
feature-hint-systembranch intomaster:git merge feature-hint-systemYou should see:
Updating b834b0a..8d2545f Fast-forward main.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)Before pushing your changes to a remote repository, it's a good practice to pull the latest changes to avoid conflicts.
Syncing with Remote
Inside
cloned-repo, pull the latest changes from the remote repository:git pull origin masterSince you are already up to date you should see:
* branch master -> FETCH_HEAD Already up to date.Push your merged changes to the remote repository:
git push origin masterYou should see a similar message:
Enumerating objects: 5, done. Counting objects: 100% (5/5), done. Delta compression using up to 4 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 595 bytes | 595.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0📚 Summary and Next Steps
Congratulations! You've successfully navigated the branching and merging features of Git. You've added a new feature to PyWords and integrated it into the main codebase.
đź’ˇ Next Steps for Practice:
- Inside
cloned-repo, create a new branch for additional features or fixes. - Explore merging branches with conflicts and practice resolving them.
- Familiarize yourself with Git's branching strategies and workflows.
Keep experimenting with these concepts, and consider contributing to open-source projects to further develop your skills in a real-world environment.
- Inside
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.