Welcome to our comprehensive guide on using git stash! In this tutorial, we'll learn how to manage changes in your working directory, even when you can't commit them right away. Let's dive in!
git stash is a useful command that helps you save your current changes temporarily, so you can switch to another branch or task without losing your work. It's like a 'to-do list' for your git work!
Imagine you're working on a feature branch, but suddenly you're asked to fix an urgent bug on the master branch. Without git stash, you'd have to either abandon your feature work or commit it unfinished. But with git stash, you can save your changes, switch to the master branch, fix the bug, and then return to your feature work later.
This command saves your current changes, including both staged and unstaged ones.
$ git stash save "My work in progress"This command shows you a list of your stashed changes. Each stash is given a unique ID.
$ git stash list
stash@{0}: My work in progress on feature-branchThis command applies the latest stashed change to your working directory.
$ git stash applyThis command removes the latest stashed change from the list.
$ git stash dropLet's see git stash in action with a simple project.
$ mkdir my-project
$ cd my-project
$ git init$ touch index.html
$ echo "Hello, World!" > index.html$ git add index.html
$ git commit -m "Initial commit"$ git checkout -b feature-branch
$ echo "This is a feature!" >> index.html$ git stash save "Feature changes"$ git checkout master
$ echo "Fixed an urgent bug!" >> index.html
$ git commit -am "Fix urgent bug"$ git checkout feature-branch
$ git stash applyNow, your feature changes are back in your working directory!
What command saves your current changes in git?
What command shows a list of your stashed changes in git?
What command applies the latest stashed change to your working directory in git?
Keep learning and happy coding! 💡