Temporarily store some work in progress because I have to jump to another branch

Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 years ago

We all know this. You’re working on some new feature and you have lots of “work in progress”. Then an urgent issue comes in, you have to jump onto the master and apply a hotfix. In this lesson we will learn how to temporarily store your current changes s.t. you can switch the current branch and come back later to resume your work again.

Instructor: [00:00] I am currently in my app folder branch here where I am creating a new app for the component for our application.

[00:06] Assume I have to change back to master because there is some bug that came in and we have to do a hot fix on there. In order to not lose my ongoing changes here, what I could do is either to commit a working progress commit on my future branch, which I'll then clean up later, or I can use the git stash command.

[00:24] Here I am having one file that has been modified and one that hasn't even being tracked by git. In order to capture all of them, let's just do a git add. I'll apply git stash.

[00:36] If I take a look now at my git repository, you can see that nothing has to be committed and everything is clean basically. I can now go back to master, apply my changes. Let's simply open the Readme here for the purpose of making some change. Let's drop here a section three. Save that.

[00:59] Let's edit, let's commit it. Now you can see that everything is still clear. I am a master. I create a new commit. Now I can jump back to my app folder branch. I can do a git stash pop to take it out again.

[01:17] Let's first take a look at what we have in that stash. You can imagine that stash as a stag structure where all the messages will pop in one after the other. We'll have here stash@{0stash@{1}, and so on.

[01:30] I can now do something like git stash pop which basically takes it out of my stash stag. If I do stash list, we can see it's empty. I have all of my files again here.

[01:43] Let's again stash them. If I do git stash list, we see they are still in there. Now I could also instead of doing a pop, because I want to leave them in there for some reason, I could do a git stash apply.

[01:58] What git stash apply will do is it will take out the changes again into my working directory just as the git stash pop did. If I do git stash list, I can see it's still in there.

[02:10] Additionally, what you could do is also when stashing new changes to give them a name, in order to remember what kind of changes you actually put in there. What we can do is instead of just git stash, we can do git stash save. I am giving it a name like WorkInProgressAppFolder.

[02:28] Let's do a git stash list. Now you can see at the point 0we have basically that WorkInProgressAppFolder. At stash@{1} we have all the generated messages which the git stash command adds for us automatically.

Miguel Romero Gutierrez
Miguel Romero Gutierrez
~ 5 years ago

Good course. I'm think you could add some useful examples to use git reflog, this command has been save me the life more than once. And maybe add some about git flow.

Juri Strumpflohner
Juri Strumpflohnerinstructor
~ 5 years ago

Hi Miguel. Thanks for the feedback. Yep indeed, I'm already drafting some more lessons I'll be adding to the course very soon. Git reflog is one of them, among bisect and a couple of others. So stay tuned 😉.

JP Lew
JP Lew
~ 5 years ago

Useful course, well done. As a heads up, git stash save is deprecated. From the manual (v2.20.1): This option is deprecated in favour of git stash push. It differs from "stash push" in that it cannot take pathspecs, and any non-option arguments form the message.

Juri Strumpflohner
Juri Strumpflohnerinstructor
~ 5 years ago

@JP oh thx for the heads up 👍

Dr.Emmett Brown
Dr.Emmett Brown
~ 5 years ago

Thanks for your effort. Apart from this git examples, I would like to see some of "git flow" examples by you. I'm sure that it'll be useful. Thanks.

Juri Strumpflohner
Juri Strumpflohnerinstructor
~ 5 years ago

I would like to see some of "git flow" examples by you. I'm sure that it'll be useful. Thanks.

Thanks, I'm definitely thinking about something like that. Not sure whether it would be about the "flow model" but rather more in the direction of the trunk based development approach with git.

codecentric doo
codecentric doo
~ 4 years ago

Hi, great course, I learned new stuff watching it :)

One minor thing that I've noticed and that I think you can improve is: In every lesson you are doing git add . to stage all files that have been modified and then you are doing git commit -am but here in this command -a is unnecessary because you've already added changed files to stage area with previous command, so git commit -m is sufficient here :).

Markdown supported.
Become a member to join the discussionEnroll Today