How to Stash One File in Git

Abdul Jabbar Feb 02, 2024
How to Stash One File in Git

Sometimes, when we are working on a huge project, we come across situations where our work state becomes messy for various reasons.

In that case, we need to switch branches for a while to work better. For this situation, the problem we could face is that we don’t need to commit to the work that is half-done just so that we can approach this point later.

The solution to this problem is the git stash command. The git stash command lets us temporarily store our working directory’s state in the temp file.

, we can say that we can then switch branches, commit our desired work, and then afterward return to the work that we have put on hold with the command git stash.

In other words, we can say the git stash command is much simple; it stores the current state of our working directory and the index in our stash file. That means there are no changes committed to the index or the working directory.

Stashing takes the irrelevant state of our working directory, that is, our modified tracked files and staged changes, and saves it on a stack data type of unfinished changes that you can later reapply using a different branch as well.

As we work, we may make many changes and add many dirty files, but when we stash, Git saves only a single, clean snapshot of our working directory.

We should know that stashing a file means that we push the content of this file to the stash. If we change the file again, it will be changed in the stash and not in the working directory. So, we should not change the file after stashing it.

We should use the stash save command to stash a file. If we want to stash all the changes in the working directory, we should use the stash save --all command. If we want to un-stash a file, we should use the stash pop command.

Steps to Stashing Only One File in Git

Let’s say we have seven files, and we have changed all of them. Now we will see what steps should be followed to stash only one of seven files in the current repository.

  • Viewing the changed files

    Firstly, we will execute the command git status to examine the list of the changed files.

    git status
    
  • Staging files

    After examining the list we will run the command git add to stage all the seven files:

    git add .
    
  • Unstaging the file

    After staging all the files, the next step is unstaging file2 with the help of the command git reset.

    git reset file2
    
  • Stashing the file

    Now we will stash file2 with the help of the command git stash to take it back to its present committed stage in the current repository.

    git stash --keep-index
    

    The other method by which we can do stashing only one file is executing the following command.

    git stash save -p "commit message"
    

With the help of the above method, we can select which block should be added to the stash. Also, If we have worked on a file, we can stash the changes before we commit it so that the commit is done with our working file.

The command will stash the changes added to our index (staged changes) and changes made to files currently tracked by Git (unstaged change).

Stashing is a way of taking a snapshot of the current state of our working copy, storing the snapshot away, and then returning to a clean working copy state.

Once we have added the changes to the index, we can commit them. If we want to stash the changes in the working directory but not the index, use the --keep-index option. Stashed changes can be reapplied at any time.

Note
After stashing a file, we should commit it to the repository. We should not forget to do it.
Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Stash