DEV Community

How I Set Up Git Aliases on Bash

Jess Budd on August 13, 2019

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 ...
Collapse
 
haruanm profile image
Haruan Justino

alias such=git<br>
alias very=git<br>
alias wow='git status'<br>
wow<br>
such commit<br>
very push

Collapse
 
anargh profile image
anargh

Made me giggle out of nowhere

Collapse
 
grsahil20 profile image
Sahil

Instead of polluting my .bashrc or .zshrc, I like to keep aliases in ~/.aliases file and then include them

if [ -f ~/.aliases ]; then
    . ~/.aliases
fi

Also, the best alias i ever made was

alias baa='subl ~/.aliases'

This alias enables me to open my alias file immediately to add more aliases.

Collapse
 
metruzanca profile image
Samuele Zanca

Definetly going to steal your first snippet. Thank you

Collapse
 
djdole profile image
DJDole • Edited

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'

Collapse
 
grsahil20 profile image
Sahil

just check which new_alias and it will tell you if there is something existing and you don't override by mistake.

Collapse
 
roshnet profile image
Roshan Sharma

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 becomes gp origin dev
git fetch --all --prune becomes gfp
git log --graph becomes glg

... and lots more.

Collapse
 
raphink profile image
Raphaël Pinson • Edited

I use one or two letters aliases for the most used commands:

alias g='git'
alias k='kubectl'
alias d='docker'
alias t='terraform'
alias be='bundle exec'
alias s='summon'

and then Git aliases in ~/.gitconfig:

[alias]
    since = !git log $(git merge-base --fork-point master)..HEAD
    t = tag -s
    please = push --force-with-lease
    commend = commit --amend --no-edit
    pr = !"pr() { git fetch origin pull/$1/head:pr-$1; }; pr"
    branches = !"git for-each-ref --sort='-authordate:iso8601' --format=' %(color:green)%(authordate:iso8601)%09%(color:white)%(refname:short)' refs/heads"

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 😁

Collapse
 
justdaven profile image
justdaven

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.

Collapse
 
mzaini30 profile image
Zen

Try to create upload file in ~/bin/.

Content:

git status
git add -A .
git commit -m "upload"
git push

Then, run sudo chmod +x upload.

Just type upload to push from local to Github.

Collapse
 
merriam profile image
Charles Merriam

You missed the most important one!

 alias g='echo "
    gs = git status, what is staged or modified
    ga = git add <files>, add or stage files
    ....
    git init = start a new git repository
 "'

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.

Collapse
 
adisreyaj profile image
Adithya Sreyaj

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.

Collapse
 
zybraxis profile image
Jerrell Wade • Edited

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.

Collapse
 
mindset profile image
Jayesh Tembhekar ⚡

I tried to run this command in git bash

npm install

but it says

npm not recognized

do anyone have any solution for this ? 🥺
( powershell and cmd is recognizing but not git bash )

Collapse
 
waylonwalker profile image
Waylon Walker

I like to wrap make aliases that utilize fzf. I made several ones for git commands before I realized the forgit repo did a much better job.

github.com/wfxr/forgit

Collapse
 
ajkachnic profile image
Andrew

I think that adding a gaa alias that adds all files is also helpful

alias gaa="git add --all"