Git
Stash
Shelve
IntelliJ IDEA
Version Control

Git Stash vs Shelve in IntelliJ IDEA

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Git offers developers various tools for managing their repositories, among which are the `git stash` command and the IntelliJ IDEA's `shelve` functionality. Both are invaluable when it comes to preserving changes without committing them, but they serve slightly different purposes, and their usage within IntelliJ IDEA also introduces unique nuances. This article delves into these two powerful tools, comparing their functionalities, use cases, and integration within IntelliJ IDEA.

Technical Overview

Git Stash

Git stash is a command-line utility that temporarily saves changes in your working directory so you can switch branches, pull changes, or perform other tasks without committing the changes. The stashed changes remain on a stack until you apply them or drop them.

Key Features:

  • Stack Implementation: Stashes are managed as a stack, allowing multiple stashes to be saved and managed with push (`git stash`), pop (`git stash pop`), and apply (`git stash apply`) operations.
  • Selective Stashing: Use the `git stash -p` option, which allows you to choose specific chunks of code to stash.
  • Include/Exclude Untracked Files: With `git stash -u` and `git stash -a`, you can stash untracked and all files, respectively, along with the changes in tracked files.

Example Usage:

  • Graphical User Interface: Shelving is integrated into IntelliJ IDEA's GUI, making it accessible to those who prefer not to use command-line tools.
  • Per-Project Basis: Changes are shelved and unshelved on a per-project basis, which is useful when working on multiple projects simultaneously.
  • Integration with Local History: Shelving is integrated with IntelliJ’s version control support, which benefits from IntelliJ's Local History feature, offering an additional layer of safety.
    • Open the "Version Control" window (`Alt + 9`)
    • Navigate to the “Local Changes” tab.
    • Select the changes to shelve and click on “Shelve Changes”.
    • Go to the “Shelved Changes” tab in the Version Control window.
    • Select the shelved changes you want to restore.
    • Click on “Unshelve Changes”.
  • Git Stash is effective when working with multiple repositories, as it operates at the Git level.
  • Shelve is more project-oriented and doesn't easily allow moving shelved changes between different repositories.

Course illustration
Course illustration

All Rights Reserved.