Git Stash

Author:

Git Stash

Git Stash is a built-in command that temporarily stores all of the most recent modifications to the code. That is, the modifications are stored, and the code is reset to its previous state.

Git is software that allows a group of developers to keep track of changes and alterations to a source code throughout the development of software.

A seamlessly working software is not a one-person job. A crew of devoted developers devotes their time and talents to creating high-quality software. Git comes to the rescue here since it provides a platform for developers to generate, edit, and exchange code.

For those looking for a job as a software engineer, understanding and experience with Git is not only a plus but also a necessity.

Git is a software development platform that includes a number of instructions that make the developer’s job easier. Git Stash is one such command.

What is Git stash, and why is it necessary to use Git?

Let’s delve into the article to know why.

What is Git Stash?

Git Stash, also known as stashing, is a valuable feature that allows programmers who aren’t sure what they’re doing to save their changes independently until they can figure it out.

How to Create and Save Git Stash?

Creating a Git stash is as easy as pie. All you have to do is run the git stash command without any other commands, as shown below:

$git stash

All changes staged for commit in your current working directory will be saved on the side for later use after running the command described above.

The result after running the above command will be, for instance, as follows:

$ git stash

Saved working directory and index state WIP on branch1: 809c568 Initial commit

The git stash stores the changes made in the working directory locally that is inside your projects directory as /.git/refs/stash

The stash not only lets you save the changes but also helps you retrieve the changes whenever you need them.

It helps a lot when you need to switch between contexts, enabling you to preserve changes that you may require later, and is the quickest way to clean up your working directory while leaving modifications intact.

How to Run Git Stash? 

Before we mentioned how to run the Git stash command, here is a more extensive description of how to use Git Stash.

  1. Save changes to branch A.
  2. Run the git stash command.
  3. Check out branch B.
  4. Fix the bug in branch B.
  5. Commit your work and push it to the remote.
  6. Check out branch A
  7. To get your stashed changes back, run git stash pop.

Git stash pop is the command used to retrieve the stored changes or files from the stash and put them back in the development environment. After completing its job, the stash is deleted from the history.

Likewise, there are some other standard git stash options you will need to know:

  • Git stash push: This command creates a new stash and then reverses the status of all updated files.
  • Git stash applies: This command returns the files in a stash to the development workspace, but it does not remove the stash from history.
  • Git stash list: git stash list is a command that displays the history of your stash in chronological order.
  • Git stash clear: git stash clear removes all entries in the git stash history.

Retrieving Git Stash

The purpose of using Git stash is to store the temporary changes which may come in help for later use. This means there are special commands which serve this purpose.

The pop and apply commands are used to retrieve the changes applied in the latest stash stash@{0}.

Though the goal of both is to retrieve the changes, the results are substantially different. After retrieving, the pop deletes the stored stash, whereas the apply keeps a copy for subsequent usage.

They both are used as follows:

Git Stash Pop

$ git stash pop stash@{1} 

Git Stash Apply

$ git stash apply stash@{1}

Removing the Stash

Sometimes, the stored stashes may not be in use or are forgotten. If that’s the case, instead of leaving the stash stored, the better choice would be to remove them on time.

Cleaning the stash can be done by using the following commands:

  • git stash clear: 

The git stash clear empties the stash list by removing all the stashes. The clear command is used as follows:

$ git stash clear

  • git stash drop <stash_id>:

The git stash drop <stash_id> deletes a particular stash from the stash list. The drop command is used as follows:

$ git stash drop stash@{stash_index}

Conclusion

One of the most important aspects of any software development project is making regular modifications to the source code. Though it is unavoidable, many developers find themselves in situations where they are unsure of their modifications, which is why Git Stash is such a valuable command that allows them to save, retrieve, and apply their changes once they are comfortable with them.

If what you’ve just read is all new to you, why not dip your toe in the water of code? Try our free 5 Day Coding Challenge and learn the basics of HTMLCSS and JavaScript. Register now through the form below. 

Alejandro's Journey to Software Development

Welcome to the inspiring tale of Alejandro, an adventurous individual from Argentina who embarked on a transformative journey into the world of software development through Code Institute. His story is a testament to the power of determination and continuous learning. From Argentina to Ireland: A Leap of Faith Alejandro moved from Argentina to Spain in […]

How to Become a Software Engineer

If you are fascinated by the world of technology or curious about how software applications work, a software engineer career might be the perfect fit. In this guide, we look at the journey of becoming a software engineer, from the necessary qualifications to the steps that can lead you to success in the ever-evolving tech […]

Python & Data Science

Data science has emerged as a pivotal field that empowers businesses and researchers to make informed decisions through data analysis. Python, a versatile programming language that has become synonymous with data science, is at the heart of this dynamic domain. In this blog, we look at why Python is a key player in data science, […]