- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Guided: Advanced Git Techniques
In this lab, you will dive deeper into the world of Git, focusing on advanced techniques essential for managing complex development scenarios. You will be working on two different features for a game, employing Git Stash to efficiently handle your tasks.
Lab Info
Table of Contents
-
Challenge
Introduction
Guided: Advanced Git Techniques
Welcome to the Guided: Advanced Git Techniques lab 🌟.
In this lab, you will dive deeper into the world of Git, focusing on advanced techniques essential for managing complex development scenarios. You will be working on two different features for a game, employing Git Stash to efficiently handle your tasks.
Pre-requisites
- Comfortable with basic Git operations
- Understanding of Git concepts like branching and merging
Learning Objectives
- Use Git Stash to save and restore changes.
- Understand and apply Git Rebase for updating feature branches.
- Manage concurrent feature developments effectively.
Scenario: You're developing two distinct features for your game, needing to switch between them frequently.
-
Challenge
Git Identity Setup
Setting Up Your Git Identity
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.
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.
-
Challenge
Repository Setup
Repository Setup and Branch Creation
Now let's set up our working environment for this advanced Git techniques lab.
1. Clone the Repository
Clone the repository that contains the game project:
git clone game-project my-repo2. Create Feature Branches
Navigate into the repository with
cd my-repoand create two separate branches for your features:git branch feature-one git branch feature-twoThese branches will represent the two different features you'll be working on simultaneously. This setup simulates a real-world scenario where developers often need to juggle multiple features or bug fixes.
-
Challenge
Using Git Stash
Stashing Changes
Now let's use
git stashto save and switch between different states of your work. Git stash is a powerful feature that allows you to temporarily save your work without committing it, enabling you to switch between branches or pull updates without losing your progress.Let's make changes in the
feature-onebranch. Switch intofeature-onewith:git checkout feature-oneNavigate to the file Explorer and open the
my-repo/main.pyfile. Comment out the following lines of code:# 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"]Stash your changes to
feature-oneusing:git stash save "Feature One Work"You should see:
Saved working directory and index state On feature-one: Feature One WorkThis command saves your current work-in-progress to Git's stash, allowing you to return to a clean working directory.
-
Challenge
Switching Between Branches
Switching Branches and Working on Feature Two
Now that you have stashed your changes to
feature-one, you can switch to yourfeature-twobranch:git checkout feature-twoYou should see:
Switched to branch 'feature-two'Notice that your working directory is now clean - the changes you made in
feature-oneare safely stored in the stash.In
main.pyadd the following method that you will finish later:def new_code(): # TODO passLet's add and commit the code with:
git add . git commit -m "Adds new method TODO"You should see:
[feature-two 94a982d] Adds new method TODO 1 file changed, 3 insertions(+)This demonstrates how you can work on completely different features in separate branches while keeping your work organized and separate.
-
Challenge
Restoring Stashed Changes
Restoring Stash
After working on
feature-two, let's switch back and apply our stashed changes tofeature-one:git checkout feature-one git stash popNow you should see:
On branch feature-one Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: main.py no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (5043b2e7c8968e8a37c1c2bfacb9d5f89e5ea717)The
git stash popcommand both applies the stashed changes and removes them from the stash. Your previous work onfeature-oneis now restored exactly as you left it.Let's go ahead and commit these changes with:
git add . git commit -m "Adds comment"You should see:
[feature-one 3020dd0] Adds comment 1 file changed, 1 insertion(+), 1 deletion(-)Congratulations! You've successfully used Git Stash to work on two branches simultaneously. This technique is invaluable when you need to quickly switch context between different features or when you need to pull updates from a remote repository without committing incomplete work.
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.