Why use stashing?

  • save sketches of code for possible later use without commiting
  • switch to another branch without committig your changes
  • potentially enables you to transfer code from one branch to another

commands: basics for stashing

  • git stash : save your current changes to your branch as a stash (coll. sketch)
  • git stash list : list all your stashes in the current repository. You’ll see stashes from all branches, even if you are currently on a different branch.
  • git stash apply : use code which you saved earlier (in a stash) and include it in the current place of your repository
  • git stash drop : delete a previously saved stash
  • git stash pop : apply the changes of the target stash at the current position and delete the saved stash at once

How to save code for later use?

Did you messed around with your code?
Have you done some changes which might be interesting in the future?
Then you may want to save the code without committing it.
So that you may use this code again later, if you want.
Or you can just forget it and wipe it from your mind without feeling guilty.
You can use stashing.
Here is how:

  1. mess with your code 🤪 (you probably did this already✔️😄)
  2. check the status of your current changes using git status (optional)
  3. save the changes using git stash
C:\Users\Name\codingprojects\CoderMatching (createAd)
λ git status
On branch createAd
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   codermatching/codermatch/views.py
        modified:   codermatching/templates/codermatch/createAd.html

no changes added to commit (use "git add" and/or "git commit -a")

C:\Users\Name\codingprojects\CoderMatching (createAd)
λ git stash
Saved working directory and index state WIP on createAd: 2a938de Merge branch 'createAd'

This automatically saves the most recent changes which you did after your previous commit. At the same time it sets your code back to this last commit.
Now you can work on your current branch as it was before your started to tinker with it.
Or you can change to other branches as if you’ve just saved the previous commit.

Later you can find your saved stashes which include your non-commited code. And you can apply the changes from your stashes to later versions or other branches of your code.

How to find saved code?

To find your saved stashes from the past you can use the git stash list command.
It shows a numbered list of your stashes.
It also shows on which branch from your repository it was created.
And it even shows the related commit.

C:\Users\Name\codingprojects\CoderMatching (createAd)
λ git stash list
stash@{0}: WIP on createAd: 2a938de Merge branch 'createAd'
stash@{1}: WIP on createAd: 2a938de Merge branch 'createAd'
stash@{2}: WIP on createAd: 2a938de Merge branch 'createAd'
stash@{3}: WIP on createAd: 2a938de Merge branch 'createAd'

This may be very helpful, if you want to apply these saved changes later to a more recent version of your code.
Because if you know the exact branch and commit on which you’ve stashed your changes earlier, then you’ll be able to go back to exactly that point and inspect the changes.

Use the saved code later

Using the git stash apply command, you can apply the respective stash.
In the documentation they say, that you should use the complete title stash-version (e.g. git stash apply stash@{3}).
But I figured out that you can simply use the stash-number (e.g. git stash apply 3).

C:\Users\Name\codingprojects\CoderMatching (createAd)
λ git stash apply 3
On branch createAd
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   codermatching/codermatch/views.py
        modified:   codermatching/templates/codermatch/createAd.html

no changes added to commit (use "git add" and/or "git commit -a")

Delete stashes

After applying your saved stash you may want to delete it, if you don’t want to save the stash itself anymore.
Or maybe just want to discard them because you are not going to use it anyway.

To delete stashes you can use the git stash drop.
Like in git stash apply you can just use the stash number here too.
This will delete the respective stash from the list, so that you can not use it again.

C:\Users\Name\codingprojects\CoderMatching (createAd)
λ git stash drop 3
Dropped refs/stash@{3} (2bd6f2efda6528939c5e5b9c6d6643ab25bc3455)

C:\Users\Name\codingprojects\CoderMatching (createAd)
λ git stash list
stash@{0}: WIP on createAd: 2a938de Merge branch 'createAd'
stash@{1}: WIP on createAd: 2a938de Merge branch 'createAd'
stash@{2}: WIP on createAd: 2a938de Merge branch 'createAd'

You can also apply the changes from your target stash and delete the stash immediately, in one step.
Use git stash pop for that.

C:\Users\Name\codingprojects\CoderMatching (createAd)
λ git stash list
stash@{0}: WIP on createAd: 2a938de Merge branch 'createAd'
stash@{1}: WIP on createAd: 2a938de Merge branch 'createAd'

C:\Users\Name\codingprojects\CoderMatching (createAd)
λ git stash pop 1
On branch createAd
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   codermatching/codermatch/views.py
        modified:   codermatching/templates/codermatch/createAd.html

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{1} (c989516b3c9ed84181df47c43c724e5c483e1924)

