Git Stashing: A Step-by-Step Example
Git stashing is a feature that allows you to save changes in your working directory that are not ready to be committed. It's useful when you need to switch to another branch or address an urgent issue without committing incomplete changes. Here's a step-by-step example:
If you haven't already, create a new directory and initialize a Git repository:
$ mkdir my_project
$ cd my_project
$ git initCreate a few files and make some modifications:
$ touch file1.txt
$ echo "Content for file1" > file1.txt
$ touch file2.txt
$ echo "Content for file2" > file2.txtCheck the status of your working directory:
$ git statusStash the changes in your working directory:
$ git stash save "WIP: Work in progress"The message "WIP: Work in progress" is just a description of what you are stashing. You can customize it.
Check the status after stashing to see that your working directory is clean:
$ git statusNow, you can switch to another branch or make other changes without carrying the uncommitted changes from the previous branch.
List your stashes to see what you have stashed:
$ git stash listWhen you want to reapply your stashed changes, use git stash apply:
$ git stash applyCheck the status to see that your changes have been reapplied:
$ git statusFinally, commit the changes you reapplied:
$ git commit -m "Committing stashed changes"Git stashing is a helpful feature for temporarily saving changes without committing them. It allows you to switch branches or address urgent issues without losing your work. Stashed changes can be reapplied when you are ready to continue working on them. Regularly checking the status and listing stashes helps you manage your work effectively.