Alias command
Writing long commands is boring. Making commands smaller or chaining of commands helps. This can be achieved using alias
command. Alias command is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command.
For example, instead of writing:
git add .
git commit -m "COMMIT MESSAGE"
git push
we can write following to achieve the same goals.
gc "COMMIT MESSAGE"
gp
In the example, git commands were chained. Git doesn’t allow chaining using git alias
command but we can simply overcome that by alias
command as shown above. Do visit git-scm.com to evaluate what’s best for your use case.
Usage
For GNU/Linux (bash)
- Add
.bash_aliases
file in~
/home/USER_NAME
directory:
$ touch ~/.bash_aliases
- Edit the file to add:
alias gc='git add . && git commit -m '
alias gp='git push'
Note - no spaces between the alias target and alias input commands and single quotes only.
- In terminal, write:
$ source ~/.bash_aliases
- Restart terminal.
For Windows (Git Bash)
-
Run git bash in administrator mode.
-
Navigate to
C:\Program Files\Git\etc\profile.d
.
cd ../../Program\ Files/Git/etc/profile.d/
- Open
aliases.sh
usingnano
:
nano aliases.sh
- Add following in the end:
# CUSTOM ALIASES
alias gc='git add . && git commit -m'
alias gp='git push'
Note - no spaces between the alias target and alias input commands and single quotes only.
- Save and exit
nano
:
CTRL+O
# PRESS ENTER
CTRL+X
- Restart terminal.
Use Cases
Some useful aliases can be:
alias update='sudo apt update && sudo apt upgrade'
alias rm='rm -i'
alias gc='git add . && git commit - '
alias gp='git push'
alias gr='git restore --staged'
My bash aliases can be found here.