1. 19
  1. 34

    How common is it to find git stash scary? I can’t recall hearing that from anyone I’ve worked with. The manpage has a clear explanation of what it’s for and how it works, with multiple examples covering common use cases.

    1. 19

      I think stash is an odd corner of Git.

      • It doesn’t modify the commit graph. It’s really easy to stash stuff and forget about it. I’ve definitely rewritten code because I forgot it was stashed.
      • It’s an optional part of your workflow. I use git stash but I know plenty of people who have used git for years who don’t touch it. This means it’s not mentioned in “Intro to Git”-type resources.
      • git stash drop is one of the few ways to lose your work. I’ve lost more work stash drop than to reset --hard

      I really like git stash now, but I have learned to avoid keeping important code stashed for very long.

      1. 8

        Yeah, my stash tends to grow with dead code-paths and I have to clean it out every so often. IMO it’s particularly useful as a method to move uncommitted changes to a different a branch. The syntax of stash save will never not confuse me though, it’s so unergonomic.

        1. 2

          Hmm, in what sense does it not modify the commit graph? It seems like it does to me

          git init
          echo hello > a
          git add a && git commit -m "Added a"
          echo goodbye > a
          git stash
          git log --decorate --all --graph --pretty=format:"%C(auto)%h %<(7,trunc)%C(auto)%ae%Creset%C(auto)%d %s [%ar]%Creset"
          *   232e380 tom-g.. (refs/stash) WIP on master: 1a86647 Added a [2 seconds ago]
          | * ba5c812 tom-g.. index on master: 1a86647 Added a [2 seconds ago]
          * 1a86647 tom-g.. (HEAD -> master) Added a [10 seconds ago]
        2. 8

          I don’t find it scary as such, but feel it’s messy and tends to lose me work. Typically, by running into conflicts that I can’t resolve with git stash pop (possibly by accidentally popping the wrong change). I expect there might be a way to recover, but that’s where it typically ends and I start over from scratch.

          1. 2

            Makes sense. Popping the wrong change by mistake and thus losing it would hurt. I almost always use git stash apply instead of git stash pop, though obviously sticking with apply has the side effect of the stash list growing over time.

          2. 3

            How many here learned git with a GUI? I use git stash all the time without thinking about it but I remember avoiding it for a couple years after I somehow shot myself in the foot with it using a GUI. I only use the cli and scripting at the moment.

            1. 2

              I wouldn’t call it scary, however it’s common for guides to caution against it, and advocate replacing it with temporary commits.

              The potential for merge conflicts and subsequently losing work when doing git stash pop are the one part I might call scary.

              1. 2

                I find stash scary because if you make a mistake with it you cannot fix it with the reflog. I use it very gingerly and only when I know my editor is also making backups.

              2. 9

                The thing with git stash is that it essentially just duplicates everything you can do with branches and commits, but, IMHO, not really better. I don’t really see anything in this article that’s a clear advantage of stashing either.

                And commands such as git stash apply stash@{1} are just … ugh.

                The problem I had with git stash is that I would build up a queue of 7 different things, and then figuring out what’s what isn’t too easy. Making a named (local) branch is just a lot easier.

                1. 8

                  I typically use stashes when I’m not ready to commit.

                  For example, I’ll often work on something for an hour and then realize I forgot to git pull. To fix, I stash, pull, and then stash pop. If I later decide I don’t want to commit the code, I can just reset --hard and move on. If I’d instead committed, I would have to modify history or add a revert commit.

                  I almost never revisit old stashes and I periodically drop all my stashes to minimize clutter.

                  1. 3

                    Same workflow here. I recently learned about git pull —autostash which is 1 command instead of 3.

                  2. 6

                    The problem I had with git stash is that I would build up a queue of 7 different things, and then figuring out what’s what isn’t too easy. Making a named (local) branch is just a lot easier.

                    Have you tried adding comments to your stashes? To me this is what helped to make it understandable and usable.

                    1. 5

                      One nice thing about stash is that it can preserve both the index and the working tree. So if you are in the middle of a change and have git added some files but not others, git stash push followed by git stash pop will restore your previous state in a way that git commit -a wouldn’t.

                      1. 2

                        For the record, you can name your stashes when you create them to make it easier to know what’s what.

                      2. 5

                        Scary? If anything it gives you peace of mind.

                        You can give stashes name for easier navigation later on retrieving. If you save important.stuff there you probably want to favour apply instead of pop.

                        Ir you use fish Shell and press tab after you type “git stash apply” it lets you pick the stash interactively from a list.

                        1. 2

                          My favorite thing I learned about git stash recently is that you can just go edit the stash ref list file if you want to give your stashes better names. Just open up .git\logs\ref\stash in your favorite editor and rename the stashes as you see fit.

                          It looks something like:

                          0000000000000000000000000000000000000000 b77a4fd5c052a9ec212780eca4952540ad882bcb Author <author@example.com> 1563653910 -0700     My Special Stash Message just for lobste.rs
                          b77a4fd5c052a9ec212780eca4952540ad882bcb 36ab6d95465bcce60636aa398cdf95350f5fc688 Author <author@example.com> 1564602521 -0700     Example of another stash message that I edited just for lobste.rs

                          Also, if your list of stashes ever grows to a large amount, you can use tig to browse the stash list with tig stash. In that view by default maps the ! key to drop the stash. This lets you easily browse through the list of stashes, seeing what’s in them, and dropping all the stuff you don’t care about.

                          1. 2

                            Git stash scary? Git stash is a key part to how I work in git.

                            Make changes, decide nah, this isn’t quite right, lets sit on it a bit. Stash away, give it a good name, sometime later come back to it and make it a real commit/branch and pr. Boom done.

                            Also good for a cheap stack of local only changes you want to reapply to new branches you create or review.

                            1. 2

                              I got thrown off by seeing the title and the site domain, expecting to see tooling for managing Git stashes, named as a pun, as “jemma” would be a fine colloquial-Finnish translation for “stash”!

                              (Like “kätkö” would be more official, but also harder unicode-wise. So many projects been named in strange languages and puns…)

                              A good write-up nonetheless :)

                              1. 1

                                Lately I‘ve been using worktrees for interruptions. A feature I don’t see mentioned often.

                                1. 1

                                  Nice write up. While show was mentioned, I feel this is missing git stash show stash@{3} -p to review the changes a patch contains. I’ve also used this to resolve conflicts by dumping the patch to a file that I can easily modify before applying.

                                  $ git stash show stash@{3} -p > tmp.patch
                                  $ vim tmp.patch
                                  $ git apply tmp.patch
                                  1. 1

                                    Imagine coming into a thread and reading about people using stash properly while I use it as a sweat-free git reset when I did a tiny modification and there is a merge conflict. Oh well, this might be the day that I learn :D

                                    1. 0

                                      Let me again plug Fork, a really great Git client for macOS and Windows.It’s not open source, and it’s not free ($50), but it’s worth every penny. It makes stashes very easy and intuitive — they even show up in the history view, as though they were a 1-commit branch.