Git Stash Command

Learn via video courses
Topics Covered

Overview

Git stashing temporarily stores data in Git without committing the code to the repository. Git stashing takes the incomplete state of your code and saves it temporarily for future use.

Options are Available with Git Stash

Let's take a basic Github project which has two files in it. We'll change the files and then stash them or store them temporarily in Git to switch branches and work on other files. options-in-git-stash

Git Stash

Let's check the status of our files: status-of-files-in-git-using-git-status Now let us store them temporarily using the git stash command.

Syntax:

Example:

example-of-git-stash

Our work is temporarily saved using the git stash command and can now switch branches to work on other files.

Git Stash Save

You can save your files with a message with the git stash save command.

Syntax:

Example:

example-of-git-stash-save

The work will be saved with a message.

Git Stash List

This command is used to check the saved stashes.

Syntax:

Example:

example-of-git-stash-list

We made two stashes in the working directory, so the list is displayed.

Git Stash Apply

The files in a stash will be returned to the development workspace, but the stash will not be deleted from history.

Syntax:

Example:

example-of-git-stash-apply

The output says "Changes not staged for commit:", which means it restores the previous state before stash.

Git Stash Changes

The git stash changes command allows us to track the changes made before and after using the git stash command.

Syntax:

Example:

example-git-stash-changes

The above output tells us that there has been one insertion and deletion in the design.txt file.

Git Stash Pop

The git stash pop command allows users to reapply previous commits. With the popping option, your stash changes will be removed and applied to your working file.

Like git stash applies, git stash pop works similarly. One of the main differences between these two commands is the git stash pop command removes the stash once it has been applied.

Syntax:

Example:

example-of-git-stash-pop

Git Stash Drop

The git stash drop command is used to delete a stash. Generally, it deletes the most recent stash from the queue. Be careful while deleting the stash because it is difficult to return once deleted.

Syntax:

Example:

example-of-git-stash-drop

In the above image, we can see that previously the list contained three stashes, and after deleting the most recent stash, there are two stashes in the queue.

Git Stash Clear

All available stashes can be deleted simultaneously with the git stash clear command.

Syntax:

Example:

example-of-git-stash-clear

Git Stash Branch

A conflict may arise during merging when some work is stashed on a given branch, and the branch is then continued to be worked on. So, here comes the git stash branch command, which allows saving the work on a different branch.

Syntax:

Example:

example-of-git-stash-branch

It creates a new branch and saves the work on that branch.

Git Stash vs Commit

A git commit and a git stash command take snapshots of modified files in the git working tree and then store them for reference in the future. The main difference is that Git commits are publicly available, while stashes are stored locally.

Git Stash vs Reset

Git stash and git reset are hard to accomplish the same thing, reverting all files in the working directory to the point at which they were before.

Which Folder Stores Git Stash History?

In the hidden folder .git/refs/stash, you will find the latest version of your git stash. The reflog of this folder contains all other stashes.

Git Stash Best Practices

The git stash command is vital for developers to save their work.

The git stash command is a utility that allows a developer to save modified files temporarily. These local stashes do not allow code to be seen by other developers, nor do they replicate the code to other git repositories like GitHub or GitLab.

The best practice is to create development branches and commit to them regularly. In this way, other developers can see the work after pushing the code to Github. When files are temporarily shelved and to be returned within a short time frame, the stash command makes the most sense.

Conclusion

  • A git stash command allows you to temporarily save your changes to your working copy while you work on another file and then reapply later.
  • There are several options available with Git stash:
    Git stash saves, Git stash list, Git stash apply, etc.
  • A git commit and a git stash command save modified files in the git working tree and then store them for reference in the future.
  • The best practice is to create development branches and commit to them regularly.