git in pills : git stash
Embed Size (px)
TRANSCRIPT

[email protected] - CTO

Git-Stashsave your work during work :)
[email protected] - 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 …

Git-Stashsave your work during work :)
[email protected] - 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.

Git-Stashsave your work during work :)
[email protected] - 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.

Git-Stashhow to use it ? - stash p. 1
[email protected] - 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.

Git-Stashhow to use it ? - stash p. 2
[email protected] - CTO
Stashing your current files is easy and simple: in your directory where messy files are now type:$ git stashSaved 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_ronothing 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

Git-Stashhow to use it ? - stash p. 3
[email protected] - CTO
Where are your stashed files now ?
$ git stash liststash@{0}: WIP on feature/610_i18N_de_ro: 049d078 added the index filestash@{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.

Git-Stashhow to use it ? - stash p. 4
[email protected] - 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

Git-StashBEST PRACTISES - stash message
[email protected] - 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:

Git-StashBEST PRACTISES - stash lifetime p.1
[email protected] - 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.

Git-StashBEST PRACTISES - stash lifetime p.2
[email protected] - 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 testchangesSwitched 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)

Git-Stashdropping a stash
[email protected] - 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)

Git-StashCommand reference
[email protected] - CTO
• git stash list • git stash • git stash save message • git stash apply —index • git stash drop stash{n} • git stash branch branchname

Git-StashReference
[email protected] - CTO
• http://git-scm.com/book/en/v1/Git-Tools-Stashing • https://www.kernel.org/pub/software/scm/git/docs/git-stash.html • http://gitready.com/beginner/2009/01/10/stashing-your-changes.html