Git

How to Unstash Only Certain Files?

Developers use the Stash mechanism to store their project’s unnecessary changes in a separate place. They can store one or multiple file changes in the stash list. However, sometimes, it is required to add the stashed changes to their project. In this situation, you can unstash any file whenever you want. Moreover, various Git commands are available to unstash certain files.

This article will demonstrate different methods to unstash only specific files in Git.

How to Unstash Only Certain/Particular Files?

Different Git commands can be used to unstash only certain files, such as:

Method 1: Unstash Certain Files Utilizing “git checkout” Command

To unstash only certain files:

  • First, switch to the local directory and view its current status.
  • Then, list all the available stashes.
  • After that, choose the particular stash and view its changes.
  • Next, select the files that need to be unstashed.
  • Finally, execute the “git checkout stash@{<index-no>} — <filename>” command and verify changes.

Step 1: Navigate to Local Repository
First, redirect to the desired local repository by writing out the below-provided command:

$ cd "C:\Git\Repos1"

Step 2: View Current Status
Next, check the current status of the working repository:

$ git status

According to the below output, the current repository status is clear:

Step 3: View List of Stashes
Then, display the list of available stashes in the current repository:

$ git stash list

It can be observed that the repository contains two stashes, i.e., “stash@{0}” and “stash@{1}”:

Step 4: View Particular Stash Changes
Now, run the following command and specify the particular stash index to view its changes. For instance, we have specified “stash@{0}” stash:

$ git stash show stash@{0}

It can be observed that the “Test1.txt” and “demofile.txt” file changes are stored in the current stash:

Step 5: Unstash Certain File
After that, unstash a particular file by executing the “git checkout” command along with the desired stash id and file name:

$ git checkout stash@{0} -- Test1.txt

Step 6: Verify Changes
Finally, ensure that the particular file has been unstashed by checking the Git status:

$ git status

It can be seen that the “Test1.txt” file has been unstashed successfully:

Method 2: Unstash Certain Files Utilizing “git restore” Command

Users can also use the “git restore –source=stash@{<index-no>} — <filename>” command to unstash only the particular files in Git. Follow the provided steps for practical demonstration.

Step 1: View Specific Stash Changes
First, choose the particular stash and display the changes stored in it using the following command:

$ git stash show stash@{1}

The below output displays the list of files stored in the “stash@{1}” stash. Select the particular file that needs to be unstashed:

Step 2: Unstash Particular File
Then, type out the given-provided command along with the desired stash id and particular file name to unstash it. For instance, we want to unstash the “demofile.txt” file:

$ git restore --source=stash@{1} -- demofile.txt

Step 3: Ensure Changes
Lastly, check the status of the current repository to verify new changes:

$ git status

As you can see, the desired “demofile.txt” file has been unstashed successfully:

We have explained the easiest methods to unstash only certain files in Git.

Conclusion

To unstash only certain files, Git provides different commands. These command include “git checkout stash@{<index-no>} — <filename>” command and the “git restore –source=stash@{<index-no>} — <filename>” command. This article explained the methods to unstash specific files in Git.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.