Everything you need as a full stack developer

Stashing Changes Temporarily

- Posted in Junior Developer by

TL;DR As a full stack developer, stashing changes temporarily is a vital technique to know when switching between different branches or versions of your codebase. Stashing refers to setting aside uncommitted changes, allowing you to switch to a different branch or commit without losing progress. This technique saves time and reduces stress in your development workflow, especially when addressing urgent issues or experimenting with new ideas.

Stashing Changes Temporarily: A Lifesaver for Full Stack Developers

As a full stack developer, you've likely found yourself in situations where you need to switch between different branches or versions of your codebase. Maybe you're working on a feature branch and suddenly, an urgent bug fix comes up that requires immediate attention. Or perhaps you're experimenting with new ideas but don't want to commit them just yet. Whatever the reason, stashing changes temporarily is a vital technique every developer should know.

What is Stashing?

Stashing, in the context of Git version control, refers to the process of temporarily setting aside your uncommitted changes. It's like hitting the "pause" button on your work, allowing you to switch to a different branch or commit without losing your progress.

Why Do We Need Stashing?

Imagine you're working on a new feature, and you've made significant changes to your codebase. However, before committing those changes, you need to address an unexpected bug report that requires immediate attention. Without stashing, you'd have to either commit your incomplete work or lose all the changes you've made so far.

Stashing saves the day by allowing you to set aside your unfinished work, switch to a different branch (like the main branch), fix the urgent issue, and then return to your original feature branch without losing any of your progress.

Hello World Example: Stashing Changes

Let's create a simple "Hello World" example to demonstrate stashing in action. We'll start by creating a new Git repository and adding a basic HTML file:

mkdir hello-world
cd hello-world
git init
echo "<h1>Hello World!</h1>" > index.html
git add .
git commit -m "Initial commit"

Now, let's say we want to experiment with a new CSS styling for our "Hello World" page. We'll make some changes to the index.html file:

<h1 style="color: red;">Hello World!</h1>

However, before committing these changes, an urgent issue comes up that requires immediate attention. We need to switch to a different branch (e.g., the main branch) to fix this issue.

Stashing Our Changes

To stash our unfinished work, we'll use the git stash command:

git stash save "Experimental CSS styling"

This will set aside our changes and revert our working directory to its original state. You can verify this by running git status, which should indicate that there are no changes pending commit.

Switching to a Different Branch

Now, we can switch to the main branch (or any other branch) using git checkout:

git checkout main

We're now on the main branch, ready to address the urgent issue. Once we've fixed the problem and committed our changes, we can return to our original feature branch.

Applying Our Stashed Changes

To retrieve our stashed changes, we'll use git stash apply:

git checkout feature
git stash apply

Our working directory will now reflect the changes we made before stashing. We can continue working on our feature branch without losing any progress.

Conclusion

Stashing changes temporarily is a powerful technique that saves time and reduces stress in your development workflow. By setting aside your unfinished work, you can seamlessly switch between branches or versions of your codebase without worrying about losing your progress. With git stash, you're always prepared to tackle unexpected issues while keeping your original work intact.

In the next article, we'll explore more advanced stashing techniques, including how to manage multiple stashes and create custom stashes. Stay tuned!

Key Use Case

Here is a workflow/use-case example:

You're working on a new feature branch for an e-commerce website, adding a "wish list" functionality to the user profile page. You've made significant changes to the codebase, but before committing those changes, you receive an urgent bug report about a critical payment gateway issue that requires immediate attention. To address this issue without losing your progress on the feature branch, you stash your unfinished work, switch to the main branch, fix the payment gateway issue, and then return to your original feature branch to continue working on the "wish list" functionality without losing any of your changes.

Finally

Stashing changes temporarily is particularly useful when you're in the midst of refactoring code or experimenting with new ideas, but aren't ready to commit them just yet. In such cases, stashing allows you to set aside your incomplete work, address other pressing issues, and then return to your original task without losing any of your progress. This technique essentially gives you the freedom to multitask and navigate between different branches or versions of your codebase with confidence.

Recommended Books

Here are some engaging and recommended books:

• "Clean Code: A Handbook of Agile Software Craftsmanship" by Robert C. Martin • "The Pragmatic Programmer: From Journeyman to Master" by Andrew Hunt and David Thomas • "Refactoring: Improving the Design of Existing Code" by Martin Fowler

Fullstackist aims to provide immersive and explanatory content for full stack developers Fullstackist aims to provide immersive and explanatory content for full stack developers
Backend Developer 103 Being a Fullstack Developer 107 CSS 109 Devops and Cloud 70 Flask 108 Frontend Developer 357 Fullstack Testing 99 HTML 171 Intermediate Developer 105 JavaScript 206 Junior Developer 124 Laravel 221 React 110 Senior Lead Developer 124 VCS Version Control Systems 99 Vue.js 108

Recent Posts

Web development learning resources and communities for beginners...

TL;DR As a beginner in web development, navigating the vast expanse of online resources can be daunting but with the right resources and communities by your side, you'll be well-equipped to tackle any challenge that comes your way. Unlocking the World of Web Development: Essential Learning Resources and Communities for Beginners As a beginner in web development, navigating the vast expanse of online resources can be daunting. With so many tutorials, courses, and communities vying for attention, it's easy to get lost in the sea of information. But fear not! In this article, we'll guide you through the most valuable learning resources and communities that will help you kickstart your web development journey.

Read more

Understanding component-based architecture for UI development...

Component-based architecture breaks down complex user interfaces into smaller, reusable components, improving modularity, reusability, maintenance, and collaboration in UI development. It allows developers to build, maintain, and update large-scale applications more efficiently by creating independent units that can be used across multiple pages or even applications.

Read more

What is a Single Page Application (SPA) vs a multi-page site?...

Single Page Applications (SPAs) load a single HTML file initially, handling navigation and interactions dynamically with JavaScript, while Multi-Page Sites (MPS) load multiple pages in sequence from the server. SPAs are often preferred for complex applications requiring dynamic updates and real-time data exchange, but MPS may be suitable for simple websites with minimal user interactions.

Read more