This write-up will explain the method of extracting just one file from a “git stash”.
How to Extract One File (or changes to a file) From a git stash?
To extract a single file from a “git stash”, check out the below-given procedure:
- Switch to the required Git local directory.
- List repository content.
- Select the required file and update it.
- Temporarily holds the added changes.
- View the lists of the stashed index.
- Utilize the “git diff stash@{0}^1 stash@{0} — <file-name>” command.
Step 1: Redirect to Local Repository
Execute the “cd” command to redirect to the particular repository:
Step 2: Check Content List
Now, view the existing content of the current working repository by running the “ls” command:
Step 3: Update Existing File
Next, run the “echo” command to modify the existing text file:
Step 4: Git Stash
After that, temporarily hold the working area changes through the “git stash” command:
Step 5: List Stash Changes
To list the temporary hold changes, execute the following command:
Here, all stash changes are listed below. Highlighted stash index is the most recent stashed changes and copy its index:
Step 6: Extract Single File
Finally, execute the “git diff” command along with the copy stash index and extract stashed single file:
Here, the:
- “stash@{0}^1” represents the parent of the given stash.
- “file2.txt” is the stashed file that is modified.
- “—” symbol for an old version of the file.
- “+++” symbol for newly added changes.
In the below-given output, the highlighted text is the newly added text which is a temporary push to the stashed index:
That’s it! We have provided the easiest way to extract a single file from a “git stash”.
Conclusion
To extract a single file from a “git stash”, first, switch to the required Git local directory and list its content. Then, select one file and update it. After that, temporarily holds the added changes and lists the stashed index. Lastly, execute the “git diff stash@{0}^1 stash@{0} — <file-name>” command. This write-up described extracting a single file from a “git stash”.