• Servers and Tools

Customize your Prompt

From the class:  Getting Started with Git

To conclude, I'm going to show you how I get this nice command prompt here, which shows me what branch I'm on in the current project and whether or not I have untracked files or whether the files are dirty relative to the index or if I have changes relative to the upstream. There's all kinds of different prompts you can show here that are really useful when you're working in Git projects.

We need to grab a shell file that we can use to get the shell command that will allow us to do this. And if you head over to GitHub and go to the GitHub Organization for git-- so this is actually the source code for git-- we can click on the git project and then head into the Contrib folder and Completion. Or on Google, you can just search for git prompt sh, and you should be taken almost right away to this file.

Go ahead and click on that file directly. It will open it up in GitHub. And you can click this link over on the right called Raw. That's going to give us the raw file. And we're going to curl that into our home directory, so that's why we need the raw file. And this URL up here will give us this raw sh file.

And this file actually gives us pretty nice instructions on what to do to get this installed. So first we need to Copy this file to some directory. We can copy it to our home directory, like this. And then we need to source it inside of our bashrc, and that will kind of load it up. And then we can change our prompt environment variable ps1 to call this weird looking __gitps1. And then we provide it this sort of syntax here with a %s, and that will update our prompt to use the GitHub information.

So let's head over and actually download this file. But before you do that, go ahead and Copy the URL. Then, inside of your home directory, you can use curl or wgit and paste in that URL and just save it to I'm saving it right in my home directory. OK, it should be in the directory now. And just to double check, I'll cat it out. It looks like it is. Good.

Then open up your .bashrc file in whatever editor you'd like. The first thing we need to do is to source that shell file that we just downloaded. So we're going to source they git prompt, sh file for git commands or git shell commands-- or functions, I guess I should say. And I can use dot or source, just like we did in the shell class. And I'll bring in the

So now we have access to the special functions that this file gives us. Then I'm just going to Paste in some extra code here. The way that we can figure this prompt is by using environment variables. And if you go look at this, the file, the source code for this file, it'll give you all the different environment variables you can use. But I've given you a few examples here, like the way that we want to show the description, how we want to show the upstream, and the dirty state.

Then I'm going to customize the Command prompt by exporting the ps1 environment variable. And let me show you what these variables actually mean. The first thing I'm doing is /w. That means show the working directory that I'm currently in. And then /$, we're going to actually execute this piece here. And inside of that, that's where we're going to call the special function __git_ps1 space.

And then inside of these brackets, the %s is where we actually want to print the information. And then the function will take care of putting the right information in this area for us. And then at the very end, a /$ means just literally print out this $ so that we see it in the command, in a little space.

And then you'll see in the command prompt, if we open up a new window and navigate into the Source folder My Project, now we get this nice parentheses here that tells us that we're on the master branch and that the upstream is equal to what we have locally. Upstream is like the remote repository. So this all came from using this function right here in our bashrc.

And you might have noticed that there's no color there like I have in mine normally. Normally I show some nice colors that indicate the different states. So I'm going to give you a link to a more advanced bash file that includes these colors. You can head over to this cmather area in GitHub, to my account, and look for the dotfiles project. And inside of that, there's going to be a prompt.bash, and you can take that and reuse it.

Just be careful with the sourcing script here. This needs to point to the git prompt sh for the location on your machine as opposed to where I have it here. And if you scroll down a little bit on this page, you'll see that I've got the different colors defined. And then at the very end, I'm using this special escape syntax to say I want this to be bright black and then just show the user and the host and then to make this green and then finally to reset it before actually showing the rest of it on the Command prompt.

So you can use this as an example if you want to make your prompt a little bit fancier.