WindowsMacSoftwareSettingsSecurityProductivityLinuxAndroidPerformanceConfigurationApple All

How to Stash Changes in Git

Edited 1 week ago by ExtremeHow Editorial Team

GitStashWindowsMacLinuxCommand LineDevelopmentVersion ControlSource ControlWorkflowProductivity

How to Stash Changes in Git

This content is available in 7 different language

Git is one of the most widely used version control systems in the software development world. It was created by Linus Torvalds in 2005 for the development of the Linux kernel. Since then, it has been used in software projects of every size, by teams of developers…sometimes even by developers working alone. Git helps you track versions of code, collaborate with multiple developers, and ensure that you can always revert to previous versions of your software when needed.

When working on a Git-managed project, there may be times when you need to switch branches. For example, you might be working on a new feature and suddenly there is a critical bug report that needs to be fixed immediately. In such cases, you would want to switch context and work on the bug fix without losing your progress on the feature you were working on. This is where Git Stash comes in handy.

Understanding Git Stash

Git stash is a powerful feature that allows developers to temporarily save their changes without committing them to the repository. It effectively "shelfies" the changes you've made, allowing you to check out a new branch without committing a new feature that may not yet be ready for others to see. When you're ready, you can apply these changes back to your working copy or to another branch.

When you run git stash command, your changes are saved to a stack. This stack can contain multiple stashes, and you can apply any of the stashes listed later. A single stash is a combination of:

Once the changes are stashed, your working directory is clean, and you can switch branches, compile your code, or do anything else you need to do. After you've completed that task, you can return to your original work by popping or applying the stash.

Basic Git Stash Commands

To start using stash in Git, you need to be familiar with some basic commands. Here's a look at the most important commands:

1. Keeping your change money safe

To store your changes, use the following command:

git stash push

This will save your local modifications and revert your working directory to match the HEAD commit. It's as if you never made those changes.

2. Hiding with the message

It's often useful to include a message with your hidden changes for easy identification:

git stash push -m "your stash message"

This message helps you remember what changes are in this stash without referring to line-level changes.

3. Listing the stored stash

To see a list of all your saved collections:

git stash list

This command will print a list of all the stashes you have saved and their associated messages.

4. Applying stash

To reapply the latest stash:

git stash apply

This command applies the latest stash to your current working directory, but does not remove it from the stash list.

5. Applying specific stash

If you want to apply a specific stash, use:

git stash apply stash@{n}

Replace stash@{n} with the specific stash you want to apply, which you can identify from git stash list output.

6. Stash popping

To apply and immediately delete the latest stash:

git stash pop

This is similar to the apply command, but it also removes the applied stash from your list.

7. Dropping the stash

If you want to delete a particular stash (without applying it), use:

git stash drop stash@{n}

This will destroy the specified stash.

8. Clearing all secret storages

If you want to delete all stash, the command is:

git stash clear

Use this command with caution, as it will permanently destroy all stored changes.

An example workflow

Consider a scenario where you are developing a new feature on the branch feature-xyz , and you have made several changes to your working copy. Suddenly, you need to fix a serious bug on the main branch.

A Step-by-Step Guide to Using Git Stash

  1. View current changes: Make sure the changes you want to stash have not been committed yet. You can check their current status via:
    git status
  2. Protect your changes: Protect your modifications with a descriptive message:
    git stash push -m "Saving work on feature-xyz"
  3. Switch branches: Switch to main or any other branch you need to work on:
    git checkout main
  4. Fix the bug: Make any necessary changes to fix the bug and commit them.
    git commit -a -m "Fixed critical bug"
  5. Revert to your feature branch: Once the bug is fixed, revert to the feature branch:
    git checkout feature-xyz
  6. Apply your stored changes: Restore your work from storage:
    git stash pop

    If there is any disagreement, resolve it and then proceed with your work.

Advanced use cases and flags

Git Stash offers some flexibility with more advanced options:

1. Archiving untracked files

By default, git stash push will only stash changes to tracked files. To include untracked files:

git stash push -u

This will archive both tracked and untracked files.

2. Hide ignored files

If you also want to stash ignored files (files explicitly excluded by .gitignore ):

git stash push --all

3. Hiding part of the file

You can selectively archive parts of files using:

git stash -p

This command is interactive and allows you to select specific hunks to stash.

4. Implementing stash with indexes

To reapply stored changes, including staged changes (in the index), use:

git stash apply --index

Be aware that this can cause conflicts if there are conflicting changes to the index.

Understanding stash conflicts

While stashing and unstashing are generally straightforward, conflicts can arise, especially when the stash is applied to an updated codebase. If conflicts occur, Git will mark them like merge conflicts. You must edit the affected files, resolve the conflicts manually, and mark them as resolved:

git add <file>

Once resolved, you can continue working and eventually submit your changes.

Git Stash best practices

Here are some tips to make working with Git Stash easier and more effective:

Conclusion

Git stash is a versatile tool in the developer's arsenal, particularly useful for preserving uncommitted work and temporarily changing references. By understanding how to use Git stash effectively, developers can seamlessly manage changes across different tasks, improving efficiency and flexibility in their workflow.

From saving unfinished work to focusing on important bugs, properly implementing git stash capabilities into your workflow can make handling code changes easier and more manageable. As you become comfortable with Git stash, you'll find it becomes an indispensable part of your development toolkit.

If you find anything wrong with the article content, you can


Comments