Selective Stashing with Git: How to Stash Individual Files and Hunks

Introduction

In the world of version control, managing changes efficiently is key to maintaining a clean workflow. Git provides a powerful feature called stashing that allows you to temporarily shelve modifications so you can switch contexts or branches without committing incomplete work. Sometimes, however, your working directory contains multiple changed files and you may only want to stash certain parts of your work. This tutorial will guide you through the techniques for selectively stashing individual files and hunks in Git.

Understanding Git Stash

The git stash command takes modified tracked files out of your working tree and saves them on a stack of unfinished changes that you can reapply at any time. It’s useful when you need to switch branches but aren’t ready to commit the current state of your work.

Basic Usage

To stash all modified and staged files:

git stash

To include untracked files in the stash, use:

git stash -u  # or --include-untracked

Stashing Specific Files

Sometimes you only want to stash changes from a particular file. Here’s how you can do that:

Using git stash push

From Git version 2.13 onwards, you have the ability to stash specific files using git stash push.

Example:

git stash push -- my/file.sh

This command stashes changes in file.sh while leaving other files unchanged.

Selective Stashing Before Git 2.13

For those on older versions of Git, selectively stashing involves a few more steps:

  1. Stash All Changes:
    First, stash all changes temporarily.

    git stash
    
  2. Reapply Specific Files:
    Then, re-apply the changes for files you want to keep in your working directory:

    git checkout -- my/file.sh
    
  3. Pop Stash:
    Finally, bring back all stashed changes.

    git stash pop
    

Interactively Selecting Hunks

When dealing with modified files, sometimes you want to stash only certain parts of a file (hunks). Git provides interactive stashing using the -p option.

Example:

git stash push -p

This command initiates an interactive mode where you can select specific hunks from your changes:

  • y: Stash this hunk.
  • n: Do not stash this hunk.
  • q: Quit without stashing further hunks.
  • a: Stash this and all subsequent hunks in the file.
  • d: Do not stash this or any later hunks in the file.
  • Additional options include splitting a hunk, manually editing it, or searching for specific changes.

Advanced Techniques

For situations where even git stash push is too cumbersome due to numerous modified files, consider using temporary diff files:

  1. Generate a Diff:
    Create a diff of the desired path:

    git diff path/to/dir > stashed.diff
    
  2. Revert Changes in Working Directory:
    Revert changes for that specific directory or file:

    git checkout path/to/dir
    
  3. Apply the Diff Later:
    When you’re ready to reapply your changes, use:

    git apply stashed.diff
    

Best Practices

  • Clear Intentions: Clearly determine which files or hunks need to be stashed before executing commands.
  • Backup Changes: Regularly commit work to avoid losing progress when manipulating the stash stack.
  • Version Awareness: Be aware of your Git version capabilities, as newer versions offer simplified options for stashing.

Conclusion

Stashing changes selectively is an essential skill in any developer’s toolkit. By mastering these techniques, you can manage your workflow more efficiently and handle complex development scenarios with ease. Whether you’re working on older or newer versions of Git, the ability to control what gets stashed provides greater flexibility and precision in your version control practices.

Leave a Reply

Your email address will not be published. Required fields are marked *