Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
We now know how to switch between branches in Git, and we also know that the process of doing that is to use the checkout command, and what the checkout command does is it tells Git to go and get the latest version of the branch and to make our working directory look exactly like it. There is one thing that you need to be cautious about with that, which is that your working directory must be clean in order to switch. Actually, mostly clean. We'll talk about that later, but if it's not clean, then Git won't let you do the switch. Let's take a look. Right now, I'm on my shorten title branch, I do git status, you will see that right now my working directory is clean.
Let's make a change. So I'm going to make a change to index.html, open it up, and in the same line that I changed before, I'm going to change the hyphen to be a colon. I am going to save my changes and close it. So now we have an unsaved change, not stage for commit or anything, just sitting in our working directory. Let's try switching to our master branch, git checkout master, just clear my screen so it's at the top. Now it pops up, and it gives us an error, it says, your local changes to the following files would be overwritten by checkout: index.html. Please, commit your changes or stash them before you can switch branches. Aborting.
Git is telling you, sorry, I can't do this, because if I do, if I make your working directory look exactly like master, you're going to lose the changes that you just made. So rather than destructively just blow out your changes, which you might want to keep, you might not have realized that this was going to a problem, Git stops and does the safe thing, and it says you need to deal with this problem first. You have three options at this point. One, you can scrap the changes by checking out the file again. That's git status git checkout -- and the file name.
The second thing you can do is you can commit the changes to the current branch. Once they're committed, they won't get lost anymore. They're stored in Git, we can just switch back and forth between the branches, and it will just switch back and forth between the HEAD of each one of those branches. The third possibility is that we can stash the changes, and we'll talk about the stash a little later on. Basically it's a little pocket that we can put things in and save them until later. We can put them away to a little area that we can pull them back when we're ready. So for now, let's go ahead and just commit this, git commit, and we'll use the -a option to both add it and commit it at the same time, and then give it a message, we'll say, Swap Out - for : in index.html title.
So now, we've made our change, we can now checkout master, no problem. We can checkout our shorten title, and it switches back and forth between those just fine. Now I said it has to be mostly clean, because it doesn't have to be completely clean. It just has to be clean enough that there are no conflicts. So for example, let's say that I put a new file in here, tempfile3.txt, just any file will do. Now if I say git status, we see that we are on our shorten title branch, and you can see that here's the file, tempfile3.txt that's untracked.
If we say git checkout master, it said no problem, I have no problem switching for you. git status, now we're on branch master, still have this untracked file. So that's what I mean when I say mostly clean, we can't have anything that would cause us to lose data if we made the switch.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104548 Viewers
56 Video lessons · 116470 Viewers
71 Video lessons · 85677 Viewers
131 Video lessons · 41006 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.