C:\Users\Name\codingprojects\CoderMatching (createAd)
λ git stash list
stash@{0}: WIP on createAd: 2a938de Merge branch 'createAd'

Is it possible to transfer code to a different branch?

Have you done some changes to your current branch?
And maybe you want to transfer it to another branch in your repository without committing your changes to your current branch?
It may be possible, using stashing!
Here I am not completely sure…
Does it even make sense or are there solutions which are more suitable?🤔

My idea is this:

  1. Save your changes as stash (git stash) on your current branch
C:\Users\Name\codingprojects\CoderMatching (createAd)
λ git stash
Saved working directory and index state WIP on createAd: 2a938de Merge branch 'createAd'

C:\Users\Name\codingprojects\CoderMatching (createAd)
λ git status
On branch createAd
nothing to commit, working tree clean
  1. switch to another branch (git switch)
C:\Users\Name\codingprojects\CoderMatching (createAd)
λ git switch master
Switched to branch 'master'

C:\Users\Name\codingprojects\CoderMatching (master)
λ git status
On branch master
nothing to commit, working tree clean
  1. apply stashed changes to different branch (git stash apply <stash-version>)
C:\Users\Name\codingprojects\CoderMatching (master)
λ git stash list
stash@{0}: WIP on createAd: 2a938de Merge branch 'createAd'

C:\Users\Name\codingprojects\CoderMatching (master)
λ git stash apply 0
Auto-merging codermatching/templates/codermatch/createAd.html
On branch master
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git restore ..." to discard changes in working directory)
        modified:   codermatching/codermatch/views.py
        modified:   codermatching/templates/codermatch/createAd.html

no changes added to commit (use "git add" and/or "git commit -a")
  1. commit your changes on the other branch (git commit)
C:\Users\Name\codingprojects\CoderMatching (master)
λ git add .

C:\Users\Name\codingprojects\CoderMatching (master)
λ git status
On branch master
Changes to be committed:
  (use "git restore --staged ..." to unstage)
        modified:   codermatching/codermatch/views.py
        modified:   codermatching/templates/codermatch/createAd.html


C:\Users\Name\codingprojects\CoderMatching (master)
λ git commit -m "transfer stashed code from createAd to master branch"
[master 264f798] transfer stashed code from createAd to master branch
 2 files changed, 14 insertions(+), 11 deletions(-)

This worked fine for me, when my master and the createAd branch where equivalent.
But it also worked when I has trivial changes on the between the two branches (nothing which would be an issue while merging).
But could I also apply the changes on a different branch, if the branches where significantly different?
Or would this cause issues, similar to merging issues?

Stashing changes in code: is it necessary?

You may wonder, whether it’s even necessary to use stashing to switch between branches.
Yes, if you’ve made changes to your current branch than you may not be able to switch to another branch without stashing.
When trying to switch between branches you sometimes encounter the following issue:

C:\Users\Name\codingprojects\CoderMatching (createAd)
λ git switch master
error: Your local changes to the following files would be overwritten by checkout:
        codermatching/templates/codermatch/createAd.html
Please commit your changes or stash them before you switch branches.
Aborting

switch to another branch after stashing

If you encounter the error above you can switch to your master branch (or any other branch) as follows:

C:\Users\Name\codingprojects\CoderMatching (createAd)
λ git stash
Saved working directory and index state WIP on createAd: 2a938de Merge branch 'createAd'

C:\Users\Name\codingprojects\CoderMatching (createAd)
λ git switch master
Switched to branch 'master'

After stashing your changes you are able to change your position within your repository.
If you repeat the git switch master command, you’ll not encounter the error anymore and git will not stop you from switching the branch.
Hence you can access the other branch now – congratulations!🥳

Stashing = confusing

From my point of view the stashing is quite confusing, so far.
It seems like the numbers of the old stashes in the list will increase, when you apply new stashes.

Wouldn’t it be easier if every new stash gets the incremented number, instead of changing all the stashing numbers on every git stash?
Or did I just get it wrong?

Especially when you are working with several different stashed versions this may become very confusing quickly.
Because if you want to switch between two different stashing versions, several times, you need to stash your changes again and again (at least with the commands, which I know so far).
And if the numbers of your previous stashes increase every time you save a new stash, then you may become confused, if you don’t delete the old stashes frequently.

However, is this really the best workflow to work on changes in the code like this?