Git Stash vs Shelve in IntelliJ IDEA

John Wachira Jul 08, 2022
  1. Shelve Changes in Git
  2. Unshelve Changes in Git
  3. Stash Changes in Git
  4. Apply a Stash in Git
Git Stash vs Shelve in IntelliJ IDEA

This article will differentiate between git stash and git shelve while working with the IntelliJ IDEA. The two come in clutch when we want to switch between several tasks while working and return to them later.

The IntelliJ IDEA allows us to work on different features without losing our progress. We can either stash or shelve.

The two have a lot in common. The big difference comes when generating and applying patches.

When we stash, Git generates the patch, and we can use the IntelliJ IDEA or another IDE to apply the patch. On the other hand, when we shelve, the patches are generated and applied within the IntelliJ IDEA.

A stash will store all uncommitted changes, while shelving allows us to choose what to shelve.

Shelve Changes in Git

When shelving changes, we temporarily store uncommitted changes to work on them later if we need to switch to a different task. The IntelliJ IDEA allows us to shelve separate files or all changes.

You can apply the patch generated as many times as you need. Here is the typical workflow.

  1. On the IDE, we will navigate to the commit tool. Once there, we can right-click the change list or files we want to shelve and select Shelve Changes.

    git shelve changes

  1. Next, we will review our list, give our shelf a name in the Commit Message, and save the shelf by clicking Shelve Changes.

An alternative is using the Silent option. The IDE has a Shelve Silently option that allows us to shelve without displaying the dialog.

Unshelve Changes in Git

To unshelve changes, follow these steps.

  1. Navigate to the Shelf tab on the IDE and choose the files or change the list you need to unshelve.

  2. Tap Unshelve from the menu and select the list to which you want to restore the shelved changes. You can also create a new list if you do not want to restore them to an existing change list.

    Remember to mark the new list Active.

  3. You can alternatively use the Unshelve Silently icon and move the changes to the active change list.

Stash Changes in Git

A stash is simply a record of the difference between the current working directory and the HEAD commit. You can apply a branch patch as often as you need.

Keep in mind that:

  1. You may face conflicts while applying a stash.
  2. You cannot apply a stash to a working copy containing uncommitted changes.

Follow these steps to stash changes with the IntelliJ IDEA.

  1. Navigate to Git > Uncommitted Changes > Stash Changes.
  2. Select the correct directory and switch to the correct branch.
  3. It is always good to briefly describe the stash in the Message field.
  4. Click Create Stash.

Apply a Stash in Git

To apply a stash, follow these steps.

  • Select the correct directory and switch to the correct branch.
  • Choose the stash, click View to see what files will change after the patch is applied, and select Pop Stash to discard the patch after it is applied.
  • You can create a new branch and apply the patch by keying in the name of the branch in the As new branch field.
Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Stash