Improve Your Git Productivity with Bash

Dale Nguyen - Oct 16 '19 - - Dev Community

There are a few methods that you can work with Git. You can either use some user-friendly software like SourceTree to manage your workflow and repository. It has a beautiful Git GUI for you to “click” around. However, if you are a guy like me, who just wants to use a command line, then you are in the right place.

Imagine that when you finish your work, then you want to add all the changes, write a commit message and push to the branch that you are working all.

git status
git add .
git commit -m "Some random messages" 
git push origin dev
Enter fullscreen mode Exit fullscreen mode

This is a long and repetitive process to me. It may cost a few seconds to a minute to do all of this. How much time did I lose in a week or a year — I’m being dramatic here :D

So I decided to find a method to improve this process and thanks to bash, there is a way to combine these commands together.

From now, every time I finish a task, this is the only command that I use:

gf "Some random messages"
Enter fullscreen mode Exit fullscreen mode

Looks how much time, did I save over the year :D

I will show you how I did it by taking advantage of bash. I’m using Mac, but you can still do it with on a Linux machine.

In your home directory, edit or create the .bash_profile file. You can use any programs to edit it. I’m using Vi. If you don't use .bash_profile as your default bash, replace it by your own such as .zshrc

vi /Users/dnguyen/.bash_profile
Enter fullscreen mode Exit fullscreen mode

Then we start to add our shortcuts:

# Github
alias gs="git status"
alias gd="git diff"
alias gp="git pull"

# Git finish will push to current branch
# Eg. gf "commit message"
gf() {
 CURRENT_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
 git add . && git commit -m "$1" && git push origin "$CURRENT_BRANCH"
}

# Git merge
# Eg. gm branch-name
gm() {
 git merge "$1"
}

# Git checkout
# Eg. gc branch-name
gc(){
 git checkout "$1" && gp
}
Enter fullscreen mode Exit fullscreen mode

After that, we need to load any functions file into the current shell script.

source .bash_profile
Enter fullscreen mode Exit fullscreen mode

Now, enjoy the hack. Hope this help ;)

. . . . . . . . . . . . . . . .