If you’ve never heard of aliases in linux, the simplest way to explain it is that they are shortcuts to the command line, instead of typing a huge command, or not so big, you type a simple command.
An example, instead of:
git add . && git commit -m "initial commit"
You can use:
git add . && git commit -m "initial commit"
Which makes it even easier for very used commands, right?
Enough talking and creating these scripts, or if you prefer, I already created a script here which installs a lot aliases for you.
Alias
To create an alias is very simple, just add a line at the end indicated in the file that loads your terminal, something like this:
alias gac="git add . && git commit"
And what is this file? most of the time just add it to the ~/.bashrc
file. but if you use another shell like zsh it will be ~/.zshrc
, and if it’s another one, just look in its documentation and you’ll find the file.
And easy like that, you can save a lot of time with this, but this way we can have a problem when installing on a new machine, let’s say we have many aliases, it will be a bit annoying to search inside these files, since many times they there are many other settings.
To make it easier, let’s create a ~/.aliases
file with:
touch ~/.aliases
we edit the file as nano
, or simply:
echo 'alias gac="git add . && git commit"' >> ~/.aliases
Important: when using this form be sure to use >>
and not >
, the first option adds the text to the file but the second replaces the entire content of the file with the value passed.
And with that done we still need to load this information in your terminal so just go to the ~/.bashrc
or ~/.zshrc
file and add:
[ -f ~/home/diego/.git_aliases ] && source ~/.git_aliases
After adding your alias, save the files and close and reopen your terminal before testing.
Git alias
For us developers, git is the most basic and most used tool of all, and some commands can also be tiring to be typed all the time, there is a simpler option that comes from the very git for adding aliases.
A simple way is with the command:
git config --global alias.co checkout
And the git commit
becomes:
git co
If you want you can also remove the alias with:
git config --global --unset alias.co checkout
Script that save lives
As I’m always having to create these aliases I decided to create a script, if you want, just clone and install and already configure some cool aliases:
Alias | Comando |
---|---|
git co | git checkout |
git br | git branch |
git ci | git commit |
git st | git status |
git unstage | git reset HEAD – |
gac | git add . && git commit |
gi | git init && gac -m ‘Initial commit’ |
gam | git commit –amend |
gadm | git add . && git commit |
gp | git push |
gpo | git push origin $(git rev-parse –abbrev-ref HEAD) |
gpu | git push -u |
gpou | git push -u $(git rev-parse –abbrev-ref HEAD) |
gl | git pull |
gst | git status |
glog | git log |
gfo | git fetch origin |