by Ryan Irelan
An alias, otherwise known as a shortcut, allows to place a simple command in front of a longer or less memorable command.
For example, we could type:
git commit -m "some change"
Or we could type even less and do:
git c -m "some change"
We’re only saving a few letters but considering how many times you commit in a day or week, it’s a bit easier on your fingers. Less tapping of the keyboard and more working on the fun code.
To create a Git alias, we need to open up our
.gitconfig file and edit it. Because we want to have these aliases available to us in every project, we are going to edit the
.gitconfig file in our user directory.
If you don’t already have the file in your user directory, that’s okay. We can have Git handle that for us.
We’ll create an alias first using the
git-config command, to which we pass the type of config file (in our case it’ll be global in our user home directory) and then define the alias.
git config --global alias.c commit
This will add the following line to our
~/.gitconfig file. Let’s take a look to see if it was saved.
git config --list
This lists out all of our config items and you should see the alias:
c = commit
listed as output.
Now we have access to that alias within Git. We can type:
git c -m "something"
and it will work just as if we typed out the full command.
Let’s open up the config file and take a look. We’ll open ours in Vim, but you can use any editor you want.
You should see something like this:
[alias] c = commit
Let’s add another alias by hand without using the
git-config command. It’s just a text file so we can edit it and type out our alias by hand.
Let’s add an alias to make it quicker to get the status of our working directory. Under the existing alias for commit, let’s add this:
st = status
We’ll save our
.gitignore file and then try it out.
And we should get some output from Git (assuming we’re inside a Git project) as if we ran the full command.
We can add another for
l = log
And how retrieving the basic log is two letters faster than before!
Okay, let’s do one more to quickly archive the entire repository.
Git provides a command called
git-archive that allows us to export the entire repository as either a zip for tar archive file.
git archive --format=zip -o latest.zip HEAD
This command will output the current repository at HEAD to the a zip file named
latest.zip. It will save the file right where you are in the repository.
This can come in handy if you have to quickly share your work with someone who is not in the Git repository or if you need to throw the files up on a server somewhere.
To alias this we add this to our
zip = git archive --format=zip -o latest.zip HEAD
Now when we run:
in a valid Git project directory, git-archive will zip up the entire project and place it in the project root.
You don’t have to have a fully decked out
.gitconfig file right from the get-go. Add aliases to your config file as it makes sense and as you need them. They can be tough to remember if you add too many at once!
We have courses, articles, and tutorials on Git. Start learning immediately!
Filed Under: Git