Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
The course shows how to use Git, the popular open-source version control software, to manage changes to source code and text files. Using a step-by-step approach, author Kevin Skoglund presents the commands that enable efficient code management and reveals the fundamental concepts behind version control systems and the Git architecture. Discover how to track changes to files in a repository, review previous edits, and compare versions of a file; create branches to test new ideas without altering the main project; and merge those changes into the project if they work out. The course begins by demonstrating version control in a single-user, standalone context, before exploring how remote repositories allow users to collaborate on projects effectively.
Now that we understand the basics of working with branches, I want us to pause and add a little something extra to our configuration, and that is I want us to configure a command prompt to show us the branch. This is not strictly necessary, but I think at some point, most Git developers choose to do this, so it's worth us doing here. Now the command prompt is this bit that's right here at the beginning. So every time I hit a Return it prompts me with a bit of text. Yours almost certainly looks different than mine, because I have already configured mine just to say Kevin with the dollar sign after it.
What we're going to do is have that include our branch name as well. So that way it will always tell us which branch we are on. Every time we enter a command the current branch is sitting right there at the beginning of the line, and that will help to make sure that we don't end up in a feature branch when we actually meant to be on our master branch. We will take a look at this in two parts. First we will look at the Unix side of things, that's for Mac OS X and Linux, and then we will look at Windows. The first thing you want to do if you are on Mac or Linux is to make sure that you install that Git Completion Script that we had at the configuration.
You remember we put that into our root directory here, .git-completion.bash, and then we also made an entry to load it in either our bash_ profile or a bashrc file, so one of those should be loading in git-completion. And the reason why we want that file is because that file declares a function that we are going to want to use. That function is __git_ps1. You can just type it from the command line, and it will then return to us the name of the branch that we are on. If we swap branches, well then that would change to show us the current branch.
So this is the function that we are going to use, and we're going to use that inside our prompt. If you are not in the Git directory, let me just go backwards into my Documents folder, it's where I am now, see I'm just in Documents, now if I do, __git_ps1, it doesn't put anything there at all. So it's only when we are in a Git directory that it does that. So let's go back into cd explore_ california/, and we can configure our prompt. And let me stop to give you a quick tutorial about prompts in Unix, if you don't already know. The prompt is stored in a variable called PS1, that's for Prompt String 1, and we can see our current value of Prompt String 1 with echo and then a $PS1.
That comes back and tells me what it is mine is the literal characters kevin$. Yours will likely include some letters and symbols that represent dynamic data, and you can have all sorts of things in your command prompt, you can have it show the current time, the date, you can have it show the current directory you are in, all kinds of things. You can Google for information about all things that you can do with it. If you want to set your prompt to something else, the way you want to do that is with export PS1, no dollar sign in front of it this time, equals, and then inside single quotes we can put whatever we want the prompt to be.
But let's just make it a bunch of arrows. So now that's our prompt, or we can make it some dashes with an arrow, that's our prompt. It can be absolutely anything we want. So mine was the literal characters, kevin$ with the space after it. The space is important because it's going to determine where we place the cursor. So that's the basics. So now let's actually use that function and set it to what we want. So I am going to just erase all of this, and we are going to put inside these single quotes is we want to do a command substitution.
So we do that with $(__git_ps1) and to that we are going to pass a format string, for how it auto format things. So we will put that inside double quotes and then the important part is that you have %s, that's where the branch name is going to go. Around that %s I am going to put parenthesis so that's what I want to have around mine is just some parenthesis. You can do something different. If you want square brackets or curly braces, anything you want, this is how it's going to format that output.
And at the end, after that command styling, I am going to have it put a space and then arrow and a space. Let's go ahead and just hit Return, and you will see that's what it gives me now. For every one it just tells me what branch I am on. In addition to that, you can put other things in here, you can look up how to configure your prompt and customize it to your heart's desire. The way that I do it, and what I am going to have you do and recommend for you, is to use \W, and that's going to be your current working directory, not your working directory in Git but the working directory that you are in Unix.
So when we hit Return, you will see it tells me that I am in the explore_california directory. If I go backwards one, it tells me I am in Documents with no branch after it. So that's kind of nice. So suddenly you go into a Git repository and the branch just appears appended at the end. So it tells me I'm in explore_ california, and I have the master branch. This works great except as soon as I close this window that's going to go away, that export command that I did right here is only active as long as this window is still active.
In order to keep it around I need to copy this, and then I need to edit my bash_profile or my bashrc file. So here we are, I have bash_profile as what I am using, if you are using a bashrc file instead, that's fine. The way I am going to edit that is just with nano, nano .bash_profile, and you can see that I already have a declaration here that I was using before, I am going to take that out, I am just going to put in our new one, but I need to put it after I've loaded in the source code for git-completion.bash.
You can load it inside the if statement if you want or you can load it outside, either one. This will just make sure that it is declared first, so it's not a bad idea. And then Ctrl+X, Yes to save changes, Return to keep the file name, and now it will be there every time I load up a new window. Let's just test it to see. I will close that window and open a new one, and there I am, cd into Documents/explore_california, and it tells me which branch I'm on. Git branch, see the list of branches, git checkout and then let's do a seo_title, and now switch to seo_title, and that's what it tells me each and every time.
Now I want to show you how to get set up on Windows. Now it may be super simple for you to set up on Windows, because you may already have it installed. When I installed my version of Git, it went ahead and installed the git-completion script, and that Git Command Line prompt, and went ahead and set my prompt for me so that it shows it. You can see here explore_california, and it tells me that I am inside my master branch. So you may have already been seeing this all along. Now if you aren't for some reason though, we can still set it up. It's the exact same thing, let's take a look, echo $PS1 will show what it's using currently, and you can see that there is some additional stuff here, there is / with some numbers after it, that does the code coloring. That's what it allows it to be green and yellow and things like that.
We can leave those in or take them out, it's also got my username and @ sign and the host name, that's the screen part here, followed by the part that we were installing here on Mac. Now if we want to do it ourselves we can do __git ps1 to see that it returns the same thing to us, and if we want to set our command prompt, the PS1, if we want to make it permanently be something different we can also do it that with our bash_profile. Now you probably don't have a bash_profile here unless you have created it for some other reason, but we can create it.
On Unix we would use the nano command in order to have a simple text editor, here we are going to need to use Notepad, we can just find Notepad here from our Program menu, it will open up, and we can type our command right here. So export PS1= and then inside single quotes, we are going to type \W$ and then inside parenthesis I will put our Git command, __git_ps1, space, quotes, parenthesis, and make sure I got that right, and then I also want to have a caret at the end.
So there is the same command that we had before, and then when I am done with that, I need to save it as, and I want to save it in my Kevin Skoglund directory, that's my user directory, and I want to call it .bash_profile, and I want to make sure that Save as type is set to be All Files, so it won't put any kind of extra file extension at the end. It will just be called bash_profile. So let's save that, and we can come back over here. If we do ls -la on our user directory, which is the tilde, we can scroll up here, and we can see that bash_profile is right there.
Now in order to run that bash_profile script, we could either close this window and start a new one, or I can actually just type source ~/.bash_profile, and now it runs that command and sets my prompt to be what I want it to be, which is just to have explore_california and then the branch name after it. So now it looks exactly like the Unix version. This one was all colored, it broke to a new line afterwards, you can keep that one if you like but my version now is going to be exactly like the Unix one.
Find answers to the most frequently asked questions about Git Essential Training.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
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.