FREELessons: 24Length: 3.1 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

6.6 Showing Repository Info in your Prompt

One way to speed up your workflow if by putting repository information in your prompt. You’ll not only know when you’re inside a repository, but also what the state of that repository is.

6.6 Showing Repository Info in your Prompt

You might have noticed that every time we want to see the state of our Git repository, we've been running the Git status command. And I can't help thinking that it would be nice if we didn't have to do this. If there was just some way for us to always know exactly what the state of our repository was. Knowing whether we had modified files that we needed to commit. Knowing whether we had completely untracked files. And even knowing what branch we were on. Is there a simple way to do this? Actually there is. There's a really nice way that we can get this information to show up in our prompt. And that's what we're gonna look at doing in this screencast. Now if you're not familiar with customizing your prompt, let me tell you that your prompt is this piece right here. When you open a terminal, it's the part that comes before where you'll type the command. So when we type our command here to move into our directory, you can see that this is our prompt. Right before what we typed, but then, when I hit Enter, notice that the prompt actually changes. After the colon, here, it has the name of the current directory that we're in. In this case, that's project. We also have other information such as the name of the computer we're on and the name of the user. Now, this isn't meant to be an entire tutorial on what the prompt is and how to customize it. I've written two tutorials on NetTuts, and some other people have written some other command line tutorials that will really help you learn to customize the terminal and make it as powerful as it can be for what you have to do. But I'm gonna assume you're kinda familiar with that. So what we're gonna do is add our Git information to the prompt. And we're actually going to need a little third party piece of code to do this. And that is called vcprompt. This little tool will pull out some version control information from a number of version control software. And in our case we're interested in the Git version control. So you can get this at github.com/djl/vcprompt. If you just Google vcprompt, you'll probably find this URL, among the several others that are pretty much the same repository. However, I'm gonna grab that, come back here, and let me actually move on to our desktop. And we're going to Git clone this repository. So I'm gonna clone that repository down. And now, if we come back to the read me file for this repository down here, you can see that they recommend copying the bin/vcprompt file, which is the binary that we're gonna run, into a directory of our own. And you don't, it doesn't have to be this specific folder, although this is the practice I use so that's what we're gonna do. We have to mark it as executable and then we're gonna add it to our prompt. So I should mention that this does require Python to run. And i haven't done anything with the Python version that is on this computer. I am just using the stock version that came with this computer. As you can see that's version 2.7. Here this recommends 2.4 or later. If you're running with anything later than 2.4 you should be just fine. So we'll just copy vcprompt/bin/vcprompt, and we're gonna copy that into the bin file that I have already created in my home directory. This is where I keep any kind of binary like this that I want to be able to use from the command line. This means that this folder is gonna have to be in our path but we'll add that in a second. There we go and if we run home directory/bin/vcprompt, you can see that we don't get anything up because we're not actually in a Git repository, but if I move into that vc prompt folder and run it, you can see we have gitmaster put out which tells us that we are using the Git version control system and run the master branch. It's telling us that we are using Git because this script also works for other version control systems. So now that we have our script installed, let's actually go ahead and customize our prompt. And we're gonna do this within our .bash_profile file, which you will find in your home directory, and first I want you to note that we have added home directory /bin to our path. This means that we'll be able to access the VCPrompt program from the terminal. So creating our customized prompt is just as simple as setting the variable PS1 equals and then we are going to have double quotes and in here we are going to put what we want our prompt to be. So just to show you how this works if I just put in hello git and then a bracket there and I save that open a new tab you can see that that is what our prompt is hello git with arrow and then we will write our command after that. Now that's pretty boring, so let's see if we can customize this a bit more. Well, let's first bring back our username. We do that with \u, and we'll say @\h for host, and then I'm gonna have a colon, and I'm gonna say \w to get our path. So if I just save that, you'll be able to see how that works. You can see that we have username at computer name colon path, and if I actually move into a different directory, you'll be able to see how path changes there all right that's good so we have to start there. I am actually going to put a new line at the beginning because that is how I like to do it and I am going to put a new line just before the bracket so now we actually have a free line prompt which actually gives us some space and this is similar to what we use in my daily work on the terminal. But now we want to add the Git info, and we're gonna add it right here after the W. So you're gonna have to do \$, then in parentheses, this is where the command is going to go. So we're going to run the VC prompt command. And let's start with just VC prompt in there. If you open a new tab you can see that nothing is showing up, but if I move into a Git directory, we now have Git master showing up. So even with just that, we now know when we're in a Git repository and when we're not. And we also know what branch we're using, okay. So now, let's customize this a bit more. We don't need to know that we're using Git, because that's the only version control system we're using right now. And we also want to know if we have untracked files, or if have changes. So let's customize this a bit. We're gonna pass it the -f flag, which mean we're gonna pass at a format string. And if you come back here to the README for this file. You can see the -f means we're going to pass it a format string. And down here, these are the formatting tokens that we can use. So we're gonna be interested in the last three I think. %b is the current branch name. So if we just start with that. Add %b. Now when we open up a new terminal and move into our project, we're just seeing master and that's fine. But we want to add some more, so let's say %m will display a plus sign if the repository has any modified files. And %u will display an exclamation mark if there are any untracked files in the repository. And I happen to know that this is a bit out of date. It's actually gonna display a question mark. So let's go ahead and add those in. We're gonna have %u and, actually %m first, and then %u. And now if we come back here, you can see that we actually have a plus sign, and that means there's a modified file. And if I run Git status, you can see that that is exactly the case. We have modified the TODO file. Now if I use git checkout -- TODO file name, to discard the changes that are in working directory in the TODO file. You can see that the plus sign has now disappeared. Also, if I was to make a file called hello.txt, you can see we have a question mark which means we have an untracked file but as soon as I remove hello.txt we now no longer have a question mark because their are no untracked files. Finally, let's make sure that the actual branch name is working. If I went git branch, we have two branches, master and gh-pages. And if I run git checkout, I'll use our alias and git checkout gh-pages, you can see that this has changed. Our label here now says gh-pages, so we know which branch we're on. And I can switch back with gco master, and now this does say master. So there you have a very simple way to customize your command prompt so that it shows you some information about your Git repository. And there's a lot more that you could do with this. You can actually have colors, so we could say if it's a clean working directory, make the branch name show up in green. If it's not clean, make it show up in red. And you can do all kinds of different things with that. And that's why I recommend some of those tutorials that you'll see on NetTuts, as well as other places around the web, will help you customize your prompt to be exactly the way you want it to be.

Back to the top