2

I'm having a hard time understanding why anyone would ever use git stash when it seems to be an inferior option.

As far as I can tell, git stash's use case is to save work in progress that you have on a branch when you go work on another branch.

Usually, when I am in this situation, I run this instead:

$ git add --all
$ git commit -m "WIP: some info about the changes"

and go work on the other branch. When I come back to the branch I was working on, I can simply run git reset HEAD~1 to go back to my previous commit and resume where I was, or stack WIP commits until I'm done and I squash them.

Compared to using git stash, this seems to me like the superior option:

  • I can always run git pull --rebase to update my branch and rebase my WIP on top
  • I can cherry-pick my WIP on another branch if necessary

If I want to, I can even run git checkout -b temporary_branch before creating my commit so that it is located in a temporary branch instead of the one I was working on:

  • this gives me the usual benefits of branches, i.e. I can have several WIP in parallel
  • this is also reassuring because it's easier to lose stashed changes than committed changes; if my company's policy allows it (it does), I can even push the temporary branch to ensure I can't lose my changes even if something happens to my computer (while you can't push a stash)

Because of all that, I currently see absolutely no reason to ever use git stash. So... what am I missing? I assume the people making git wouldn't include such a command if you could already do everything rather simply without it

Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
Eternal
  • 2,648
  • 2
  • 15
  • 21
  • 1
    Why did someone tag this as a duplicate of https://stackoverflow.com/questions/20537223/what-is-the-intended-use-case-for-git-stash ? I'm not asking what can be done with git stash, I'm asking why you would do it that way when this seems like an inferior option to me – Eternal May 21 '21 at 08:06
  • I have edited my question to make it more clear how it's different from the other one. I'm asking for it to be reopened – Eternal May 21 '21 at 08:22

2 Answers2

2

The git commit is a part of the project history, which can be obviously reverted by the git reset command.

Stashing is a powerfull helper command to "temporarly park" your source code. You can choose to get it back by git stash pop or to drop it by git stash drop.

A frequent scenario where stash is a primary tool is the following:

  1. You modified the source code.
  2. You discover that you are on a wrong branch, for example on the master instead of feature branch.
  3. Now you stash the changes by git stash -u (the untracked ones too).
  4. Change branch by git checkout feature.
  5. Get back stashed changes by git stash pop.
Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74
0

for example you're experimenting something new and you broke something,

and you don't understand how that piece of code was working before,

so you stash the changes, have a look at the code that is working,

after you apply the stash back and fix it or undo some of your changes

buga
  • 852
  • 9
  • 21