Join Kevin Skoglund for an in-depth discussion in this video Saving changes in the stash, part of Git Essential Training.
In this chapter we're going to learn about a feature of Git called the stash. The stash is a place where we can store changes temporarily without having to commit them to the repository. It's a lot like putting something into a drawer to save it for later. The stash is not part of the repository, the staging index or the working directory, it's a special fourth area in Git, separate from the others. And the things that we put into it aren't commits, but they're a lot like commits, they work in a very similar way. They're still a snapshot of the changes that we were in the process of making, just like a commit is.
But they dont have a SHA associated with them. Let's try making a change, and then we can store it in the stash. Let's start by checking out one of our other branches. Let's check out this shorten_title branch, so git checkout shorten_title. Now the shorten_title branch does not include the changes that we've made to our text_edits branch. So git branch --merged, you'll see that it includes all the changes that are in seo_title, but not the changes that are in either master or in text_edits.
So if you remember back we've made some changes before to the mission file. Let's open up that file again, and let's make another edit to this file. I'm just going to say Explore California: Our Mission, change the title of it. So I'll save that, close it up, git status, we see the change. While we've got that change there, let's now try to switch branches back to our master branch, checkout master, it says nope, you can't do that, because if you did it would overwrite the changes that you just made because master has something different for mission.html.
So please commit your changes or stash them before you can switch branches. This is a classic case of when we want to use stash. It's not the only time you can use it, you can use it any time you want to just take some stuff and shove it in a drawer. But this is the time that you'll probably use it most often when you need to switch branches, you have some changes, you're not quite ready to turn them into a commit yet, so we've got stash them instead. The way that we do it is we simply say, git stash save, so we're telling to git stash that we want it to save our changes, and then we're going to provide a message.
Now there's no -m option this time, we just provide the message inside quotes. So I'm going to say that the changes "changed mission page title." Now this can be anything. This is for your benefit. This is not a commit that anyone else is going to see, so it can be a little bit sloppier perhaps. It doesn't have to have a good message the way that you should do with the commit, but it should be something descriptive that's going to be meaningful to you when you come back and look at it later and try and remember what was it that you put into the stash. So we'll hit Return, save the working directory and index state on shorten_title, changed mission page title, HEAD is now at here, swap out - for : in index.html title.
So that's the name of the commit that I'm on now, git status, and you'll see that this is clean, git log --oneline, and I'll just do a couple of lines here, and you'll see this is the commit that it's on now. So it stashes the changes and says, now you're back here at that commit. So what it actually did, after I put those changes into the stash, was it ran git reset hard HEAD, so if you remember what we talked about with git reset hard, that takes whatever is in the Repo, and puts thats into both our index and our working directory, so they're exactly the same as the commit where HEAD is pointing right now.
One note, if you did have untracked files, you can include those too, you use these include, untracked option. You can look that up in the help file if you need to. But normally this would just include things that are in your working directory, which are tracked files, because those are the things that would conflict, it would keep us from being able to switch. So that's it. That's all there is to be able to put things into the stash. Now we are going to see how we can take a look at things that are in the stash, and that's what we'll do in the next movie.
- 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