Dark Light
How to stash (“stasher”) one or more files, or some pieces of one or more files (bonus at the end of the article).

The problem

We have the habit to use stash command with his simplest way by doing git stash to temporarily set aside its modifications, and then a bit later, git stash pop, to apply its. But how to do in the case when we want to stash only one file? GIT documentation tell us git stash is equivalent to git stash push.

The solution

We will do simply pass some arguments to this option:

git stash push -p -m "My beautiful message"

-p (or --patch long version) aloow to "Interactively select sections" to stash. -m allow to specify a message, which is more user-friendly.

Once the command is executed, you will have to select how to process for each hunk. If you want to display the help, press ? and then enter. It will promp the help :

y - stash this hunk
n - do not stash this hunk
q - quit; do not stash this hunk or any of the remaining ones
a - stash this hunk and all later hunks in the file
d - do not stash this hunk or any of the later hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
e - manually edit the current hunk
? - print help

a and d are interesting, if you want to perform faster processing, file by file, instead of processing piece by piece. For example, if you only have one file that you want to store, use d until you find the right file, then use a to stash all the file and finally q to exit and save.

To go further

I am sharing with you some options that are useful to me.

-k or --keep-index which allows you not to store what you have in the staging area (i.e. ready to be committed). You can also choose to store them with the `–no-keep-index’ option.

The -u' or –include-untracked’ option allows you to store untracked files, which is not the default `stash’ option.

Use the "show" button to display the contents of a stash stack. Without an option, displays the differences, in summary form, of the last storage. To view the differences in detail, use the -p' option. To see the next-to-last one, git stash show stash@{1}`.

Keep in mind that the shed is a FILO type stack, which means "First In, Last Out". It’s like a stack of plates, you will always take the last stowed.

For more information, you can read the official documentation here : https://git-scm.com/docs/git-stash/en

Conclusion

This is not the most user-friendly way of doing things, but it allows you to better understand how git works, this powerful and little-known tool. I advise you to learn how to use it on the command line before using graphical tools like PHPStorm or Sublime Merge. This will allow you to better understand what you are doing and not be afraid to do anything any more 😉