Regardless of what language you're coding in, you need to use some type of version control for any serious project. Personally, I like using the command line to push my code to a git repository (and if you're going to try to argue that your git GUI client is better- please, the command line is faster to use, lighter on your machine, and just gives you the most control). Although I love using git from the shell, I found myself repeatedly doing the same three commands over and over:
Originally, I was just looking for a way to at least combine the add and commit steps into just one command. I learned that you could add a "-a" flag onto the end of commit, but that's not quite the same as add -A. I even started this reddit thread about the subject, and it was from these answers that "git gg" was born.
Git gg - It's Just A Git Alias
It was the great suggestion of alfunx to use a git alias in the first place. We can create a global git alias with a single command that will be available in all command shells on your machine from the time you run it onward forever and ever, and you can name it whatever you want! I personally like "gg" because it's very short and just gives me a nice feeling reminiscent of defeating and then trolling a tough opponent in an online multiplayer game. In this case you have defeated not another person, but git itself. You have tamed the wild, ferocious, time-sucking beast and boiled it down to a mere 2 letters.
Anyway, here's the command to add the git alias:
In this above command we define and then return a shell function, f, where we then run the three commands: git add -A, git commit -m, and git push. Notice that we can even take the string passed into git gg and use that as our commit message in the git commit command by referencing it with "$@". Pretty dang nifty. Also, shoutout to my friend Christina for giving me the idea to add the git push right there in the function to be run when the commit finishes.
Removing A Git Alias
If for some unthinkable reason you might want to delete this magically fantastic git alias, you can easily do that with the --unset flag:
Just Do It
It's literally just one command to set up this git alias, and trust me it will make your life so much better (assuming you push or want to push to git frequently). This command works on Mac, Linux, and Windows so you have no excuse! Create it, use it, live it. Don't forget to high five me in your mind every time you use this and laugh at how ingeniously efficient you are.
Taking It One Step Further
For different programming languages you will have different build systems, cli tools to run tests, etc., but I highly doubt you could find one that doesn't have some concept of pre-commit git hooks. For Nodejs front-end or backend projects I like to use a library called husky. With this package installed all you need to do is define a script called pre-commit in your package.json file, and have it run any commands you'd like to have passing before the code can be committed. I like to load up this command with an eslint command, unit tests, smoke tests, integration tests... It's just very awesome to only have to type "git gg" and a short message and then watch as your command prompt get flooded with stuff happening all while you, the puppet master, can happily sip your tea or coffee or beer or water. Code on, my friend, and keep pushing code like boss.
The posts on this site are written and maintained by Jim Lynch. About Jim...