| by Arround The Web | No comments

How to Apply Stashes in Git

Git stash is the temporary shelf utilized to keep all the changes made to the current working directory and revert to the last commit done on the branches. The stash changes can be performed with a special set of Git commands designed to perform different actions, such as create, apply, and remove states.

In this manual, we will discuss the procedure to apply the stashes in Git.

How to Apply Stashes in Git?

To apply stashes in Git, first, we will navigate to the Git local repository. Next, create and add the new file to the stage area. Then, commit the changes made with the commit message. After that, we will execute the “$ git stash” command to keep the changes temporarily and then utilize the “$ git stash apply” command to apply stash without removing them from the stash stack.

Now, check out the below-provided procedure to implement the above scenario!

Step 1: Navigate to Git Repo

First, run the “cd” command to move to the Git local repo:

$ cd "C:\Users\nazma\Git\demo"

Step 2: Create File

Next, execute the Git “touch” command to create a new file in Git repo:

$ touch file.txt

Step 3: Add File

Add the newly created file to the stage area from the working area:

$ git add myfile.txt

Step 4: Commit Changes

Now, made changes in the Git repository and commit a corresponding message with the provided option “-m” in the “git commit” command:

$ git commit -m "file.txt added"

Step 5: Update File

Next, open and modify the added file using Git “start” command with the file name:

$ start myfile.txt

The file will be opened with the editor, add text in it and modify it:

Step 6: Add Changes

Next, add all made changes to the Git repository by executing the below command:

$ git add .

Step 7: Create Git Stash

Next, use the below-given command to keep the working directory changes temporarily:

$ git stash

Below output indicates that we have successfully saved the current changes for later use:

Step 8: List Git Stash

To display the recent modifications, run the following command:

$ git stash list

According to below output, currently, we have two stashes with the index “0” and “1”:

Step 9: Apply Stash

Finally, execute the “git stash apply” command to apply the latest Git stash:

$ git stash apply

It can be seen that the latest stash is applied without removing it from the stash stack which indicates that we have modified “myfile.txt”:

Let’s check out the next section to empty the stash stack.

How to Remove Stash in Git?

If you want to remove the stashes from the stack, run the below-provided command:

$ git stash clear

Now, to ensure the stashes removing process, list the stash stack:

$ git stash list

Below snippet indicates that our stash stack is empty:

We have provided the easiest procedure to apply the stashes in Git.

Conclusion

To apply stashes in Git, first, navigate to Git’s local repository. Then, create and add the file to the stage area. Next, commit changes with a commit message. Open the newly created file and update it. After that, execute the “$ git add .” command to add changes and temporarily hold the working directory changes using “$ git stash”. Lastly, execute the “$ git stash apply” command to apply states without removing them from the stash stack. In this manual, we have illustrated the method to apply the stashes in Git.

Share Button

Source: linuxhint.com

Leave a Reply