DEV Community

Cover image for Improve Your Git Productivity with Bash
Dale Nguyen
Dale Nguyen

Posted on

Improve Your Git Productivity with Bash

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 ;)

Top comments (12)

Collapse
 
alexparra profile image
Alex Parra

I have lots of git aliases... some in git config and others in bash_profile.
The one I find the most useful is for checking diff of commits between two branches.

But regarding your first alias, I’d like to raise attention that you should review what you’re committing and an alias as you’ve described may prevent less experienced developers from reviewing changes before committing.

There’s another problem with aliases... when you have to work on a machine that’s not yours, you won’t know how... unless you configure your aliases.

I’m not against aliases, I have some myself.
Just sharing concerns for others to see and consider.
Best.

Collapse
 
oscherler profile image
Olivier “Ölbaum” Scherler

I always use git add -p when I want to commit. It will go through each chunk of changes and ask if I want to add it or not. It’s a great way to review what you are going to commit, spot and skip some leftover debugging code, and it allows you to make smaller commits at the same time.

You can even edit the patch before adding it, which is daunting at first but can be quite convenient to quickly omit a leftover print statement in the middle of an otherwise valid change.

Collapse
 
dalenguyen profile image
Dale Nguyen

Thanks for your concern. I agree that we should check the differences before committing. This step is done after we check everything. We can do it in the editor like VSCode or through the alias: gs and gd.

Collapse
 
rahul0705 profile image
Rahul Mohandas

Seems to me your looking for git commit -am <message> but I caution, the reason most of these commands are separate is because you should be aware of what your commiting, all to often junior engineers use got as a remote backup of their hard drive and while there is some more truth to that; users should be aware of what they are adding and WHY (that's the point of the message).

Collapse
 
santaxxl profile image
Dominik Mańkowski • Edited

git commit -am <msg> is different from git add . && git commit -m <msg>. The former does not add newly created files to the index.

Collapse
 
jeklah profile image
Jeklah

You can do this in any shell, not just bash.

Collapse
 
taikedz profile image
Tai Kedzierski

I've been maintaining something like this for a while now... I use it every day...! :-D

It adds and commits in one go, warns me of committing on master, shows the log graph easily, manages commit profiles (rudimentary) and plenty more. I might admittedly have gone overboard.

The only catch is that because of the dependency on bash-builder, it only runs properly on Ubuntu/Debian. No guarantees elsewhere until I rewrite bash-builder distro-agnostically...

Collapse
 
dalenguyen profile image
Dale Nguyen

Nice work, Tai! As long as it is useful, somebody will like it too.

Collapse
 
katnel20 profile image
Katie Nelson

Using the gp alias, I'd be confused on whether I was doing a pull or a push.

Collapse
 
dalenguyen profile image
Dale Nguyen

Git push is not that easy compared to Git pull, so gp is only for Git pull. You will remember after using it for few times.

Collapse
 
dalenguyen profile image
Dale Nguyen

Nice, I'm not the only one who hates repetition :D