Skip to content

Instantly share code, notes, and snippets.

@sahilister
Last active June 16, 2020 23:52
Show Gist options
  • Select an option

  • Save sahilister/3a343a692622770218e43c398aaffc59 to your computer and use it in GitHub Desktop.

Select an option

Save sahilister/3a343a692622770218e43c398aaffc59 to your computer and use it in GitHub Desktop.

Alias Command

Writing long commands is boring. Essentially shortening the commands 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.

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, chaining of git commands has been done. Git doesn't allow chaining using git alias command but we can simply overcome that by alias command shown. Do visit git-scm.com to evaluate what's best for your use case.

Implementation of alias command can be found in usage section. Some use cases can be found in use case section.

Usage

For GNU/Linux (bash)

  1. Add .bash_aliases in ~ (/home/USER_NAME)
$ touch ~/.bash_aliases
  1. Open the file using editor and add
alias gc='git add . && git commit -m '
alias gp='git push'

Bear in mind, no spaces between the alias target and alias input commands and single quotes only.

  1. In terminal
$ source ~/.bash_aliases
  1. Restart terminal.

For Windows (Git Bash)

  1. Run git bash in administrator mode.

  2. Navigate to C:\Program Files\Git\etc\profile.d .

 cd ../../Program\ Files/Git/etc/profile.d/
  1. Open aliases.sh using nano
nano aliases.sh
  1. Add following in the end
# CUSTOM ALIASES
alias gc='git add . && git commit -m'
alias gp='git push'

Bear in mind, no spaces between the alias target and alias input commands and single quotes only.

  1. For saving and exiting nano
CTRL+O 
# PRESS ENTER
CTRL+X 
  1. Restart terminal.

Use Cases

Some of the aliases i use are

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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment