In this short article Git Stash for Saving and Retrieving Changes I will show you how to use the git stash command to save and get back changes in Git. There are times when you need to save your changes in a Git repository without saving them to the repository. This is when the git stash tool comes in handy.

What is Git stash?

With git stash you can save your changes in a “stash”  without committing them. You can use this to move to a different branch or work on another task without committing the changes you have already made. After some time, you can get back to your changes and keep working on them.

Git saves all the changes in your working directory that haven’t been pushed yet in a new stash when you run git stash. Git also takes your working directory back to the state it was in before you made changes. This lets you switch to a different branch or work on another task.

Saving changes with Git stash

Here’s what you need to do to save your changes in Git Stash:

Stage Your Changes: Before stashing your changes, make sure to stage them using git add . This ensures that all the changes you want to stash are ready to be saved.

git add .

Stash Your Changes: After your changes have been made, you can use the following command to save them:

git stash save "Your stash message here" 

Change Your stash message here to a message that describes your stash. If you have more than one stash, this message will help you find the right one later.

Saving changes with Git stash

Retrieving changes from Git stash

You might want to get your changes back later after you’ve put them away. Here’s how you can do it:

List Stashes: To list all the saved stashes using the following command:

git stash list

This command shows a list of stashes along with the IDs and messages for each one.

Apply Stash: To apply a specific stash from the list, use the following command:

git stash apply stash@{n}

Replace stash@{n} with the stash identifier you want to apply. You can find the stash identifier from the list obtained in the previous step.

Pop Stash: Alternatively, you can use the git stash pop command to apply and remove the top stash from the list all at once:

git stash pop

This command uses the top stash and takes it off the list of stashes, which means that the changes are applied again to your working directory.

Retrieving changes from Git stash

Deleting Git stashes

To delete a stash, use the following command:

git stash drop stash@{n}

Replace stash@{n} with the stash identifier you want to drop. This command permanently removes the specified stash from the list.

Deleting Git stashes

Conclusion – Git Stash for Saving and Retrieving Changes

Git stash is a powerful command that lets you save your changes temporarily without committing them, this can help you switch between tasks or branches. This feature allows you to easily save your work with git stash, retrieve changes when needed, list your stashes with git stash list, and remove stashes with git stash drop. When working on a Git project, leveraging git stash can significantly enhance productivity by providing a flexible way to manage and organize changes.

Use the official Git documentation to find out more and read more specific instructions.

See also: Other posts tagged in Git category

Got any queries or feedback? Feel free to drop a comment below!

Categorized in: