Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a stash?

I have googled and searched in various places for a while, but have not found a good answer. What is a stash, and what is it used for?

(Source of confusion: using beautiful Fuel with Fossil, clicked the "Stash changes" button, see files in there with question marks, and don't know what to do with them ...)

like image 494
Ralf Avatar asked Jun 29 '12 11:06

Ralf


Video Answer


2 Answers

First of all: let us understand why do we need to use stash?

In order to understand what stash is, we first need to understand the 3-stats. Git has a build in model named 3-states which is the internal git structure for working with local repository.

enter image description here

The "problem" with the 3-states is that there is one per repository and not one per branch. So when we switch branches the only thing that is being modified is the HEAD which points to a different commit.

In git a branch is only an alias to a given commit so switching branches as explained before only changes the HEAD while leaving the working directory && stage unchanged and leaving all the modification as is. [Working directory is being updated with the required files which the new branch has but its not part of our explanation.]

So if we add new files, modified some others and now we wish to move to a different branch we will have dirt left out in our working directory and stage area as seen below.

We have some dirty work following us regardless of the branch we are now working on.

enter image description here

So how can we work on multiple branches?

Most of the git users use stash in order to gain the ability to work simultaneously on multiple branches. git stash is the basic way to accomplish it since git stash saves our work in a separate zone named stash.

We then can checkout the code at any given time for any given branch. enter image description here


So far so good.

Where is the problem and why not use stash in first place?

The problem is that when using stash we don't have the ability to really work on multiple branches since we will have to stash every time we wish to switch branches.

Another issue is, that we can pull the stash code to the wrong branch and than we have to figure out which files are the correct ones, if we did a mistake.

So how can we really work on multiple branches?

Git had this ability since 2007. Under the contrib folder the was a hidden command named 'new-workdir' which was later on added to git in version 2.5 and was renamed to git worktree.

git worktree

git worktree will create a new working folder allow us to work on multiple branches on the same time. Each copy will point to the origin repository while the 3-states are a new and fresh copy. This save us the need to use git stash or even to clone a new repository, since those worktrees shares the same repository, we can checkout any branch on any worktree, we can do a cherry-pick or merge and all will be done locally on our machine.

Usage:

git worktree add <second path>

will create another folder on your computer which allows you to work on a different branch simultaneously.

This will allow you to do any experiments on the new worktree without having any effect on the repository itself. In the attached image you can see that there are 2 separate working folder but both of them are using a single repository and share the content.

enter image description here

like image 71
CodeWizard Avatar answered Oct 18 '22 10:10

CodeWizard


Fossil, Git, and possibly other revision control systems share the idea of a stash. Pro Git has a section on stashing. It says, in part:

Stashing takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time.

In other words, it's a way to save your current work while doing something else, without making a "real" commit or affecting your repository history.

like image 23
Todd A. Jacobs Avatar answered Oct 18 '22 09:10

Todd A. Jacobs