Join Kevin Skoglund for an in-depth discussion in this video Retrieving stashed changes, part of Git Essential Training.
We've been taking a look at how we can use git stash to save our changes temporarily. We saw we could put changes in there, and we saw how we could list them, and we saw how we could use show in order to inspect them. Now what we want to do is be able to retrieve those stash changes out of there, essentially do the opposite of git stash save. If we put them in the drawer before, now we want to get them back out of the drawer, and when we do that it's going to bring those changes back into our working directory, whatever that working directory is, whichever branch we're on. And now just as a refresher, remember when I did the stash before I was on the shorten_title branch, now I'm on the master branch.
Git doesn't care. For all it knows this is absolutely what you intended, which is stash the changes, switch branches and then bring them out again. So it doesn't matter which branch you're on, it's going to try to bring them into your working directory and apply those changes. Like with merges, there's the possibility that there may be conflicts that those changes don't apply cleanly, and in that case it works like merge does. It does its best to try and figure out how to merge the changes, but if not, then there will be conflicts, and it'll be up to you to resolve those conflicts.
Now there are two commands that we can use to pull items out of the stash. The first is going to be git stash pop and the other one is git stash apply, both of them will pull what's in the stash out, and put it in the working directory. The difference is that stash pop also removes it from the stash as well. Git stash apply leaves a copy there, so pop is going to pop it out so that it's no longer in there at all.
We'd just popped it out of the drawer and back into our working directory. It is exactly the opposite of git stash save. Git stash apply pulls it from the stash into the working directory but leaves a copy in the stash. The idea here is that it might be some change that we want to apply to where we are now, then we might want to switch to a different branch and apply it there, switch to another branch and apply it there as well. So we want to keep it in the stash as we move between each of those branches. Or it might be something that we want to apply then make a few more commits and then apply it again, and so on.
Most of the time, pop is what you're going to want to use, and that's what we're going to use here. After pop, I need to specify which stash item I want it to pull out. If we don't say then by default, it's going to pull the first one, which is this one. I'll just go ahead and write that in there. So if we don't say, it will pull the same thing. It'd be exactly the same as if we did stash @ sign, curly braces around a zero. If we had three of them, and we wanted to pop up the third one, well then we'd do git stash and then 2, 2 because the third one is actually number 2 because they start numbering at 0; 0, 1 and 2.
But in this case, I want to pop out that first one. So I'll pop it out, auto-merging mission.html, changes not staged for commit, that means it's in my working directory, and it dropped the stash right here. So now if we say git stash list, you'll see that it's not in there anymore. And if we go and look at that file, mission, you'll see that it has the Our Mission in it, which is the change that we made. Now we can re-stash this change if we want.
If we say, oops, wait! I didn't want to be on the mission branch, we can say git stash save, and then we can store "change to mission page title." Now it's saved again, git checkout, and we'll check out our shorten_title branch, okay. Git status, nothing in there, git stash, and this time just for the experience of it, let's do apply, and we won't specify which one, that will pull the top one out of there.
Just take a list just so you see it, there it is. And let's this time do apply, now it still does the auto-merge but this time when we look at our stash, you can see that it's still there. It did not take it out of there. For now, you can just leave the changes we made to mission.html in your working directory. We're not going to commit it or worry about right now. What we want to focus on next is how we can delete items that are in the stash. In particular, I want to see how we can delete this item that we left in there.
- Exploring the history of version control
- Installing Git on Mac, Windows, and Linux
- Initializing a repository
- Writing useful commit messages
- Understanding the Git three-tree architecture
- Tracking when files are added, edited, deleted, or moved
- Viewing change sets and comparing versions
- Undoing changes and rolling back to previous versions
- Ignoring changes to select files
- Creating and working with code branches
- Merging branches and resolving merge conflicts
- Stashing changes for later
- Working with hosted repositories and remote branches
- Developing an effective collaboration workflow
Skill Level Beginner
Q: In the Chapter 10 movie "Configuring the command prompt to show the branch," when I type the function "__git_ps1," I do not get the expected result.
A: The function "__git_ps1" was recently moved to a new file, .git-prompt.sh, as described here: https://github.com/git/git/commit/af31a456b4cd38f2630ed8e556e23954f806a3cc.
We will update the video. In the meantime, you may do the same steps you do for .git-completion.bash, but a second time using ".git-prompt.sh" as shown here: https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh.
Q: When I use the code the instructor advises in the above video ("git config --global user.name "Nelda Street"), I still get an "Illegal Instruction" error. I have OS 10.6.8. Am I doing something wrong?
A: The current installer version of git isn't compatible with older Mac OS versions.
1. To add "-mmacosx-version-min=10.6" as described here:https://stackoverflow.com/questions/14268887/what-is-the-illegal-instruction-4-error-and-why-does-mmacosx-version-min-10https://stackoverflow.com/questions/10177038/illegal-instruction-4-shows-up-in-os-x-lion
2. Or to use the version of git that comes with Xcode, or to use homebrew to install git instead.http://superuser.com/questions/697144/installed-git-not-sure-how-to-get-it-working