Shorten your Git daily commands with bash aliases

Working on a daily basis with Git to manage the versioning of our projects source code, I find myself repeating the same commands every day, something like git add . , git commit -m “my message” and git push origin my_branch, quickly became very tedious over time even using git aliases to make things a little bit easier.

Using unix shell scripts made me a bit scared at first, because I already had to learn how to create and code them, but the task is easier than I imagined, a little research on Google allowed me to make commands lighter and above all effective. So for e.g. an alias like gps allowed me to do both a git add, commit and push at the same time.

Below are some bash aliases that I have created that might serve you:

As you can see, there are a list of 6 aliases :

mrgm, mrgp, gco, gps, gdb, gsync

Those are the main aliases that i personnaly use almost everyday, so let’s take a deeper look at every one:

  1. mrgm, mrgp: almost the same thing, used to make a merge with a non fast-forward method with my master or preprod branches depending on which aliase am i using. (mrg is for merge, m for master, p for preprod)
    For e.g.: mrgm super_feature will merge my local master branch directly with the remote super_feature branch after doing a git fetch before, and push (if the merge is correctly done)
  2. gco: create a local branch, push it to the remote and set the upstream to link the local and remote branches.
    For e.g.: gco new_feature will create a local new_feature branch, push it to origin/new_feature, and set the upstream.
  3. gps: the classic add/commit/push commands can be done with this little alias.
    For e.g.: gps branch “Custom style added”, is the same thing as doing the following commands :
    > git add.
    > git commit -m “Custom style added”
    > git push origin branch
  4. gdb: simple alias to delete both local and remote branch (with the same name)
    For e.g.: gdb my_branch will delete my_branch and origin/my_branch
  5. gsync: a very useful alias who make it possible to synchronise two branches. In my case, i have in all projects two main branches named master (for production environment purposes) and preprod (for staging environment purposes), and sometimes i merge some unnecessary branches into preprod branch, so of course i can go back to an old commit to get ride of them but what if i only need to get the latest master branch version and put it in my preprod branch? So gsync in the rescue!

Now that we have seen a description of all my personal aliases, let’s see how can we use them in our terminal.

Really easy, using unix or windows based OS, you can simply open a Terminal for unix users, or Git Bash for windows users, and follow the steps below :

  1. In command line, type : vi ~/.bashrc
  2. Next, you have to paste aliases code (in the bottom of this post)
  3. Save with Esc > :wq > Enter
  4. Reload your bash source with : source ~/.bashrc

And that’s it!

You can now use your bash aliases as described before and get ride of all the tedious daily git commands.

Happy coding!

Aliases source code: