Git Essential Training
Illustration by John Hersey

Configuring the command prompt to show the branch


Git Essential Training

with Kevin Skoglund

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Video: Configuring the command prompt to show the branch

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.
Expand all | Collapse all
  1. 2m 46s
    1. Introduction
      1m 7s
    2. How to use the exercise files
      1m 39s
  2. 19m 44s
    1. Understanding version control
      4m 8s
    2. The history of Git
      7m 58s
    3. About distributed version control
      5m 4s
    4. Who should use Git?
      2m 34s
  3. 26m 12s
    1. Installing Git on a Mac
      3m 44s
    2. Installing Git on Windows
      5m 37s
    3. Installing Git on Linux
      1m 30s
    4. Configuring Git
      7m 29s
    5. Exploring Git auto-completion
      5m 35s
    6. Using Git help
      2m 17s
  4. 15m 49s
    1. Initializing a repository
      1m 58s
    2. Understanding where Git files are stored
      2m 34s
    3. Performing your first commit
      2m 4s
    4. Writing commit messages
      5m 22s
    5. Viewing the commit log
      3m 51s
  5. 17m 44s
    1. Exploring the three-trees architecture
      3m 57s
    2. The Git workflow
      3m 15s
    3. Using hash values (SHA-1)
      4m 7s
    4. Working with the HEAD pointer
      6m 25s
  6. 25m 52s
    1. Adding files
      5m 59s
    2. Editing files
      3m 56s
    3. Viewing changes with diff
      3m 35s
    4. Viewing only staged changes
      2m 28s
    5. Deleting files
      5m 29s
    6. Moving and renaming files
      4m 25s
  7. 19m 18s
    1. Introducing the Explore California web site
      2m 2s
    2. Initializing Git
      3m 48s
    3. Editing the support phone number
      6m 20s
    4. Editing the backpack file name and links
      7m 8s
  8. 38m 45s
    1. Undoing working directory changes
      3m 49s
    2. Unstaging files
      2m 37s
    3. Amending commits
      4m 50s
    4. Retrieving old versions
      4m 7s
    5. Reverting a commit
      3m 12s
    6. Using reset to undo commits
      3m 44s
    7. Demonstrating a soft reset
      4m 8s
    8. Demonstrating a mixed reset
      4m 7s
    9. Demonstrating a hard reset
      5m 8s
    10. Removing untracked files
      3m 3s
  9. 27m 22s
    1. Using .gitignore files
      8m 23s
    2. Understanding what to ignore
      4m 47s
    3. Ignoring files globally
      4m 49s
    4. Ignoring tracked files
      5m 26s
    5. Tracking empty directories
      3m 57s
  10. 26m 51s
    1. Referencing commits
      4m 52s
    2. Exploring tree listings
      3m 46s
    3. Getting more from the commit log
      7m 38s
    4. Viewing commits
      4m 4s
    5. Comparing commits
      6m 31s
  11. 39m 35s
    1. Branching overview
      4m 56s
    2. Viewing and creating branches
      2m 57s
    3. Switching branches
      2m 58s
    4. Creating and switching branches
      4m 53s
    5. Switching branches with uncommitted changes
      3m 26s
    6. Comparing branches
      4m 28s
    7. Renaming branches
      2m 28s
    8. Deleting branches
      4m 18s
    9. Configuring the command prompt to show the branch
      9m 11s
  12. 28m 32s
    1. Merging code
      3m 11s
    2. Using fast-forward merge vs. true merge
      6m 49s
    3. Merging conflicts
      7m 26s
    4. Resolving merge conflicts
      7m 5s
    5. Exploring strategies to reduce merge conflicts
      4m 1s
  13. 14m 34s
    1. Saving changes in the stash
      4m 5s
    2. Viewing stashed changes
      2m 39s
    3. Retrieving stashed changes
      4m 24s
    4. Deleting stashed changes
      3m 26s
  14. 1h 5m
    1. Using local and remote repositories
      6m 38s
    2. Setting up a GitHub account
      5m 39s
    3. Adding a remote repository
      4m 0s
    4. Creating a remote branch
      4m 3s
    5. Cloning a remote repository
      4m 26s
    6. Tracking remote branches
      4m 5s
    7. Pushing changes to a remote repository
      5m 8s
    8. Fetching changes from a remote repository
      5m 47s
    9. Merging in fetched changes
      4m 50s
    10. Checking out remote branches
      3m 22s
    11. Pushing to an updated remote branch
      2m 6s
    12. Deleting a remote branch
      3m 8s
    13. Enabling collaboration
      3m 40s
    14. A collaboration workflow
      8m 43s
  15. 16m 23s
    1. Setting up aliases for common commands
      5m 14s
    2. Using SSH keys for remote login
      2m 56s
    3. Exploring integrated development environments
      1m 4s
    4. Exploring graphical user interfaces
      4m 32s
    5. Understanding Git hosting
      2m 37s
  16. 55s
    1. Goodbye

please wait ...
Watch the Online Video Course Git Essential Training
6h 26m Beginner Aug 24, 2012

Viewers: in countries Watching now:

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.

Topics include:
  • 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
Git GitHub
Kevin Skoglund

Configuring the command prompt to show the branch

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 .

Expand all | Collapse all
please wait ...
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,, as described here:

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 "" as shown here:
Q: When I use the code the instructor advises in the above video ("git config
--global "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.
The workaround solutions people offer are:
1. To add "-mmacosx-version-min=10.6" as described here:
2. Or to use the version of git that comes with Xcode, or to use homebrew to install git instead.
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.

Mark all as unwatched Cancel


You have completed Git Essential Training.

Return to your organization's learning portal to continue training, or close this page.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from

Sign up and receive emails about and our online training library:

Here’s our privacy policy with more details about how we handle your information.

submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.