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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Git Tutorial – Git Fu With The Command Line
30 mins
Even the Best Tools Aren’t Without Fault
OK, the Git CLI is cool and powerful, but you might feel that:
- There’s a bunch of typing, especially if you’re using a formal branching strategy.
- It’s easy to misspell parameters, options, names of branches, remotes, files and commit hashes.
- Here’s the clincher: It’s easy to complete hours of work, only to find that you’ve been working on and committing to — and possibly even force-pushing — #gasp — the WRONG BRANCH.
- All in all, the CLI involves a lot more stuff to remember than just sticking with a GUI.
Stay with me; you’ll see the true value of the CLI in a few moments.
Those are fair points. Any type of GUI that’s outstandingly effective uses menus to organize the functionality servicing the myriad of use cases. Why do GUIs use menus?
Run an Internet search for ‘recognition vs. recall‘ and you’ll see a ton of results that talk about how much faster and more efficiently your brain recognizes something than it recalls something.
For example, pretend you’re trying to remember the name of that one actor who played a supporting role in your favorite movie of 2014, without peeking at your phone. Unfortunately, you just can’t seem to remember.
You could probably pick his name out of a cast list, because your marvelous brain will recognize the name even though it couldn’t recall it.
That’s precisely why GUIs use menus. Users can easily scan menus and recognize the item they want. You can actually use the same idea on the command line.
If you’ve used the command line to navigate around your file system, you probably use a combination of recall and recognition to navigate.
Imagine that you’re typing cd ~/D[tab][tab]
(recalling the first letter), and you’ll see a list of items in your home folder that start with the letter D. Then recognizing the name from that list, you type a few more characters, and then [tab]
to finish out the directory name.
Everything seems a little easier now, doesn’t it?
Meet your new friend autocompletion, who will help you by offering your brain something to recognize, rather than recall. It’s the next really cool Git CLI tip you’re going to learn all about.
Autocompletion – It’s Not Just For Navigation Anymore
If you’re an experienced terminal user, you probably have a totally awesome shell that you absolutely love, and it’s probably bash since that’s the default shell on OS X. If you use another totally awesome shell, this next section may or may not be relevant, since it’s based on scripts that aim at bash.
Now for the fun stuff! You’re going to install Git bash-completion, aka Git-flow-completion. This provides autocompletion of Git commands, parameters, options, branch names, filenames, remotes — and did I mention branch names??
This is amazingly helpful if you use feature branches. For example, I regularly use branch names that are more than 20 or 30 characters long, because the teams I work with use user-stories for the feature-branch names.
With autocompletion, you’ll prevent brain strain by using command line to help you recognize even the longest, most difficult-to-remember names.
Next up – Homebrew, the power tool that makes installing autocompletion (and more) a breeze.
Homebrew Installation
There are several ways to install Git bash-completion. Since you’re probably a Mac user, this tutorial will use Homebrew for the installation.
What is Homebrew? Are we making beer now?
Well, I’m glad you asked, and no you’re not going to learn how to make a refreshingly crisp ale in this tutorial.
Homebrew is a totally awesome package-manager, written in Ruby and utilising Git. In Homebrew’s own words it “… installs the stuff you need that Apple didn’t.”
Note: As with any software installation, it’s always good to backup your system and data before proceeding.
Note: As with any software installation, it’s always good to backup your system and data before proceeding.
First check to see if Homebrew is already on your Mac by entering brew -v
in the command-line.
- If you see something like
Homebrew 0.9.5
, then you already have Homebrew. - However, if you see
Command not found
, then you don’t have Homebrew installed. You’re going to change that right now.
Installing Homebrew is stunningly easy. Enter the following into your command line:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
This will download and run a Ruby script.
Note: If you’re running Snow Leopard or later, by default you should have a fairly modern and stable version of Ruby on your machine. However, it’s recommended that you run the latest versions of Mac OS X and Ruby.
Note: If you’re running Snow Leopard or later, by default you should have a fairly modern and stable version of Ruby on your machine. However, it’s recommended that you run the latest versions of Mac OS X and Ruby.
If you’re curious about what Homebrew’s installed, look in the directory /usr/local/Cellar/.
Updating Homebrew and Playing Doctor
Now that you have Homebrew installed, make sure all of the brew formulae are up to date as there are continual updates, pretty much every day. To bring it up to speed, just enter brew update at the prompt and give it a minute or so, as there are many formulae out there.
Optionally, you can use Homebrew to clean up your environment by entering brew doctor.
For this tutorial, it’s not mandatory and can feel a bit squirrelly because it will tell you about any oddities it finds in your system environment and tell you how to fix them. However, the good doctor’s output can be very helpful when you’re looking for a clue about why an installation went awry.
For this tutorial, it’s not mandatory and can feel a bit squirrelly because it will tell you about any oddities it finds in your system environment and tell you how to fix them. However, the good doctor’s output can be very helpful when you’re looking for a clue about why an installation went awry.
You’re almost there! Now that you have installed and updated Homebrew, you’re going to install the magical autocomplete tool. Enter the following command at your prompt:
brew install git bash-completion
You’re not quite finished yet. Next, you need to change your bash profile so that bash-completion loads whenever you open the terminal. Follow these instructions.
- Open a terminal window, and navigate to your home folder:
cd ~
- Check to see if the file .bash_profile exists in your home folder:
ls .bash_profile
- If the file is missing, create it:
touch .bash_profile
- Open the file in your favorite text editor:
open .bash_profile
- At the end of the file (unless you have a reason to put it elsewhere), enter the following:
- Close your editor and the terminal window.
if [ -f `brew --prefix`/etc/bash_completion ]; then . `brew --prefix`/etc/bash_completion fi
if [ -f `brew --prefix`/etc/bash_completion ]; then . `brew --prefix`/etc/bash_completion fi
if [ -f `brew --prefix`/etc/bash_completion ]; then . `brew --prefix`/etc/bash_completion fi