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 ...)
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-states
. Git has a built-in model named 3-states
which is the internal git structure for working with local repository.
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 modifications 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.
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.
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 allowing 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 saves us the need to use git stash
or even to clone a new repository, since those worktrees share 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.