It was almost 2 year agos I first saw someone in a tutorial using an alias for their git commands. I thought at the time it was something I should set up, but not being incredibly comfortable with the command line, I didn't look into it much further.
What's an alias?
For those that may not have heard of aliases before, they're like shortcut commands in your terminal. You type the shortcut in place of typing the whole command.
For example instead of typing git push
whenever I want to push my repo, I can assign a shortcode for that command and simply type gp
(or whatever shortcut I set) to do the same thing.
Magic!
Setting up an alias
This week, thinking it was about time I set up these aliases, I went off to google. I found a bunch of posts on how to create git aliases like this one and this one, but got a little overwhelmed by the different options and as with most tutorials they assumed a fair bit of up front knowledge.
Following the most simple format example, I typed my most common git actions into the command line window I already had open for my current project.
alias gs='git status '
alias ga='git add '
alias gc='git commit -m '
alias gp='git push'
alias gb='git branch '
alias go='git checkout '
alias gm='git merge '
alias gpl='git pull'
alias gl='git log'
I then tentatively tested my shiny new aliases with a gs
.
It worked! Huzzah!
I went along my merry away.
Until the next day, when I got my dev environment up again and tried to use my new aliases. They didn't work.
-bash: gs: command not found
Sad face.
Setting up an alias globally
It turns out I had only created temporary aliases that existed in that sinlge CLI window only. To create global aliases that would persist in all future windows I needed to add them to my .bash_profile
.
On a mac, the .bash_profile
file is located in the home username folder. This was a hidden file, so I needed to show all hidden files by pressing cmd + shft + .
to find it.
So in my .bash_profile
file I typed the exact same code as I'd typed the day before.
alias gs='git status '
alias ga='git add '
alias gc='git commit -m '
alias gp='git push'
alias gb='git branch '
alias go='git checkout '
alias gm='git merge '
alias gpl='git pull'
alias gl='git log'
I saved the file, closed down all my terminal windows and tested the alias in a fresh window and they worked. Yay!
Other methods
Over the next couple days (thanks to the twitterverse) I found there are possibly better ways to do this. For example Atlassian has a good article that describes how to set up your aliases through the gitconfig file.
One fender mentioned oh-my-zsh comes with git aliases built in. Another mentioned putting the git aliases in a seperate alias file and then referencing that in your .bashrc file.
I'm sure there are good reasons to choose one method over the other, but for now I'm happy with the way I've done mine as it's fine for my situation.
Top comments (16)
Made me giggle out of nowhere
Instead of polluting my
.bashrc
or.zshrc
, I like to keep aliases in~/.aliases
file and then include themAlso, the best alias i ever made was
This alias enables me to open my alias file immediately to add more aliases.
Definetly going to steal your first snippet. Thank you
They're not git aliases, but bash aliases for git commands.
You can also use bash functions within aliases, in order to provide richer functionality rather than mere command string replacement.
But one should avoid overuse of aliases, as it can severely hamper learning the raw commands.
Also, be sure to test the command the alias is defining.
Make sure you're not overriding any existing commands (unless that's your intention, I.E alias rm='trash').
You CAN after all, make an alias to replace even the most basic commands, and this can really screw your account...
alias ls='/bin/rm'
just check
which new_alias
and it will tell you if there is something existing and you don't override by mistake.Great post!
I'd like to add that using zsh (an alternative to bash) with oh-my-zsh comes with an advantage - it does that for you!
So,
git push origin dev
becomesgp origin dev
git fetch --all --prune
becomesgfp
git log --graph
becomesglg
... and lots more.
I use one or two letters aliases for the most used commands:
and then Git aliases in
~/.gitconfig
:As a result, most of my work in feature branches is a series of:
g commend
g please
until I'm happy with a PR 😁
These are great, Jess! Thank you!
To add my $.02, to get specifica about the "separate alias file" I would recommend putting these in the .bash_aliases file (one may not exist on your system, but the default .bashrc (at least in Ubuntu) has a command that will load it if it exists. It then makes the aliases more portable, in my opinion.
Try to create
upload
file in~/bin/
.Content:
Then, run
sudo chmod +x upload
.Just type
upload
to push from local to Github.You missed the most important one!
Now you can have a single key to remind you of all your git aliases! Maybe use 'c' for all the Conda aliases as well.
I've been using git aliases for quite a while. This makes the commands even shorter. Great!🤩
Can wait to set it up.
Thanks for sharing, Jess.
I try to learn something new everyday and I didn't know the true conceptual meaning of alias pertaining to git cmd. as short cuts. Thank you and for the deferent links to other methods of setting them up. Master of none and noob to most. See bash alias for git ooowe or its just early, good morn.