3. Git-Stash
save your work during work :)
federico.panini@fazland.com - CTO
The problem
When your working on a ticket to solve a bug or to implement a new feature,
frequently happens that you need to switch to another branch for solving a
problem that has occurred. So what to do ? Your actual work is in a messy state
you have new files, uncommitted ones …
4. Git-Stash
save your work during work :)
federico.panini@fazland.com - CTO
You could commit your amends and switch to the other branch for solve a bug:
DON’T DO IT!
You’ll loose all of the informations about your progress and also you’re storing a
commit with inconsistent code.
5. Git-Stash
save your work during work :)
federico.panini@fazland.com - CTO
How can I leave the state of my current branch clean, and switching to another
branch ?
use GIT STASH
Using git stash will allow you to maintain the dirty state of your current working
directory and saves it on a stack of unfinished changes that you can reapply
at any time.
6. Git-Stash
how to use it ? - stash p. 1
federico.panini@fazland.com - CTO
Stashing your current files is easy and simple: in your directory where messy
files are type:
$ git status
# On branch feature/610_i18N_de_ro
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: routing_de.yml
# modified: src/Fazland/WebsiteBundle/Controller/QuoteController.php
# modified: src/Fazland/WebsiteBundle/Controller/SettingsController.php
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
#
# modified: scripts/crontab
#
You’ll see a list of unstaged files.
7. Git-Stash
how to use it ? - stash p. 2
federico.panini@fazland.com - CTO
Stashing your current files is easy and simple: in your directory where messy
files are now type:
$ git stash
Saved working directory and index state
"WIP on feature/610_i18N_de_ro: 049d078 added the index file"
HEAD is now at 049d078 added the index file
(To restore them type "git stash apply")
Now your working directory is clear
$ git status
# On branch feature/610_i18N_de_ro
nothing to commit, working directory clean
at this point you can freely switch to another branch do whatever you need to
do to fix bugs or implement new feature
8. Git-Stash
how to use it ? - stash p. 3
federico.panini@fazland.com - CTO
Where are your stashed files now ?
$ git stash list
stash@{0}: WIP on feature/610_i18N_de_ro: 049d078 added the index file
stash@{1}: WIP on feature/610_i18N_de_ro: c264051 Revert "added file_size"
stash@{2}: WIP on feature/610_i18N_de_ro: 21d80a5 added number to log
You can see a list of stashes and the first one (stash{0}) is the last that you
created. At any time you can get the stash back.
9. Git-Stash
how to use it ? - stash p. 4
federico.panini@fazland.com - CTO
How to stash back your files ?
$ git stash apply --index
# On branch feature/610_i18N_de_ro
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: routing_de.yml
# modified: src/Fazland/WebsiteBundle/Controller/QuoteController.php
# modified: src/Fazland/WebsiteBundle/Controller/SettingsController.php
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
#
# modified: scripts/crontab
#
When your critical bug fixing is done you can get the files back from the stash
and continue you development on that files
10. Git-Stash
BEST PRACTISES - stash message
federico.panini@fazland.com - CTO
If you don’t give a message comment to a stash git automatically creates it
taken it from the last commit log message. This could be a bit confusing when
your working with multiple stash and could lead to errors … huge errors!
So the adivice is to use git stash save “message”.
This command is equal to git stash with the difference that you can give a
message name to the stash
$ git stash save “stash fiels for #610 working progress on upload image bug fixing ”
# On branch feature/610_i18N_de_ro
# Changes to be committed:
11. Git-Stash
BEST PRACTISES - stash lifetime p.1
federico.panini@fazland.com - CTO
Git Stash is used for switching quickly between branches when you have to
work on multiple different tasks, or when suddenly you have to fix a critical bug.
Considering that the stash is “shared” between all the git repository it is not
absolutely advised to leave the code in the stash for a long period. The stash is
a quick “knife tool” which will help you with sudden “code” events.
12. Git-Stash
BEST PRACTISES - stash lifetime p.2
federico.panini@fazland.com - CTO
If you plan to work on a new feature for
a long time period, instead of putting
the working files your on in the stash
you can create a branch for them:
BRANCH IT!
$ git stash branch testchanges
Switched to a new branch "testchanges"
# On branch testchanges
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: routing_de.yml
# modified: src/Fazland/WebsiteBundle/Controller/QuoteController.php
# modified: src/Fazland/WebsiteBundle/Controller/SettingsController.php
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
#
# modified: scripts/crontab
#
Dropped refs/stash@{0} (f0dfc4d5dc332d1cee34a634182e168c4efc3359)
13. Git-Stash
dropping a stash
federico.panini@fazland.com - CTO
When your stash has been got back you can delete it. Remember that it’s
always a best practice to delete your stash just after you got back from stash
stack.
$ git stash drop stash@{0}
Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)