Home Git The ultimate guide to using Git Stash on Linux

The ultimate guide to using Git Stash on Linux

Git Stash is a powerful feature for managing changes in your Git repositories on Linux. This guide covers the essentials of using Git Stash, from basic commands to advanced stashing techniques, helping you effectively manage your code changes and maintain a clean working directory.

by Arun Kumar
Published: Last Updated on
git stash tutorial

Hello, FOSS Linux enthusiasts and developers! As someone who’s navigated the turbulent seas of code changes and version control, I can’t stress enough how invaluable git stash has become in my daily workflow. In this blog, I’m excited to share with you the ins and outs of using git stash on Linux. Let’s dive in!

Understanding git stash

Before we get our hands dirty with commands, let’s understand what git stash does. Imagine you’re in the middle of a code change, and suddenly you need to switch branches to fix an urgent bug. But, you’re not ready to commit. That’s where git stash comes in handy – it temporarily shelves (or stashes) your changes so you can work on something else, and then come back to them later.

Here’s why git stash is so important for me :

  • Temporary storage for uncommitted changes: It provides a convenient way to save uncommitted work temporarily when you need to switch branches or perform unrelated tasks without losing your progress.
  • Clean working directory: It allows you to quickly clean up your working directory and create a clean slate for other tasks without committing your current changes.
  • Sharing changes easily: Stashes can be shared with other developers by exporting them and importing them into their repositories.
  • Flexibility: You can apply specific stashes instead of all of them, and manage them with various commands like listing, dropping, and applying with specific options.

While alternative approaches like branching or creating merge commits exist, git stash can offer a simpler and faster solution in certain situations. Overall, git stash remains a widely used and practical tool within the Git ecosystem.

Installing git on various Linux distributions

Before we can use git stash, we need to ensure Git is installed on our Linux system.

On Ubuntu/Debian-based systems

sudo apt-get update
sudo apt-get install git

On Fedora

sudo dnf install git

On Arch Linux

sudo pacman -S git

Once installed, you can verify the installation using git --version.

Starting with git stash

Step 1: Checking the status

Let’s start by checking the status of our repository. In your terminal, navigate to your Git repository and type:

git status

You’ll see a list of modified files. For example:

On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   README.md
    modified:   app.js

no changes added to commit (use "git add" and/or "git commit -a")

Step 2: Stashing your changes

To stash your changes, simply run:

git stash

The output will be something like:

Saved working directory and index state WIP on master: 049d078 added the index file

Step 3: Checking the stash list

To see what’s in your stash, use:

git stash list

Step 4: Reapplying stashed changes

When you’re ready to get back to your stashed work, you can apply the latest stash with:

git stash apply

Working with multiple stashes

What if you have multiple stashes? Here’s how to manage them:

Creating multiple stashes

Simply repeat the git stash command to create more stashes. Each stash is indexed.

Listing all stashes

git stash list

You’ll see something like:

stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c20a24d some other changes

Applying a specific stash

Use the stash index to apply a specific stash:

git stash apply stash@{1}

Removing stashes

Once you’ve reapplied your stashed changes, you might want to clean up your stash list.

Dropping a specific stash

git stash drop stash@{1}

Clearing all stashes

git stash clear

When to use git stash

I personally use git stash when I need a clean working directory quickly or when I’m juggling multiple tasks and need to switch contexts without committing half-baked code. However, it’s not a substitute for a thoughtful commit history. Use it as a temporary space, not long-term storage.

A quick and useful commands cheat sheet for your daily Git tasks

Command Description
git stash Stashes the current changes in your working directory.
git stash -u Stashes both tracked and untracked files.
git stash -a Stashes changes including ignored files.
git stash list Lists all the stashes you’ve currently stored.
git stash apply Re-applies the most recent stash without removing it from the stash list.
git stash apply stash@{index} Applies a specific stash identified by its index.
git stash pop Applies the most recent stash and then removes it from the stash list.
git stash drop stash@{index} Deletes a specific stash.
git stash clear Removes all stashes.
git stash push <file_or_directory> Creates a new stash with changes from the specified file or directory only.
git stash show stash@{index} Shows the changes recorded in a specific stash.
git stash show -p stash@{index} Shows the detailed changes (diff) of a specific stash.
git stash branch new_branch stash@{index} Creates a new branch from a stash and applies the stash to it.
git stash save "Custom Message" Creates a new stash with a custom message for easier identification.

FAQ: Navigating the Nuances of Git Stash in Linux

What happens to untracked files when I use git stash?

By default, git stash will not stash untracked files (files that are new and not yet added to the git repository). To stash untracked files along with your tracked changes, you can use:

git stash -u

Or, if you want to include ignored files as well:

git stash -a

Can I stash only specific files?

Absolutely! Sometimes you don’t want to stash everything. To stash specific files or directories, use:

git stash push <file_or_directory_path>

For example:

git stash push README.md

How do I view the contents of a stash without applying it?

Curiosity is a developer’s trait, isn’t it? To peek into the contents of a stash without reapplying it, use:

git stash show stash@{index}

For a more detailed view, add the -p flag:

git stash show -p stash@{index}

Can I create a branch from a stash?

Yes, and it’s a neat feature! If you want to create a new branch and apply your stash to it, do:

git stash branch new_branch_name stash@{index}

This command creates a new branch from the stash and removes the stash if applied successfully.

Is it possible to rename a stash?

Git stashes are not directly renameable since they’re referenced by their index. However, you can create a new stash with a custom message for better identification:

git stash save "My custom message"

What’s the difference between git stash apply and git stash pop?

git stash apply re-applies the stashed changes but keeps the stash in your stash list. git stash pop, on the other hand, re-applies the stash and then removes it from the stash list. Use pop when you’re sure you won’t need that stash again.

How secure is git stash for sensitive changes?

It’s crucial to remember that stashing is not a secure way to handle sensitive data. Stashes are stored locally in your Git repository, so anyone with access to your repository can access your stashes. Always handle sensitive data with care and consider more secure options if needed.

Can I recover a dropped stash?

This is a tricky one. In Git, when you drop a stash, it’s removed from the stash list. However, under certain conditions, it might be recoverable using Git’s reflog feature, but this isn’t guaranteed. It’s best to double-check before dropping a stash.

How does git stash impact remote repositories?

git stash is purely a local operation. It doesn’t affect the remote repository in any way. Any stashed changes are only present in your local Git environment.

Final thoughts

Git stash is a powerful tool in your Git arsenal, especially when you’re in a pinch. It keeps your working directory clean and allows for a seamless context switch between branches. As you get comfortable with it, you’ll find it an indispensable part of your daily coding workflow.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.