Git

Difference Between Stash vs Stage Files in Git

While working on Git, developers make many changes to their development projects. They must save all the modified untracked changes by adding them to the Git staging area. However, sometimes, certain unnecessary changes do not need to be saved to the Git index. In this situation, users add those changes to the stash.

This article will illustrate:

Difference Between Stash and Stage Files in Git

In Git, the “Stash” is a process that saves the untracked modified files into the stack. Stash changes are unnecessary changes that need not be added to the Git index. On the other hand, the “Stage” is the step that adds the untracked files to the Git staging area for tracking purposes.

How to Stash Files in Git?

To stash files in Git, first, switch to the required directory and view its current status. Then, write out the “git stash” command to save the untracked or uncommitted changes to the stash and verify changes.

Step 1: View Current Status

First, type out the below-provided command to check the current status of the working directory:

$ git status

It can be seen that the current repository contains “Test1.txt” and “Test2.txt” two modified files that need to be tracked:

Step 2: Stash Files

Then, save the untracked files changes to the stash using the provided command:

$ git stash

Step 3: View Stash History

Next, enter the following command to view the stored changes in the stash:

$ git stash show

According to the below output, the files changes have been stored in the stash:

Step 4: Verify Changes

Lastly, check the current status of the working repository:

$ git status

As you can see the Git status is clear now which indicates that the changes have been stashed successfully:

How to Stage Files in Git?

To stage all files in Git, utilize the “git add .” command. Moreover, if the user wants to add a single file, the “git add <file-name>” command can be used.

Step 1: View Repository Status

First, check the current status of the working directory:

$ git status

It can be observed that the repository contains two untracked or unstaged files:

Step 2: Stage Files

Now, run the provided command to add files to the Git staging area for tracking purposes:

$ git add .

Step 3: Verify Changes

To verify whether the files have been staged or not, check the Git status:

$ git status

It can be observed that the unstaged files’ changes have been staged successfully:

We have explained about the stash and stage files in Git.

Conclusion

Stash” saves the untracked modified files’ changes into the stash list. To stash changes, the “git stash” command is utilized. On the other hand, “Stage” moves the untracked changes to the Git index. To all files to the Git index, utilize the “git add .” command. This article illustrated the difference between stash and stage 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.