DEV Community

Cover image for Equivalent Perforce Shelving Command in GitHub
Vincent Tsen
Vincent Tsen

Posted on • Originally published at vtsen.hashnode.dev

Equivalent Perforce Shelving Command in GitHub

Let's explore git stash usages, which is equivalent to (not 100%) perforce shelving files concept

In Perforce, you can shelve files. The idea allows you to save your temporary changes (shelve files) and retrieve them later (unshelve files).

I'm a Perforce guy. So, I wonder what is the equivalent shelving files in GitHub? After doing some research, I think the equivalent Perforce shelving files command in GitHub is git stash.

git stash is Similar to Shelving Files

Well, it is not 100% equivalent. The key difference is git stash is purely local, while Perforce shelved files are in the server. Since it is local, you're the only one who can access the changes. If you want to share with others, you have to create a branch and commit your changes to the remote repository (this will not be covered in this article).

Other than that, I think git stash is pretty much similar to shelving files in Perforce. Let's explore its usages!

git stash save "<description>"

You can also just use git stash but it automatically generates the description for you which is often wrong and incorrect. So, I prefer to use git stash save command instead with a description that I want to save.

C:\Work\AndroidStudy\Demo\CleanEmptyCompose> git stash save "First Changes"
Saved working directory and index state On master: First Changes
Enter fullscreen mode Exit fullscreen mode

Once your changes are stashed, it restores to the original version before you make any changes. You can now make another change and stash it again.

C:\Work\AndroidStudy\Demo\CleanEmptyCompose> git stash save "Second Changes"
Saved working directory and index state On master: Second Changes
Enter fullscreen mode Exit fullscreen mode

git stash list

To see all the stashed files, you can git stash list command.

C:\Work\AndroidStudy\Demo\CleanEmptyCompose> git stash list 
stash@{0}: On master: Second Changes
stash@{1}: On master: First Changes
Enter fullscreen mode Exit fullscreen mode

So, I have 2 changes now.

git stash apply "<stash name>"

To restore my first changes, I run git stash apply "stash{1}" command:

C:\Work\AndroidStudy\Demo\CleanEmptyCompose> git stash apply "stash@{1}"
On branch master
Your branch is up to date with 'origin/master'.
Enter fullscreen mode Exit fullscreen mode

git stash drop"<stash name>"

To delete the stashed files for my first changes, I run git stash drop "stash{1}" command:

C:\Work\AndroidStudy\Demo\CleanEmptyCompose> git stash drop "stash@{1}"
Dropped stash@{1} (5c1cced4511bb453492f27fd6c71d74ff570b7ae)
Enter fullscreen mode Exit fullscreen mode

If you perform git stash list, now you see the first changes stashed files are removed now.

C:\Work\AndroidStudy\Demo\CleanEmptyCompose> git stash list            
stash@{0}: On master: Second Changes
Enter fullscreen mode Exit fullscreen mode

git stash clear

This deletes all the stashed files! git stash list doesn't show anything now after you run the command.

C:\Work\AndroidStudy\Demo\CleanEmptyCompose> git stash clear
C:\Work\AndroidStudy\Demo\CleanEmptyCompose> git stash list
Enter fullscreen mode Exit fullscreen mode

Summary

There are other git stash commands (e.g. git stash pop/ git stash push, etc.) but I think the above commands are good enough for me.

For complete git stash commands, you can refer to the official documentation here.


Originally published at https://vtsen.hashnode.dev.

Top comments (0)