DEV Community 👩‍💻👨‍💻

Paul Golubkov
Paul Golubkov

Posted on

Most useful aliases for git

Hello everyone! In this article, I'll share with you some useful aliases for git that I use every day.

Working with branches

For each feature I work on, I create a new branch, this alias saves me a lot of time:

gco

This is an alias for

git checkout
Enter fullscreen mode Exit fullscreen mode

Usage

gco branch-name // switch to branch an existing branch
gco -b branch-name // create a new branch and switch to it
gco . // discard all changes in the working directory
Enter fullscreen mode Exit fullscreen mode

Commits

Every day I create a lot of commits, each time type git commit is boring, so I use these aliases:

gcmsg

This is an alias for

git commit -m
Enter fullscreen mode Exit fullscreen mode

Usage

gcmsg 'feat: commit text' // create new commit
Enter fullscreen mode Exit fullscreen mode

gca

This is an alias for

git commit --amend
Enter fullscreen mode Exit fullscreen mode

gcfix

This is an alias for

git commit --fixup
Enter fullscreen mode Exit fullscreen mode

Usage

gcfix commit_hash // create a fixup commit for commit with provided hash
Enter fullscreen mode Exit fullscreen mode

Rebasing

When you work on a large project, rebasing is part of your everyday routine, so this alias will save you some time of typing

gpr

This is an alias for

git pull --rebase origin dev // remote and branch may be different in your case
Enter fullscreen mode Exit fullscreen mode

Also often I need to rename, remove some commits or squash fixups in my local branch, for this I use this alias:

gria

git rebase -i --autosquash
Enter fullscreen mode Exit fullscreen mode

Usage

gria HEAD~4 // interactively change last 4 commits
Enter fullscreen mode Exit fullscreen mode

Pushing

After I've created some commits I need to push them, for that I use next:

ggp

git push origin $(current_branch) // instead of origin you can use your own remote
Enter fullscreen mode Exit fullscreen mode

What is $(current_branch) ? It's an function that returns name of current branch, It looks like that:

function current_branch() {
    ref=$(git symbolic-ref HEAD 2> /dev/null) || return
    echo ${ref#refs/heads/}
}
Enter fullscreen mode Exit fullscreen mode

How to keep aliases

If you just type in terminal command

alias gs="git status"
Enter fullscreen mode Exit fullscreen mode

it will work, but when you restart the terminal it will not.

For fix that I keep all my aliases in file ~/.bash_aliases and import them in my ~/.bash_profile like that:

if [ -f ~/.bash_aliases ]; then
  . ~/.bash_aliases
fi
Enter fullscreen mode Exit fullscreen mode

My ~/.bash_aliases looks like that:

function current_branch() {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || return
  echo ${ref#refs/heads/}
}

alias ~="cd ~";
alias ..="cd .."
alias g="git"
alias gs="git status"
alias gc="git commit"
alias gcmsg="git commit -m"
alias gcfix="git commit --fixup"
alias gria="git rebase -i --autosquash"
alias gca="git commit --amend"
alias gco="git checkout"
alias gb="git branch"
alias gpr="git pull --rebase origin dev"
alias gp="git pull"
alias ggp="git push origin $(current_branch)"
Enter fullscreen mode Exit fullscreen mode

Thank you for reading

I hope you've enjoyed this article! Please click on the "Follow" button to see my future articles.

I'll glad to see any feedback!

Top comments (2)

Collapse
 
vishalraj82 profile image
Vishal Raj

@paulcodes Wouldn't it be better to create git aliases instead of bash aliases? My personal choice is to have git aliases.

Collapse
 
paulcodes profile image
Paul Golubkov Author • Edited on

Hi! Good question! Yes, you can use git aliases, it's not a bad choice. I prefer use bash aliases instead because I also have other type of aliases, so I like to have one place for all of them. And how you can see in the article I use bash script for getting current_branch name and use it in my aliases, I think it's not posible in git aliases.

And bash aliases are shorter :)

🌙 Dark Mode?!

Turn it on in Settings