In this short and simple post I will talk about how to optimize your git commits.

What does it mean?

As developers, we often come across the need to using git commands to commit and push our code. There is a way to optimize that process so it becomes a standard across all your repositories and code bases where you commit to, and also has fewer commands to write.

Just follow the simple steps below.

Setup

Just open your ~/.bashrc file on your favorite text editor, mine is sublime text as it is quick to open and can be managed through the command line as well.

$ subl ~/.bashrc

Go to the bottom of your file, and paste the following:

# base method to actually do the add, commit and push
gcap() {
    git add . && git commit -m "$*" && git push
}

# commit when you initialize a repository
ginit() {
    gcap "πŸŽ‰ init: [email protected]"
}

# commit when you add or improve something
gadd() {
    gcap "πŸ› οΈ add: [email protected]"
}

# commit when you create a new feature
gnew() {
    gcap "🌟 new: [email protected]"
}

# commit when you fix a bug
gfix() {
    gcap "πŸ› bugfix: [email protected]"
}

# commit when you are ready to create a release
grelease() {
    gcap "πŸš€ release: [email protected]"
}

# commit when you improve the documentation or add comments
gdoc() {
    gcap "πŸ“š doc: [email protected]"
}

# commit when you want to test or debug something
gtest() {
    gcap "πŸ§ͺ debug: [email protected]"
}

# commit when you have work in progress but want to save your state
gwip() {
    gcap "🚧 wip: [email protected]"
}

# commit when you introduce some breaking change
gbreak() {
    gcap "πŸ’₯ breaking: [email protected]"
}

# helper function to list the available commands
gtype() {
echo "
    ginit β€” πŸŽ‰ init
    gadd β€” πŸ› οΈ  add
    gnew β€” 🌟 new
    gfix β€” πŸ› bugfix
    grelease β€” πŸš€ release
    gdoc β€” πŸ“š doc
    gtest β€” πŸ§ͺ️ debug
    gbreak β€” πŸ’₯ breaking
    gwip β€” 🚧 wip
"
}

After saving the file, either restart your terminal, or type:

$ source ~/.bashrc

image.png

Usage

In your git repository just type the relevant command, for example:

$ gfix "Issue regarding integration"

and it will create a commit and try to push to your current branch.

Note: If you haven’t yet pushed the origin, the commit will still be created but it won’t be pushed, so make sure to push the origin after the commit.

$ git push -u origin <branch-name>

You are of course free to change the function name as you please if some other abbreviation makes more sense to you.

Thank you for reading. Hope you found the post helpful.

This post is inspired by Emoji-Log