479

I am very unfamiliar with the shelve aspect of Git (EDIT: not an aspect of Git, but rather IntelliJ IDEA feature of shelve).

If stash is used to put aside unfinished work what is shelve then? What would you use it for?

For example on Update Project (from VCS menu)

Update Project option

one will get (in IntelliJ IDEA 2019.2)

Update Project prompt window

Queeg
  • 7,748
  • 1
  • 16
  • 42
Subtubes
  • 15,851
  • 22
  • 70
  • 105
  • 26
    `shelve` is not a git command. What's the context for this question, where are these terms coming from? `shelve` exists in some other tools but it's not part of git. – Jonah Jan 18 '15 at 08:11
  • 2
    Git stash is similar to `shelve` in bzr, hg, etc. Are you referring to some git interoperability package? – drRobertz Jan 18 '15 at 08:12
  • 21
    more upvotes than the accepted answer probably confirms, this question is valid and the term ''Shelve" is equally misunderstood as a git command. Thanks for asking, this was my doubt too (from IDEA too). – PravyNandas Jun 01 '20 at 14:27
  • 1
    Slightly different issue, but if Google took you here because you can't find your Shelf tab, check out my answer here: https://stackoverflow.com/a/71167546/1679571 – Randy Feb 18 '22 at 01:59

6 Answers6

421

git shelve doesn't exist in Git.

Only git stash:

  • when you want to record the current state of the working directory and the index, but want to go back to a clean working directory.
  • which saves your local modifications away and reverts the working directory to match the HEAD commit.

You had a 2008 old project git shelve to isolate modifications in a branch, but that wouldn't be very useful nowadays.

As documented in Intellij IDEA shelve dialog, the feature "shelving and unshelving" is not linked to a VCS (Version Control System tool) but to the IDE itself, to temporarily storing pending changes you have not committed yet in changelist.

Note that since Git 2.13 (Q2 2017), you now can stash individual files too.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 127
    Turns out I misunderstood the concept. I thought it was a Git command when in fact it is a third party thing from IntelliJ IDEA. I could not find the git docs for it so thought I was missing something. https://www.jetbrains.com/idea/help/shelving-and-unshelving-changes.html – Subtubes Jan 19 '15 at 04:37
  • 5
    Your doc link indicates that "shelves" are set of changes (patch) managed by IntelliJ IDE only, unlinke "stashes" that are standard things managed by Git. So avoid shelves. – barbara.post Feb 25 '15 at 07:52
  • @barbara.post avoiding shelves? are you sure it's the right thing to do in case I want to stash only one modified file? – Jerry Chin Jun 20 '17 at 07:38
  • 3
    @jerry chin shelve is better than stash in your case. I wish if shelve was present in git. Webstorm sucks out the RAM. It would be usefull if git had it we could run it in cmd line – digender mahara Nov 29 '17 at 05:29
  • 3
    There is a common use case that shelve has that stash doesn't address - dealing with problematic co-workers/company practices. Its possible for complete garbage to get added to your repository and for to be not kosher to 'fix' it because of somebody's insanity. The alternative is something like `git stash && && git stash pop` but that sucks. Auto-shelve and apply is closer to what we need. – smaudet Aug 16 '18 at 12:35
  • @smaudet Is it like ingkevin's comment? (https://stackoverflow.com/questions/28008139/git-shelve-vs-stash/28008816?noredirect=1#comment84379481_29452148) – VonC Aug 16 '18 at 12:38
  • 1
    @VonC similar not the same - I have a use case where the projects I'm using have settings I require that can't be committed because they 'aren't standard'. And half a dozen other use cases I've come across involving things like needing modify build files etc. for local development vs dev/test/prod. – smaudet Aug 17 '18 at 20:40
249

When using JetBrains IDE's with Git, "stashing and unstashing actions are supported in addition to shelving and unshelving. These features have much in common; the major difference is in the way patches are generated and applied. Shelve can operate with either individual files or bunch of files, while Stash can only operate with a whole bunch of changed files at once. Here are some more details on the differences between them."

piers7
  • 4,174
  • 34
  • 47
Yekver
  • 4,985
  • 7
  • 32
  • 49
  • 22
    It seems that **shelve** is more flexible than **git stash**. – Dmitry Davydov Sep 28 '16 at 12:15
  • 15
    @DmitryDavydov There's `git stash -p` which trumps both. Unfortunately only in the command line. – The Vee Nov 29 '16 at 00:11
  • 1
    @TheVee yes it's very similar to shelve in terms of functionality, thanks for info. – Dmitry Davydov Nov 29 '16 at 08:20
  • 14
    Actually, since **Git 2.13 (Q2 2017)** you can stash individual files... [read more](http://stackoverflow.com/questions/3040833/stash-only-one-file-out-of-multiple-files-that-have-changed-with-git/3041055#3041055) – kyb May 13 '17 at 09:12
  • 1
    And there is also interactive more `git stash -p`. [read more](http://stackoverflow.com/a/17969785/3743145) – kyb May 13 '17 at 09:15
  • 8
    I found **shelve** very useful when you have a patch of changes that you want to apply every time that you want to auto generate documentation for example. **Shelve will create a file within .idea/shelve and you can add it to you VCS and share it with all your team** so they can apply those changes and run the same tasks. – ingkevin Feb 08 '18 at 16:50
121

In addition to previous answers there is one important for me note:

shelve is JetBrains products feature (such as WebStorm, PhpStorm, PyCharm, etc.). It puts shelved files into .idea/shelf directory.

stash is one of git options. It puts stashed files under the .git directory.

valex
  • 5,163
  • 2
  • 33
  • 40
36

I would prefer to shelve changes instead of stashing them if I am not sharing my changes elsewhere.

Stashing is a git feature and doesn't give you the option to select specific files or changes inside a file. Shelving can do that but this is an IDE-specific feature, not a git feature:

enter image description here

As you can see I am able to choose to specify which files/lines to include on my shelve. Note that I can't do that with stashing.

Beware using shelves in the IDE may limit the portability of your patches because those changes are not stored in a .git folder.

Some helpful links:

Hamza Belmellouki
  • 2,516
  • 1
  • 18
  • 39
6

Shelf is a JetBrains feature while Stash is a Git feature for same work. You can switch to different branch without commit and loss of work using either of features. My personal experience is to use Shelf.

O_K
  • 922
  • 9
  • 14
2

Shelve is an IntelliJ IDE feature. Stash is a Git feature. Newer versions of git also allows you to stash individual files using

git stash -p

IMO, an advantage Shelve has over plain stash is the ability to shelve changes from multiple repos together

riyasvaliya
  • 745
  • 6
  • 8