| by Arround The Web | No comments

git-stash Command in Git | Explained

While working in Git, often users need to switch the current Git branch to another branch or repository for doing other work. At that time, Git did not allow users to move to other branches without saving the uncommitted changes. However, they do not want to commit tracked or untracked changes because they want to add more data to the currently made changes. In that situation, they need to temporarily save the changes. For this corresponding purpose, the “git stash” command can be used.

In this guide, we will discuss:

What is the “git stash” Command in Git?

The “git stash” command is utilized for temporarily holding the uncommitted changes that users made in their working copy, then they can easily work on something else. After that, they come back and reapply these changes later on. More specifically, stashing becomes a handy process when users want to quickly switch contexts and work on something else.

How to “git stash” Uncommitted Changes in Git?

To use the “git stash” command in Git to temporarily save the uncommitted changes, follow the provided steps:

  • Navigate to the local repository.
  • View the current state of the current working repository.
  • Run the “git stash” command to temporarily hold the tracked and untracked changes.
  • Check the stash list of displaying the stash changes index.
  • Execute the “git show” command to show the change in detail.
  • To reapply, run the “git stash pop” command.
  • Run the “git stash clear” command to clear the stashed index.

Step 1: Move to Particular Git Repository

Use the provided command along with the desired Git local repository and move to it:

cd "C:\Users\nazma\Git\Coco1"

Step 2: Check Status

To view the current state of the working repository by running the “git status” command:

git status

As you can see the tracked\untracked changes exist in the Git working area and staging index. For instance, the “demofile.py” and “testfile.html” files:

Step 3: Stash Changes

Now, execute the “git stash” command to temporarily hold the staged and unstaged changes:

git stash

Step 4: List Stash Changes

Next, list the stash changes by executing the “git stash list” command:

git stash list

According to the provided output, the stash list temporarily holds the changes in the “stash@{0}” stash index:

Step 5: Show Stash

To view the stash index changes, use the provided command:

git stash show

From the below-given output, the stashed index temporarily holds the tracked “demofile.py” file and modified untracked “testfile.html” file:

Step 6: Verification

Again, check the Git repository’s current status for verification by utilizing the following command:

git status

It can be seen that there is nothing to commit and the working tree has been cleaned:

Step 7: Apply Stash Changes

To apply the temporary holds stash changes, execute the “git stash pop” command:

git stash pop

From the below-provided output, the stash changes has been reverted to the git working area and staging index:

Step 8: Clear Stash

Run the “git stash clear” command to permanently clear the stash changes:

git stash clear

Now, run the provided command to stash the changes:

git stash

It can be observed that no local changes have to save:

That’s it! We have provided the information about the “git stash” command in Git.

Conclusion

The “git stash” command is utilized to temporarily save the unstaged and staged changes that users made in their working copy. To do so, move to the local repository and view the current state of the current working repository. Then, run the “git stash” command to temporarily hold the uncommitted changes. Next, check the stash list of displaying the stash changes index. After that, use the “git show” command to view the changes in detail and reapply them by executing the “git stash pop” command. This post illustrated about the “git stash” command in Git.

Share Button

Source: linuxhint.com

Leave a Reply