Examples

Git Stash Workflow

Using a Stash Workflow

Git stash workflow saves and restores changes with git stash.

Introduction to Git Stash

Git stash is a powerful feature in Git that allows developers to save changes in a temporary storage area, known as the stash, without committing them to the repository. This is particularly useful when you need to switch branches or work on something else without losing your current progress.

Basic Git Stash Commands

The primary command for working with the stash is git stash. By default, it stashes the changes in your working directory and the index (staged changes).

Once you have stashed your changes, you can safely switch branches or make other changes. When you're ready to return to your previous work, you can use git stash apply to reapply the changes.

Listing Stashed Changes

To view the list of stashed changes, you can use git stash list. This command will display all the stashes you've created, along with their unique identifiers.

Applying a Specific Stash

If you have multiple stashes and want to apply a specific one, you can reference it by its identifier, which is visible in the list. Use the following command format to apply a specific stash:

Dropping a Stash

After you've applied a stash, it's often a good idea to drop it to clean up your stash list. You can do this using git stash drop followed by the stash identifier.

Stashing Untracked or Ignored Files

By default, git stash only includes tracked files. If you wish to stash untracked or ignored files, you can use the -u or -a options respectively. The -u option stashes untracked files, while -a stashes both untracked and ignored files.

Creating Named Stashes

For better organization, you can name your stashes, making them easier to identify later. This is done by providing a message with the stash command.

Conclusion

Using git stash effectively can greatly enhance your workflow by allowing you to save your progress without committing unfinished work. By mastering stash commands, you can keep your working directory clean and organized, improving your productivity and focus.