Git

How Would I Extract a Single File (or changes to a file) From a git stash?

While working on the tracking tool known as Git, developers are permitted to modify the existing file as well as new files. Then, they can stage changes and update the Git repository. Sometimes, developers don’t want to push changes to the Git repository and temporarily hold for further changes. For this purpose, they can stash changes. Additionally, developers are allowed to extract a desired file from the “git stash” with the help of the “git diff <stash-index^1> <stash-index> — <file-name>” command.

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:

$ cd "C:\Users\nazma\Git\perk1"

Step 2: Check Content List

Now, view the existing content of the current working repository by running the “ls” command:

$ ls

Step 3: Update Existing File

Next, run the “echo” command to modify the existing text file:

$ echo "my second text file" >> file2.txt

Step 4: Git Stash

After that, temporarily hold the working area changes through the “git stash” command:

$ git stash

Step 5: List Stash Changes

To list the temporary hold changes, execute the following command:

$ git stash list

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:

$ git diff stash@{0}^1 stash@{0} -- file2.txt

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”.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.