Coding

Git – The STASH Command

  • 00:00:01 hello and welcome to this video working
  • 00:00:04 on a project is not always a
  • 00:00:06 straightforward thing you might work on
  • 00:00:09 a specific feature or try out something
  • 00:00:11 and then you might want to go back to
  • 00:00:14 the last statues of the project the last
  • 00:00:16 clean and working statues so basically
  • 00:00:19 you want to come back to the last commit
  • 00:00:21 in your branch but you kind of want to
  • 00:00:23 say if your draft that you're just
  • 00:00:25 working on with the gates – this is
  • 00:00:28 easily possible without having to create
  • 00:00:30 a new commit for this draft statues so
  • 00:00:33 you can avoid such dirty commits you
  • 00:00:36 could say so let's find out how this
  • 00:00:38 works now
  • 00:00:41 so I prepared a little project I
  • 00:00:45 wouldn't say it's a project it's just an
  • 00:00:46 HTML file with a little bit of content
  • 00:00:49 the important thing is that if I have a
  • 00:00:51 look at my project now you can see that
  • 00:00:53 we have one commit right here in our
  • 00:00:56 branch now let's say you are working on
  • 00:00:59 a new feature right here and we could
  • 00:01:03 call it feature one this could be
  • 00:01:06 anything like that so any kind of new
  • 00:01:08 thing you are just creating and which is
  • 00:01:10 not finished yet now you want to go back
  • 00:01:12 to your initial situations or a
  • 00:01:14 situation where you only had your first
  • 00:01:16 commit in here as I said in the
  • 00:01:18 beginning adding a new commit or
  • 00:01:20 creating a new commit is possible but
  • 00:01:23 kind of a dirty approach therefore you
  • 00:01:25 can use the stash come on now this works
  • 00:01:29 quite easily actually you just have to
  • 00:01:31 enter get stash down here in your
  • 00:01:34 command line in your terminal and then
  • 00:01:37 hit enter with that you can see that the
  • 00:01:40 newly added content we had in the third
  • 00:01:42 line is gone and you're basically back
  • 00:01:45 in our commit now and if I go to get
  • 00:01:48 lock down here you can see nothing
  • 00:01:51 changed only our first commit with the
  • 00:01:53 starting code is displayed here but if I
  • 00:01:56 enter get stash apply like this boom
  • 00:02:01 then you can see that our feature one
  • 00:02:05 that we just added right here is visible
  • 00:02:07 again because now our stashed statues
  • 00:02:11 our stash project status is loaded you
  • 00:02:14 could say back again into our name
  • 00:02:16 project as you can see nothing is
  • 00:02:19 committed so far so we now have the
  • 00:02:21 option to commit this change and edit
  • 00:02:23 our project or to keep on working on
  • 00:02:26 this stash so on this draft version so
  • 00:02:29 we could also say we add a second
  • 00:02:31 feature feature to and we're working on
  • 00:02:34 both of these features at the moment if
  • 00:02:36 I save this and now enter get stash once
  • 00:02:40 again then you can see that our two
  • 00:02:43 features so the two stashed features
  • 00:02:46 that we have are not displayed anymore
  • 00:02:48 we are back again in our last commit
  • 00:02:51 that we actually have in the project but
  • 00:02:53 if you enter get stair
  • 00:02:55 list down here and then you can see what
  • 00:02:58 these two stashes are now we have two
  • 00:03:01 stashes with the index of zero and one
  • 00:03:03 right here and if we now enter get stash
  • 00:03:07 apply as we did before we can see that
  • 00:03:10 the last version of the last stat use
  • 00:03:13 that we used with these – command is
  • 00:03:15 displayed right here
  • 00:03:16 if and I want to go back to get stash
  • 00:03:19 list to see the list again you can see
  • 00:03:22 that we again have these two stashed
  • 00:03:25 versions and if gonna enter get stash
  • 00:03:29 apply as we did it before but now enter
  • 00:03:32 a number then we can basically select
  • 00:03:35 based on these index numbers right here
  • 00:03:38 which stash we want to display right
  • 00:03:40 here so the default behavior get stash
  • 00:03:43 apply is the one we just saw this
  • 00:03:45 basically applies the most recent so the
  • 00:03:48 top stash right here this one but if I
  • 00:03:52 enter get stash apply one for example
  • 00:03:55 then you can see that this is not
  • 00:03:57 working because we basically applied a
  • 00:04:00 later stash so the top stash the most
  • 00:04:03 recent stash so what you cannot do is we
  • 00:04:05 can simply say get stash once again and
  • 00:04:08 then go back to get stash list like this
  • 00:04:11 and as you can see this is the stash we
  • 00:04:14 just created and stash – as you can see
  • 00:04:18 the index now increased of course is
  • 00:04:19 this – we actually want to check out so
  • 00:04:22 if a not enter get stash apply – then
  • 00:04:25 you can see we are back in the first
  • 00:04:28 stash that we created with feature 1 and
  • 00:04:31 what we could now of course do is we
  • 00:04:34 could keep on working in this stash and
  • 00:04:37 say another awesome feature right here
  • 00:04:43 like that oops
  • 00:04:45 like this and now we could again enter
  • 00:04:48 get stash which would basically add this
  • 00:04:51 – on top of the others or we enter get
  • 00:04:54 stash push – em and now add a
  • 00:04:58 description for this – this makes it
  • 00:05:01 easy for us to identify the stash and
  • 00:05:04 especially what we changed and there so
  • 00:05:06 let's say edit
  • 00:05:08 awesome feature like that and it enter
  • 00:05:14 if we now go to get stash list once
  • 00:05:18 again you can see that our last stash
  • 00:05:21 right here has this added awesome
  • 00:05:23 feature description so this helps us a
  • 00:05:25 lot to well identify the stashes and I
  • 00:05:27 said and here we have the other ones we
  • 00:05:29 had the other stashes which only can be
  • 00:05:32 identified based on this index number
  • 00:05:34 which can be quite hard sometimes in
  • 00:05:37 case you don't remember which stash
  • 00:05:39 versions you had what we could do now
  • 00:05:42 again is we could say get stash apply to
  • 00:05:45 apply our most recent one and we could
  • 00:05:47 add what maybe right here
  • 00:05:50 the final feature like this if we now
  • 00:05:54 enter get stash push – M final feature
  • 00:06:00 as you can imagine if we go now to our
  • 00:06:05 get stash list right here you can see
  • 00:06:08 all these stashes now what we can also
  • 00:06:12 do is we can get rid of these stashes
  • 00:06:15 down here because you might say okay we
  • 00:06:17 had these previous versions but actually
  • 00:06:19 I only need the last one or the stash
  • 00:06:21 one depending on what you need then you
  • 00:06:24 can also easily drop these different
  • 00:06:26 stashes and by saying drop this is also
  • 00:06:29 the command we need right here we can
  • 00:06:31 simply say get stash drop and now we
  • 00:06:34 only have to identify these – we want to
  • 00:06:36 drop based on the index number once
  • 00:06:38 again so in our case this could be this
  • 00:06:41 one right here so the stash with the
  • 00:06:43 index – if we enter we see that the
  • 00:06:47 stash with the index – was dropped and
  • 00:06:49 if I go back to get stash list now you
  • 00:06:53 can see that now the previous stash
  • 00:06:57 number four so this one now became stash
  • 00:07:01 free – free became stash – and so on so
  • 00:07:06 if when I entered this command once
  • 00:07:08 again and one final time like that then
  • 00:07:12 you can see that we only have our two
  • 00:07:15 stash versions right here with the
  • 00:07:18 description saved in our stash basically
  • 00:07:20 now the only thing we have to do
  • 00:07:22 is which stash so which kind of draft
  • 00:07:26 version do we want to implement in our
  • 00:07:28 main project let's say the feature is
  • 00:07:30 finished now and you want to edit and
  • 00:07:31 you can also select this by simply
  • 00:07:35 typing git stash top
  • 00:07:38 that's the come on unit right here and
  • 00:07:40 now again the index number what is going
  • 00:07:42 to happen then is that if you enter pop
  • 00:07:44 0 the final feature right here will be
  • 00:07:48 added to our code right here and it will
  • 00:07:51 be deleted from our stash the same thing
  • 00:07:54 can be done with stash 1 right here so
  • 00:07:56 let's maybe add one here and press Enter
  • 00:07:59 and as you can see another awesome
  • 00:08:01 feature is now displayed right here and
  • 00:08:03 if then I'll say get add like that and
  • 00:08:09 now commit – M let's say awesome feature
  • 00:08:16 add it like this then if we go to get
  • 00:08:20 log once again we can see that we have
  • 00:08:23 this awesome feature added commit right
  • 00:08:25 here and if we go down a bit
  • 00:08:27 our starting code but all the stash
  • 00:08:29 versions we saved are not displayed
  • 00:08:32 right here but if I enter get stash list
  • 00:08:35 now you can see that only this final
  • 00:08:39 feature stash is displayed because the
  • 00:08:42 other stash the one we use right here
  • 00:08:44 was added to the code and then kind of
  • 00:08:46 deleted from the stash and now as we are
  • 00:08:49 finished basically you could say I don't
  • 00:08:52 need D stash anymore so I can simply
  • 00:08:54 enter get stash clear right here and
  • 00:08:57 with that if you know enter get stash
  • 00:08:59 list one less time you can see we don't
  • 00:09:02 have any kind of stash versions of our
  • 00:09:05 code saved right here and that's
  • 00:09:08 actually ticket stash command explained
  • 00:09:11 in hopefully a few words as you can see
  • 00:09:14 this is quite helpful if you want to
  • 00:09:16 implement new features or play around in
  • 00:09:19 the code and if you don't want to
  • 00:09:20 destroy the actual working main code
  • 00:09:23 that you have in your branch and in the
  • 00:09:25 last commit for example so I hope you
  • 00:09:28 liked this video and that you might even
  • 00:09:30 use this in your daily work now so as
  • 00:09:32 always I can only say thanks a lot for
  • 00:09:34 watching and hope
  • 00:09:36 you in the next or one of the other
  • 00:09:37 videos here on the channel and on
  • 00:09:39 academy.com so bye