Introduction
Git prevents users from switching to another branch without committing changes when there is a risk of conflicts or data loss.
With Git stash, you can save uncommitted changes without altering the branch's current state. Git stashes all tracked changes by default and allows users to stash specific files.
Learn how to stash a specific file in Git and resume working on your unfinished code later.
Prerequisites
- Git installed (see how to install Git on Windows, macOS, or Ubuntu).
- A Git repository.
How Does Git Stashing Work?
Git stash is a command-line utility that temporarily saves uncommitted changes and modified files to a local stash.
When you enter the git stash
command:
- Git identifies all changes in tracked files within the working directory.
- It saves the changes to a local stash in the refs/stash directory. Because the stash is local, it is not visible to other developers sharing the same Git repository.
- The working directory is reset to match the last committed state (HEAD). Staged changes and modified tracked files are removed.
- If there are existing stashes, the new stash is added to the stack, and older stashes are recorded in the reference log. The reflog keeps a history of updates to branch tips and other references, allowing you to restore a previous Git stash.
Instead of stashing all changes in tracked files, Git stash also allows you to select and stash specific files from your repository. This feature is useful when you want to isolate and save changes in certain files while continuing to work on other files.
Note: Learn how to use the Git stash utility to stash untracked files in Git.
How to Git Stash Specific File?
The git stash
command stashes all tracked files in the current working directory. Stashing a specific file requires an additional push
option along with the file name.
Use the following syntax to stash a specific file:
git stash push [file]
For example:
git stash push readme.me
Running the command stashes only the specified readme.md file, while any other files that may have been changed remain unstashed.
You can customize the stashed work by adding messages to the stash or use an interactive mode to select specific changes to the stash.
Add Message
Use the -m
flag to add a custom message to the stashed file. The message describes the changes and helps you identify the purpose of the stash later. The syntax is:
git stash push -m "message" [file]
For example, the following command stashes changes to the readme.md file and adds the Made edits to the readme file message:
git stash push -m "Made edits to the readme file" readme.md
Interactive Stashing
The --patch
(-p
) option allows users to stash parts of files called hunks interactively. To stash partial changes, run the following command:
git stash push --patch
The command initiates an interactive mode, prompting you to select an action for each hunk iteration. Press one of the following keys to respond to the prompts:
- y - Stash the current hunk.
- n - Skip the current hunk.
- q - Abort stashing.
- a - Stash the current hunk and all later ones in the file.
- d - Skip stashing all hunks in the specified file.
- / - Search for a hunk.
- s - Split the current hunk into smaller hunks.
- e - Manually edit the current hunk.
For example, press y and Enter to stash a hunk:
Check if the files have been stashed by listing Git stash entries:
git stash list
The output shows all stashes, including their reference and stash message.
Conclusion
After reading this tutorial, you know how to stash a specific file in Git, add a message, and use interactive stashing.
If you have multiple stashes, try naming a Git stash to help you retrieve it later or drop a Git stash to manage the stash list more efficiently.