I prefer to interface with git via the command line, at least for the simple commands. However, I’m a bit lazy, and don’t like having to repeatedly type out the same long commands multiple times a day. Over the years, I have created some short aliases that allow me to more quickly use git via the command line.
How to create an alias
Bash aliases allow us to set keywords that can trigger longer commands. They are defined in a
. bashrc , typically in the home directory for the user on the machine. For example, my
.bash_profile lives in
Macintosh HD > Users > ireaderinokun > .bash_profile .
A bash alias looks like this:
The name can be whatever we want, as long as it isn’t already mapped to some other command. The command is what we want the alias to map to. For example, an alias to open up this website in Chrome could look like this:
alias bitsofcode="chrome https://bitsofco.de"
Then, in a new terminal window, if I type in
bitsofcode, the URL will open in Chrome.
Lazy git aliases
Now onto the aliases.
The first alias I ever created was to shorten the process of adding all the unstaged changes and committing them with a particular message.
alias gac="git add . && git commit -m" # + commit message
To use this, I would type out the alias, plus the commit message I wanted.
gac "My commit message"
Another useful alias is initialising a repo with the
"Initial commit" first commit message.
alias gi="git init && gac 'Initial commit'"
As you can see here, we can use other aliases within the commands for new aliases.
Pushing & pulling
Pushing to remotes and pulling from remotes is another common command.
alias gp="git push" # + remote & branch names alias gl="git pull" # + remote & branch names
To get even lazier, I created aliases for pushing from and pulling to the common
origin remote and/or the common
# Pushing/pulling to origin remote alias gpo="git push origin" # + branch name alias glo="git pull origin" # + branch name # Pushing/pulling to origin remote, master branch alias gpom="git push origin master" alias glom="git pull origin master"
Finally, I have a few aliases for working with branches. To create create a new branch or checkout into an existing branch, I have the following aliases:
alias gb="git branch" # + branch name alias gc="git checkout" # + branch name
To create a new branch and checkout into it, I have the following alias:
alias gcb="git checkout -b" # + branch name
You can view my
.bash_profile on my Github dotfiles repository. What aliases do you have?