Join Kevin Skoglund for an in-depth discussion in this video Exploring Git auto-completion, part of Git Essential Training.
In this movie I am going to help you to get auto-completion set up for Git. Now this is a nice bonus feature, it's not something that you have to have, you can safely skip over this movie if you want to. However, I think it's something that you're going to want over time, and it makes sense for us to go ahead and set it up at the start. Now these instructions are going to be for Mac and Unix only, because Windows should already have auto-completion included, so you shouldn't have to do anything there. So if you are on Windows, you can go ahead and skip this movie, if you are on Mac and Unix, follow along with me. Now the first thing we are going to do is download it from GitHub. I am going to walk you through all these steps one at a time and actually show you how to do it.
I wanted to give you the commands here. The first is we're going to change to our user directory, then we are going to use the Curl tool which is going to go to the URL at GitHub and download the auto-completion script into our home directory. Then we will rename it. Essentially what we are doing is just changing the name to have a dot in front it so that it becomes a dot file rather than just a regular file, and then the last thing we'll do is we are going to add it to our login configuration, so it's either the .bash_ profile or that's the bashrc file. Now if you are not familiar with these two files, and you don't know the difference between them, the best thing is to add it to your bash_profile.
If you know the difference, then add it to the one that's best for you, but otherwise you just want to add it to bash_profile and have it there, and that will be the simplest. Now what are we going to put in that .bash_profile? Well, what we want to put is a bit of code, and again, I am going to actually do this with you, but I wanted you to see it nice and big, we are going to put in if the file .git-completion. bash exists then load in the file .git-completion.bash. That's what this code does. There is a little bit of shell scripting code that does that.
If the file exists, then load it. So let's see how to actually do that. So here I am in the command line. As I said, you want to just make sure that you're in your user directory, you should be by default when you first open the window. And then we are going to use curl with the -OL-- that's not a zero, that's an O--followed by and I am just going to paste in the URL here so I don't have to type the whole thing out while you watch, it's https://github.com/git/git/raw/ master/contrib/completion/git-completion.bash, very long.
Take a moment and pause it if you need to, to make sure that you copy it all down, and once you do, hit Return, and it should download the file for you. Now we can actually see that file with ls -la, and here it is right here, git-completion.bash. Now the next step is we want to turn that into a dot file. So we are just going to say move that file, and I am just going to put the tilde in front of it just to make sure we are targeting the right file, and I can hit the Tab. And if you start typing the file name, hit Tab, and it will auto complete which is exactly what we're trying to do inside Git, we are trying to add this exact kind of auto-complete feature to Git.
And then we can type it again, but this time we have to type it out, we can't auto-complete what we wanted to be, so auto-completion.bash, make sure you spelled it right, and then I am actually going to go ahead just to be sure and put the ~/ in front of it. So there it is. Now if we do ls -la again and see the listing, you will see that now it's listed up here with all the dot files, and it's been renamed with a dot in front of it. Now the next thing is we want to edit this bash_profile. Now again if you have a bashrc file, you can edit that instead, but I'm going to edit .bash_profile.
And the way that we want to do that--I am going to do it with the nano text editor--you can also do it with TextMate if you have TextMate installed,. But I am going to show you with nano because everyone should have nano installed .bash_profile. There it is. You can see that I've got a couple of things in there already for my configurations. You may have something different or nothing at all. Now what we want to put in here is that little bit of shell scripting code. Again, I am going to copy and paste it rather than type it all out, we just change the indenting here, there it goes.
So if and then in square brackets, space, -f, and then in my user directory, look for that git-completion file, close the square bracket, semicolon, then so that says if it can find the file, then load it. That's what this source does, it says read the file in and actually load everything that's in it, run it as a script. So that's what source does and the last line is fi. That's closing this if statement there, that's not a typo, it is fi, and it's what let's us know that we've finished from if down to fi is everything in the script.
I know it's kind of an odd way of doing things, but that's how the shell scripting works. And then when we are done, you will see down here it says Exit and to get this character, it's Ctrl+X, so Ctrl+X will exit, save it, and type a Y, and it will say what do you want to call this file just hit Return because it already has the old name there. And now when we close this window and reopen it, now it will have loaded in that git-completion code. Now we'll see that later, we'll work with it later, but if you follow those steps, it is installed now.
This is nice because it means we can do things like type git, and then we can start typing a word like H, and we hit Tab, it will now complete that word for us. It will do the same thing for commands and branches and a lot of the different things that we are going to be working with in Git, it will have those ready to auto complete on the command line for us and help us speed things up. Now in the next movie, we'll talk about what the command git help does for you.
- 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
Skill Level Beginner
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, .git-prompt.sh, as described here: https://github.com/git/git/commit/af31a456b4cd38f2630ed8e556e23954f806a3cc.
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 ".git-prompt.sh" as shown here: https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh.
Q: When I use the code the instructor advises in the above video ("git config --global user.name "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.
1. To add "-mmacosx-version-min=10.6" as described here:https://stackoverflow.com/questions/14268887/what-is-the-illegal-instruction-4-error-and-why-does-mmacosx-version-min-10https://stackoverflow.com/questions/10177038/illegal-instruction-4-shows-up-in-os-x-lion
2. Or to use the version of git that comes with Xcode, or to use homebrew to install git instead.http://superuser.com/questions/697144/installed-git-not-sure-how-to-get-it-working