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
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
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
Usage
gcmsg 'feat: commit text' // create new commit
gca
This is an alias for
git commit --amend
gcfix
This is an alias for
git commit --fixup
Usage
gcfix commit_hash // create a fixup commit for commit with provided hash
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
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
Usage
gria HEAD~4 // interactively change last 4 commits
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
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/}
}
How to keep aliases
If you just type in terminal command
alias gs="git status"
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
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)"
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)
@paulcodes Wouldn't it be better to create git aliases instead of bash aliases? My personal choice is to have git aliases.
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 :)