Git

How to Unshelve Changes Using Git?

In Git, stash or shelve changes permit the user to temporarily set aside tracked or experimental work. They can also collaborate effectively with others, review code more efficiently, and provide a safety net for work. Furthermore, the concept of “unshelving” changes indicates restoring or applying changes that were previously shelved or stashed.

This guide will elaborate:

How to Shelve Changes Using Git?

To shelve changes using Git, check out the below-given steps:

  • Go to the Git local directory.
  • List all files using the “ls” command.
  • Select a file and modify it.
  • Track the file in the staging area using the “git add” command.
  • Stash the changes with the help of “git stash” to save the files in temporary memory.

Step 1: Redirect to Git Local Directory

First, open the Git Bash tool and navigate to your preferred Git local directory by specifying the path along with the “cd” command:

cd "C:\Users\user\Git\testproject"

Step 2: List all Files

Execute the “ls” command to list all data using “ls” command:

ls

As you can see, the whole content of the current Git repository has been listed successfully and we have selected the “myfile.txt” file for further use:

Step 3: Modify a File

Open the previously selected file for modification by running the “start” command:

start myfile.txt

Now add some text in the opened file, save changes, and close it:

Step 4: Track the File and Verify the Status

Now, track the modified file to the staging area by executing the “git add” command:

git add myfile.txt

Then, check the status of the Git repository to verify the tracked changes:

git status

Step 5: Shelve/Stash Changes

Run the “git stash” command to save the tracked file into a temporary shelve for later usage:

git stash

The stated output indicates that the file has been temporarily saved:

Step 6: View Git Status

Check the current working status by executing the below-stated command:

git status

It can be noticed that the tracked file has been saved in a temporary shelve and there is nothing to commit in the Git directory:

How to Unshelve Changes Using Git?

To unshelve changes using Git, try out the given instructions:

  • Go to the Git working directory.
  • Check Git status.
  • List all available shelves using the “git stash list”.
  • Unshelve the changes with the help of “git stash pop” along with the stash id.

Step 1: Redirect to Git Local Directory

Move toward the Git local directory using the “cd” command:

cd "C:\Users\user\Git\testproject"

Step 2: View Git Status

Display the current working status of the Git directory by using the “git status” command:

git status

It can be noticed that the working area is clean and there is nothing to commit:

Step 3: List All Shelves

View all available shelves/stash by running the “git stash list” command:

git stash list

The resultant image shows that there are two stash indexes available and select one of them to unshelve it:

Step 4: Unshelve/Unstash Changes

Execute the “git stash pop” command along with the stash index id to unshelve and store it back in the working directory:

git stash pop stash@{0}

The stated output indicates that the file has been stored back in the Git working directory as an untracked file:

That’s all about the easiest way to unshelve the changes using Git.

Conclusion

To unshelve changes using Git, go to the Git working directory and check Git status. Then, list all available shelves/stashes using the “git stash list”. Next, unshelve the changes with the help of the “git stash pop” command along with the stash id. This tutorial demonstrated the method to unshelve the changes using Git.

About the author

Hafsa Javed