RSS

ZSH Shell Script for quick Git Commit and Push

How to create a quick zsh script
Share this page:

Issue: Repetitive manual git stuff

I’ve been using some Git Repos where my environments are called dev and master. First thing that comes to mind - automation!.

Some times I make a pretty small correction, and I want to push it to master without checking out how a spelling correction will look in dev. Call me a daredevil if you will… Or… you know - loser is fine too…

Solution: Automation

My moto: Everything you do 3 times a month - AUTOMATE. Everything that appears 3+ times in your code - REFACTOR and turn into a function.

How do we automate this?

  • Python? Overkill…
  • Golang? Dude… come on!
  • Shell? Yeah baby!

I’ll assume you’ve switched to ZSH, if not you need to read this.

Also, if you need to quickly learn shell scripting, here’s a cool video:

Luckily, ZSH is same as BASH, so if you have some skills there - you’re good to go!

If you’ve been reading my blog, you know the drill, two options:

  • Create a script in your folder, so that it works just for your project with the environments you decide.
  • Go an extra mile, and create a global executable.

I’ll start with the option one for now. Start with creating a simple .sh

Yes, I’ll use vi. Hater’s gonna hate…

vi ./gitmyshit.sh

Disclaimer: Automating commit to master is some dangerous business, so the responsibility is yours. I show you how, and you decide if you’re that crazy or not.

Since my use case is to basically do the same commit in Dev and Master, I’ll create just one variable, and that will be my commit and merge message.

Press i do insert (DuH!). And copy and modify the script below:

#!/bin/zsh
# make sure you're on dev branch
git checkout dev
# do your shit in dev with the message
git add .
git commit -m "$1"
git push

# switch to master branch and merge
git checkout master
git pull origin master
git merge dev -m "$1"
git push

# go back to dev branch
git checkout dev
pwd

# make sure you're good
git status

Cool, let’s just modify our permissions, and make they’re good:

chmod a+x gitmyshit.sh
➜  matscloud git:(dev) ✗ ls -lrast | grep shit
  8 -rwxr-xr-x    1 mjovanovic  staff    161 May  7 09:36 gitmyshit.sh
➜  matscloud git:(dev) ✗

Yeap, let’s make sure it works:

GitMyShit