DEV Community

Brijesh Shah
Brijesh Shah

Posted on • Originally published at tuls.in

1 1

git stash, to the rescue

Introduction

git, the version-control system we all use. It has helped save a tremendous number of work hours by simplifying the process to track changes in source code during software development. Today I will try to demystify one of its commands to help you save some more time.

Use cases

Lets be real, you often come across a situation where you are doing what you do best, writing some awesome code. But, suddenly you are required to just make your changes disappear and test some other code. Now, you are not ready to commit your changes yet. What do you do? The answer to this issue is the git stash command.

The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy. For example:

Dirty state

$ git status
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   index.html

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:   lib/simplegit.rb

Stashing the changes

$ git stash
Saved working directory and index state \
  "WIP on master: 049d078 Create index file"
HEAD is now at 049d078 Create index file

Clean state

$ git status
# On branch master
nothing to commit, working tree clean

You can reapply previously stashed changes with git stash pop.

$ git stash pop
On branch 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:   index.html
    modified:   lib/simplegit.rb

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

Bonus tip, you can also stash untracked files using git stash --include-untracked.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay