Published on

Git Stash Explained!

Do you think you know Git? Have you ever heard of Git Stash? Do you know what it does? If you want the answers to these questions then just keep reading as this blog post will be about these topics. Ok, let's get into it!

What is Git?

Git is a very popular, quite possibly the most popular, version control system founded by the legendary software engineer Linux Torvalds in 2005.

Git is a DVCS, also known as a Distributed Version Control System, which means it is not limited to only having one single place for the complete version history of a given software. Instead, every developer working on a given software using Git will have their own distinct copy of that software that they are free to modify, however they chose to do so.

Git logo

Figure 1: Git logo

What does Git Stash do?

Git Stash is used to primarily store the current state of the working directory and the index. If you're wondering what the definition of Git index is: it is a staging area between the working directory and repository. The currently tracked files are only pushed from the Staging Directory to the Remote Repository after running the following commands:

git commit -am 'fixed navbar display issue'
git push origin master

After the current state of the project is store away using the git stash command, Git automatically reverts the working directory to match the HEAD commit. The term HEAD refers to the current commit you are viewing when using Git.

Note that although Git Stash has a number of arguments available for use, the git stash command is synonymous with the git stash push command. That is, both aforementioned commands save your local modifications as a stash entry. A stash entry, in case you didn't know, is an entry that contains the name of the branch that was currently checkout when the entry was made and a brief description of the commit the entry was based on. Also note that the naming convention given to the each stash entry is based on a system where stash@{0} represents the latest entry, stash@{1} represents the second latest entry, stash@{2] represents the third latest entry and so on...

It's true :)

Figure 2: It's true :)

What are some useful arguments for the Git Stash command?

Here are some of the most helpful arguments when using the git stash command such as:

  • push: Used to save local modifications to a new stash entry then the user can continue to make changes to the HEAD commit. Overtime the HEAD commit will be ahead commit-wise when compared to the stash entry
  • save: Very similar to push except for the fact that it cannot take a pathspec. In case you didn't know, a pathspec is the mechanism that Git uses in order to limit the functionality of a Git command to a subset of the main repository, for example adding the following exclusion filter to the .gitignore file is considered to use the pathspec:
# .gitignore file
**/bin/Debug/
**/bin/Release/

  • list: Used to list all the current stashed entries. The stash entries are listed in the following format: stash@{0}, stash@{1}, stash@{2}, ...
  • pop: Used to remove one stash entry from the stash list and relocate to the top of the current working tree state. Basically, it's the opposite operation of git stash push
  • clear: Used to remove all stash entries from current working tree. Note that after the entries have been cleared, it is not possible to recover them
  • branch <branchname> [<stash>] : Used to create a new branch that begins with the commit at which the stash entry object(denoted by the optional <stash> parameter) was initially created. Then, the changes that are recorded in the stash entry: <stash> are applied to the new working tree and index. Finally, if the changes are applied successfully the <stash> stash entry is dropped

Conclusion

Well that's it for this post! Thanks for following along in this article and if you have any questions or concerns please feel free to post a comment in this post and I will get back to you if I find the time.

If you found this article helpful please share it and make sure to follow me on Twitter and GitHub, connect with me on LinkedIn and subscribe to my YouTube channel.