FREELessons: 24Length: 3.1 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

6.3 Creating Git Aliases

Now that you're familiar with several handfuls of GIT commands, you might be wondering this. Is there a way to shorten some of these commands? After all if your typing them that often, you're going to want to save yourself a little bit of time right? Creating aliases for your GIT commands is really simple. That's what we're going to look at doing in this video. There are two ways of creating aliases for your GIT commands, and we are going to first look at the way the GIT provides as a built in solution. It's actually part of your config file. So we're going to use the GIT config command to actually create our alias. We're going to use the global flag, so that this will be set at the user level, and we can use it in any project on this computer. And then we want to say alias dot, and now you want to put the name for the alias. So as an example, let's say we want to shorten the git status command to just git s. I can do alias.s and after that I'm just going to put status. I hit enter, and now we've created and alias for the git status command. If I run cat on our git config file, you can see right down here at the bottom, in the alias section here, we've said that the s alias is equal to the status command. Right now and within our project directory. So if I run git status you can see that we do get the information telling us that our working directory is clean. However I could just also run git s, and you can see that we get the same information. So that's how the alias works. We set an alias in our git config file, using git config. And then alias dot and then whatever you want the alias to be, and then the full name of the command. So let's create another one here. Let me clear the screen, and we'll create a few more. We can have git config alias.a is short for git add. How about git config--global for checkout. Or we could do cm for commit. Now we can actually use some of these if we make a change to the read me file here. Now I can run git s and we can see that we've modified the read me file. Then I can do git a. to add all the files. Git s shows us that this change has been staged to be committed and now I can just do git cm-m and I can put a message here. And now you can see that we have actually made our commit right there. So now we just use three of the alias we created, git s, git a, and git cm to save us a couple of keystrokes as we go about our different git tasks. So that's the built in solution that git gives you to create aliases. However you may still feel that those are a bit too long, and you wanna create something a little shorter. The problem with the built-in aliases is that you always have to write, git space at the beginning of every command. And that's four whole characters. Let's see if we can shorten that. The way to make these shorter aliases is not to use the git alias functionality. But to use the alias functionality that's part of your shell. Here, I'm running bash, so I can open my bash_profile page. Depending on your setup, you might either open up your bash_profile as I am or you might open up your .bash RC, or if you're using Azeta H shell, which is what I usually use, you'll open your RC file. So I'm going to open up this file, and as you can see, it's pretty minimal. So let's create a few aliases. And the way to do this in bash, and this also works in Zed-SH is alias space, and now we want to put our alias. So let's say we want GA, then we have an equal sign, quotes, and I can say git add. And now ga is an alias for git add. We can also do something like gst to be an alias for git status. And we could do gco could be an alias for git checkout, and just gc to be an alias for git commit. The nice thing about using Shell aliases is that we could actually create an alias that is more than just a command name but also includes flags or parameters. For example, I could do alias gi. And say this equals git add-i to open up the interactive prompt. Or I could do alias gaa to add everything in the project directory to the staging area. Let me save and close this file and now you'll either have to reopen your terminal. Or just use the source command to source your dot bash underscore profile file to make those aliases accessible. And now back within our project directory, I can just use GST as an alias for get status. If I open up the read me file and remove the change we just made. Then I can do ga. As you can see, I haven't specified anything. So I get this message, nothing specified, nothing added. This is just git add. However if I do gaa, you can see that we don't get the message. Because gaa stands for git add all. So now gst will show us that we have actually added the modified file there. And now I can just do gc-m updated README, and you can see it made the commit. So, here we've just used our gc alias, our git add alias, our git add all alias, and our git status alias, and this is bringing the basic git commands down two or three characters. Not including any flags or parameters, but of course, we're saving ourselves some time by having these really short characters. So is it a good idea to use Git aliases like this? Well, if you're using Git a lot, the time this saves in gonna add up quite quickly, and you're gonna find yourself being much more productive. However, the drawback here is when you get onto a computer that doesn't have those aliases or uses different aliases than you're familiar with. Some people come from the school of thought that says you should use the stock options in almost any tool as much as possible so that you'll be most familiar with those options when you move to a different computer or when you're in some situation where you don't have your own customized settings. Other people say to customize your tool as much as possible so that it will work best for you and allow you to be as productive as possible. I say give it a try and see if it works for you. If you like the aliases keep them, if you don't, well there's nothing stopping you from not using them because even once you've created them aliases in either one of the ways that we talked about, the vanilla git commands will still be available to you, so you don't have to use the aliases but give it a try and see what you like. And that's all for git aliases.

Back to the top