Effective & Unconventional Git
In this article I will show a few effective and unconventional git tips you may not have thought of. Mastering our craft requires learning our tools as if it’s an extension of your own hand or in our case, our mind.
Git is not just a tool to share your code on Github or bitbucket. Git allows us to treat our code as a sandbox or a sketch pad. Like an artist, we can sketch out the solution and fill in the details as we go. Our commits don’t have to be perfect the moment we invoke “git commit”.
Git can allow us to reach a state of flow and boost our productivity & confidence in our code.
Here are some goals to keep in mind. After reading this you should be able to:
- Improve your debugging skills
- Quickly switch between different solutions or ideas
- Increase your development speed
Let’s play detective
You’re digging through your code trying to understand why a certain bug happens. Ever wonder how can I find out who wrote this chunk of code or get some context as to why it was written this way? We can use a wonderful command known as “git blame”
This command will show you the exact commit hash with a timestamp for a line of code and who wrote it. Now before you go “blaming” the author or yourself, with this in hand we can then browse through the adjacent commit history to find better clues.
Maybe the change was valid back when it was written but now it’s not fitting your current use case.
Maybe it’s an edge case that slipped through QA.
Regardless of the reason, we have the evidence to either find the true root cause or start refactoring some of that crufty legacy code.
Failure is not an option
Ever find yourself stuck staring at your IDE, frozen in place and unable to start? With Git once we figure out how we’ll start, we can use our awesome version control powers to write code like Jackson Pollock or create abstract masterpieces like Picasso.
Jackson Pollock Method
With solution in mind, you begin to write your next feature or bug fix. Your flow may look like this:
- Create view/Home.vue
- “Git commit -am “wip”
- Update home template
- “Git commit -am “wip”
- Add methods and data bindings
- “Git commit -am “wip”
- Update the template html
- “Git commit -am “wip”
- Add css
- “Git commit -am “wip”
A contrived example, but you get the idea.
I use this especially in cases where you must construct several inter-related components at the same time.
Now you end up with a long string of “wip” commits but in reality you have a near finished feature.
How do we clean this up?
Let’s squash them into one and amend the commit message as if we had done this all in one commit.
No need to push each wip commit to Github or Bitbucket. You’re in your sandbox building your sand castle.
Some may argue that commits should focus on a single component but sometimes you need those supporting components like a basic data layer to finish your new feature. All depends on your style. This isn’t as important as writing quality code and getting things done. Git allows us the power to know when any offending changes occured.
I personally do this a lot as I’m the type to iterate on each small chunk. I’m a little paranoid and almost don’t trust code unless it’s committed even if it eventually gets squashed. I use this often enough that I have a bash alias setup to quickly do this and all I need to type is “gitwip” and continue building.
This method also allows me to see the code diffs between each “WIP” as I’m building to see what I may be missing during my flurry of changes. Once I squash them down I can then stand back and review the changes and remove any extraneous debug code before pushing it up for the world or my team to see.
While this may not seem efficient, it helps to not have to think about using the right git command in between changes on my feature branch.
Game Over – try again or restart?
Some developers seek perfection while others just want it to just work. As you expand your skills, your ability to reach higher levels of “perfection” becomes easier. With git you can have the confidence to experiment and let go your need for immediate perfection. Our tools should not hinder us and should lubricate our minds for the code to flow from our fingertips.
Like any artist will tell you, allow yourself to make mistakes, the art is in what you keep.
Continue on your journey and find some fun ways to learn programming!