git
git stash
version control
file management
coding tips

How do I stash only one file out of multiple files that have changed?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

When working with Git, there are times when you may have changed multiple files but only want to stash one of them. This can be useful in scenarios where you need to temporarily set aside certain changes without disturbing others. While Git doesn’t natively support stashing individual files directly, there are a few strategies and commands that can help achieve this. This article explores these methods in detail, providing technical explanations, examples, and additional insights.

Understanding Git Stash

The git stash command saves changes to your working directory so you can work on something else while maintaining a clean work tree. By default, it saves modifications to all tracked files. However, when you want to stash changes to only specific files, you need to employ certain Git features.

Methods to Stash a Single File

Using git stash --patch

The --patch option allows you to interactively select changes in the current working directory to stash. Here’s a step-by-step guide on how to use it:

  1. Stage the File:
    First, add the file you want to stash to the staging area. For example, if you want to stash changes made to file1.txt, use:
bash
   git add file1.txt
  1. Stash with --patch:
    Use git stash push together with the --patch option. This will let you go through changes piece by piece and decide what to stash.
bash
   git stash push -p

During this interactive command, you'll be asked if you want to stash each change. For the changes you're interested in stashing, type y; for others, type n.

  1. Apply the Stash:
    If needed later, you can apply the stashed changes back into your working directory using:
bash
   git stash apply

Assuming Other Changes

Suppose you've not yet staged your changes. Another flexible and less interactive way is to differentiate your desired stash from incidental changes:

  1. Commit Other Changes:
    Commit all other changes except the one you want to stash. This can be done as:
bash
   git add .
   git reset file1.txt  # Unstage the specific file
   git commit -m "Temporary commit for other changes"
  1. Stash the Specific File:
    Now, you can stash the desired file:
bash
   git add file1.txt
   git stash
  1. Amend or Undo Temporary Commits:
    You may amend the temporary commit or reset the branch to the state before the commit:
bash
   git reset --soft HEAD~1  # Moves HEAD back but keeps changes as uncommitted

Leveraging stash save and git checkout

An indirect but effective way to manage this involves using stash selectively with checkout:

  1. Stash All:
    Stashes all changes in a clean work tree.
bash
   git stash
  1. Checkout Specific Changes:
    Bring back only the file you want to retain:
bash
   git checkout stash -- file1.txt
  1. Restore Other Changes:
    Finally, re-apply the stash, except for the file you singled out.
bash
   git stash apply  # This will merge other changes back

Table: Key Commands & Their Descriptions

CommandDescription
git add file1.txtStages the specific file for stash if using --patch.
git stash push -pInteractively stashes parts of changes file-by-file.
git stashStashes all current changes; can be restricted by selective add and reset.
git reset file1.txtUnstages a specific file to commit everything else.
git reset --soft HEAD~1Resets branch to undo previous commit but keeps changes uncommitted.
git checkout stash -- fileRESTOREs specific file from stash.

Additional Topics

Best Practices

  • Message Your Stashes: Use git stash push -m "message" to clearly identify the purpose of the stash.
  • Regular Cleanup: Use git stash list to view and git stash drop to remove unused stashes to maintain clarity in the repository.
  • Branch Contingencies: Always ensure your branch context aligns with the stashed changes.

Alternative Tools

For more advanced scenarios, consider tools like git-subrepo or git-filter-repo which can manipulate sub-repositories or large change sets with more flexibility than default Git.

This guide demonstrates that with a bit of strategic planning and command usage, Git does afford the flexibility to stash changes at a granular level, even when it’s not immediately obvious. By leveraging these methods, developers can keep their workflows organized and efficient.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions