Git Tutorial – Git Fu With The Command Line

In this Git tutorial learn how to add autocompletion to the command line, and how to coerce the prompt into providing you with Git-related context and help By Jeff Wolski.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Test It Out!

Open a new terminal window. Just like in bash, autocompletion happens when you type the [tab] key. So, start typing git [tab][tab] and be sure to type a space after ‘git’ before hitting the tab button. You should see the output below.

 ~ $ git 
add                      column                   get-tar-commit-id        pull                     shortlog 
am                       commit                   grep                     push                     show 
annotate                 config                   gui                      rebase                   show-branch 
apply                    credential               help                     reflog                   stage 
archive                  credential-osxkeychain   imap-send                release                  start 
bisect                   deliver                  init                     relink                   stash 
blame                    describe                 instaweb                 remote                   status 
branch                   diff                     log                      repack                   submodule 
bundle                   difftool                 loggraph                 replace                  subtree 
checkout                 fetch                    merge                    report                   svn 
cherry                   filter-branch            mergetool                request-pull             tag 
cherry-pick              finish                   mv                       reset                    whatchanged 
citool                   format-patch             name-rev                 revert                   
clean                    fsck                     notes                    rm                       
clone                    gc                       p4                       send-email               
 ~ $ git 

Sweet! A list of all the Git commands pops up, and you can continue typing where you left off.
Rage Comic Doctor is In

Note: If you get error messages instead of the cool list above, then run brew doctor, as mentioned earlier. Fixing the issues the good doctor finds should solve your problems. You’ll know everything is fixed when brew doctor gives you the message “Ready to Brew.”

A conflict with the Git installed by Xcode is a common issue. Try overwriting that Git using brew git install followed by brew link git.

If there are problems, brew doctor will provide you with instructions to fix them.

Now navigate to a Git repository (Navigate to the git-extras folder you installed earlier if you don’t have any repos of your own).

Type git bra[tab], and you’ll see that autocompletion recognizes this as git branch. Continue typing ma[tab], and autocompletion extends this to git branch master. YAY!

[ctrl]-c out of that and try a different command. Type git log --gr[tab][tab] and you see the two available flags. Continue typing a[tab][return].

Nice! You have a graph of your most recent commits! Now use autocomplete to run the following verbose command quickly, typing only as much as is necessary to trigger autocomplete:

log --graph --pretty=oneline --decorate --abbrev-commit

Very cool.

Wait, Wait! Wasn’t there something about remembering what branch I checked out locally?

Yes, thanks for reminding me. Is it even possible for a command-line environment to keep the user informed in real time? Well, what about the prompt?

The prompt is a truly excellent way to keep you informed about the state of the local repo that’s in your current working directory, especially if you have multiple terminals open inside multiple repos.

A Branch in Your Prompt

Before continuing, are you curious to see what your current prompt really is?

In your terminal window, enter echo $PS1, and you’ll see the prompt’s definition. The default prompt for bash is ‘\s-\v\$ ’

When you installed the completion script, you also installed the bash-prompt script as well. So now it’s pretty easy to set up a custom prompt to show your repository’s active branch.

In your terminal window, enter the following. (Note that there is no space on either side of the “=” sign, and that there are two underscores at the beginning of __git_ps1.)

PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

Now you should have a prompt that looks something like this.

[jeff.wolski@Jeff-Wolskis-MacBook-Pro git-extras (master)]$ 

Well, the branch is there, but this prompt is not as useful as it could be. Enter each line below (one at a time) into your terminal command line, and observe what happens to the prompt.

Start with just the branch name in the prompt using the function __git_ps1.

PS1='$(__git_ps1 "%s")'

The "%s" is a placeholder for the value returned from the function.
You need some space between the prompt and the cursor, so add a space after the function call.

PS1='$(__git_ps1 "%s") '

Now add a $ near the end.

PS1='$(__git_ps1 "%s") \$ '

How about some parenthesis around the branch name?

PS1='$(__git_ps1 "(%s)") \$ '

Some people like to see the present working directory, so add \w to the beginning of the prompt.

PS1='\w $(__git_ps1 "(%s)") \$ '

But a nicer format might be to use \W instead (it’s much less verbose).

PS1='\W $(__git_ps1 "(%s)") \$ '

Many people switch between users and like to the current one to be in the prompt. Add \u to the beginning of the prompt now.

PS1='\u \W $(__git_ps1 "(%s)") \$ '

If you appreciate a reminder of what machine you’re on, add the machine name to the prompt with \h.

PS1='\u@\h \W $(__git_ps1 "(%s)") \$ '

Perhaps you need a timestamp for each command as you look back in your terminal history? Put a date function into your prompt with:

PS1='$(date +%k:%M:%S) \W $(__git_ps1 "(%s)") \$ '

Now close the terminal window you’ve been using and then open a new one. You’ll discover the prompt you just created is no longer there!

If you create a prompt you’d like to be the default for all your terminal windows, type it into the .bashrc file in your home directory — create the file with touch .bashrc if it does not yet exist. The bash shell reads the ~\.bashrc file whenever it starts up.

Where To Go From Here

Congratulations! You’ve set up Git completion on your machine, which should save plenty of brain-power, time and typing. By completing this Git tutorial, you also learned how to create a customized prompt with the branch name included.

Play around with different options for a custom prompt until you find one that’s right for you. You can find more bash prompt options here.

Also, Homebrew is a fantastic tool you can use to install any number of other command line enhancements. Learn more about Homebrew to discover other ways it might enrich the quality of your life :].

Finally, check out How To Use Git Source Control with Xcode in iOS 7, if you haven’t already.

Thanks for working through this Git tutorial! If you have questions, comments or just want to share your discoveries, leave your notes in the comments below.

Jeff Wolski

Contributors

Jeff Wolski

Author

Over 300 content creators. Join our team.