Git
Stash
Shelve
IntelliJ IDEA
Version Control

Git Stash vs Shelve in IntelliJ IDEA

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.