Git Stashing4:03 with Tommy Morgan
In version control, best practice is to commit your work in discrete chunks. Unfortunately, sometimes that's easier said than done when real life gets in the way. In this Treehouse Quick Tip, we'll learn how to overcome this hurdle with Git's stash command.
[treehouse presents] 0:00 [Quick Tips Git Stashing with Tommy Morgan] 0:01 In version control, best practice is to commit your work in discreet chunks. 0:05 Your project history will make the most sense and hence be easiest to work with 0:10 if each of your commits represents a functional and independent change to your project. 0:14 Unfortunately, real life isn't always that clean. 0:18 Sometimes you're half-done crafting a great commit when you get interrupted. 0:22 Perhaps there's an urgent bug that needs your attention 0:27 or you have to switch to a different branch to help a teammate troubleshoot an issue. 0:29 Your half-finished work will only get in the way, but what do you do with it? 0:33 It's not finished so committing it would make your project history needlessly complicated. 0:37 At the same time, you've already spend a lot of time on it 0:41 so it's wasteful to have to delete it. 0:45 Fortunately for us, the Git Version Control System 0:47 provides a third option with the git stash command. 0:50 [Dealing with Interruptions] In this scenario, I've got my project 0:53 and I started doing work on a new task. 0:57 You can see if I run git status that I have several unstaged changes. 0:59 In a typical situation, I would continue working on this change until it's ready to commit 1:04 but when you're working on a project, situations are rarely typical. 1:09 A coworker of mine has just walked over to ask me 1:13 to take a look at some weird bug in the code we're working on, 1:16 but all these unfinished changes are preventing me from being able to see the same behavior that he's seeing. 1:19 I need to get these changes out of the way, but how? 1:23 I could commit them but they're not finished and that would mess up my clean project history. 1:27 I could just delete the changes, but then I'd lose all the work I was doing. 1:31 Thankfully, I have a third option. 1:35 I can stash the changes with git stash. 1:38 If we look at git status again, you can see 1:42 that all of my unstaged changes from before are gone, 1:44 so now I can help my coworker with the problem he was having. 1:47 But where did my changes go? 1:50 They've been stashed away by Git 1:53 and now that I am done helping my teammate, 1:56 I can use git/stash/apply to bring them back. 1:57 [OOPS! Wrong Branch] Now, I've started work on another new feature. 2:01 If we run git status, we can see all of my unstaged changes so far 2:04 but wait! I'm still on master! 2:08 I really want to be doing this on a new branch specific for this feature 2:11 at least until I'm finished with it. 2:14 Thanks to git stash, this isn't a problem. 2:17 I'll use git stash to stash my changes, 2:20 git checkout to create a new branch, 2:23 and then git stash apply to bring my changes back again. 2:29 With 3 quick commands, I was able to create a new branch 2:33 and move all of my in-progress work over without missing a beat. 2:35 [What if...] So I have been working on my new feature but it's 2:39 a really complicated concept, and I'm not sure the idea that I had 2:42 for how to build this is a great one. 2:45 I've got another idea I want to try, even though I'm in the middle of trying my first thought. 2:47 It's okay. I'll just use git stash to save the work I have in progress and start off on my new idea. 2:53 Now I'm down this second road and it doesn't really look a lot better to me. 3:00 I think I'm going to switch back to my first attempt, 3:04 try to finish that a little bit more, and see if it grows on me. 3:06 So I'll just stash my changes—oh wait! 3:09 I just stashed this second set of changes over the top of my first. 3:14 How can I get at that older stash? 3:18 If I run git stash list, I can see a list of all the stashes for this repository, just the 2. 3:20 You can see that they have special labels. 3:26 The syntax is a little weird but the important part is the number 3:29 that gives us an indication of the order in which the stashes were applied. 3:33 We can provide that special label to the git stash apply command 3:36 just like this— 3:40 in order to retrieve changes from a specific stash. 3:43 Stashes and git are great temporary storage for your changes 3:46 and you'll only find more uses for them as you spend more time with Git. 3:51 Whether you were interrupted by an urgent request for help from a teammate 3:54 or even just realized that you were working in the wrong branch, 3:58 Git stash is a huge time saver. 4:01
You need to sign up for Treehouse in order to download course files.Sign up