What is Git?
Created in 2005 by Linus Torvalds, Git was designed to maintain the development of the Linux Kernel. Git was built upon the pillars of full distribution, speed, simple design, ability to handle large projects, and strong support for non-linear development. If we look at the history of Git, we can see it has become the preferred version control system for many developers around the world.
Each of the below Git tips comes from one of the industry proven Git experts that participated on the 2021 GitKon Git conference panel: “ Git Tips I Wish I Had Known Sooner.” Panelists included Sean Dietrich of Kanopi Studios, PHP consultant Carl Alexander, Rob Richardson of Cyral, Nick Hodges of LinearB, and Meriem Zaid from Enova.
Here’s a Git tip: enable the most powerful capabilities of Git with the help of GitKraken, which includes a GUI and CLI.
Git Tip 1: Using Git Hooks to Publish Code
Try using Git Hooks before you commit a new line of code. Git Hooks allow you to run tests on your local machine and coordinate with other developers on the project to make sure your new code doesn’t “break anything” when you pull it up to your Git remote. Using Git Hooks helps you avoid breaking something and spending valuable time coordinating with other developers trying to identify and fix problems.
Git Tip 2: GrumPHP
If you’re using PHP, try setting up the GrumPHP tool. This composer plugin will register some Git Hooks in your package repository. When somebody commits changes, GrumPHP will run some tests on the committed code. If the tests fail, you won’t be able to commit your changes. When the tool identifies a problem, you’ll be met with a grumpy old man icon on your console, detailing the issue at hand.
Git Tip 3: Using a Git GUI
Regardless of how much experience you have with Git, don’t shy away from using a Git GUI. Keep in mind that not all GUIs are created equal and you’ll need to shop around for one that meets your needs. Using a Git GUI allows you to track your Git commits, view Git branches, and coordinate with your team in a very tangible and visible way.
Git is incredibly powerful, but can be complicated to learn. The visualization offered by GitKraken will help you understand Git faster so you can safely contribute to projects.
Git Tip 4: Don’t Store Assets Directly in Your Repository
Git Tip 5: Micro Commits
When working with a large project, consider taking some time to push smaller commits. This process is known as the “micro-commit strategy”. Not only does this strategy help keep a clean repository, but it also makes it easy to revert a Git commit if necessary.
Git Tip 6: Git Autocorrect
Using the Git autocorrect feature is easy to configure and can help when you’re in a hurry. Typos happen, and the autocorrect feature allows you to either have the system correct the command for you or simply give you hints about the command it assumes you were trying to use.
Not only does the GitKraken CLI offer auto-complete for Git commands, it goes one step further to include auto-suggest, intuitively speeding up your workflow while providing helpful context about your Git actions.
Git Tip 7: Git Aliases
Using Git aliases allow developers to create other commands in Git for a faster coding experience. For example, if you have a command that you find yourself typing out multiple times a day, you can create and name your own Git alias to abbreviate the command. A word of caution: while this can be a major time-saver, using Git aliases can be dangerous because it’s possible to rename or create commands that already exist.
Git Tip 8: Keeping a Clean Git Repo
In order to keep a clean repository, you and your team should identify a repeatable naming and Git branching strategy. It can be easy to throw a random or quippy branch title in, but that can create a jumbled mess as projects grow. Creating a consistent branch naming pattern will ensure a seamless publishing and navigation process for all team members.
Taking the time to write well thought out commit messages can seem like a chore, but can also be a lifesaver when it comes to keeping a clean repository. Taking the time to craft a short summary for your commits will allow you to reap the rewards as you save time in the review and publication process later on.
Delete your feature branches once you are done with them. GitHub is really good about saving those if you need them later on, but deleting them from your project will also help with your overall repository health.
Git Tip 9: Git Rebase
Have you ever found yourself with a bunch of grouped commits on one feature branch, away from your main branch? This is a perfect example of when rebase would be helpful. The goal of Git rebase is to change your project history. Rebasing in Git allows you to take your commits and squish them together, remove them, assign a new parent commit, reorder them, or change the base commit entirely. It’s important to note that your commits will end up with different Git hashes because the Git hash is a combination of the content and the parent hashes, and using rebase will change the parent hash.
Rebase can be an intimidating command to learn because it involves a lot of moving parts and has the potential to mess things up if you aren’t careful, but if you take the time to learn how to use this tool effectively you’ll be able to keep a healthier, more linear repository.
Git After It
The most important thing to recognize about Git is that it’s constantly evolving. Many of these Git tips and tricks came from years of hands-on experience as well as a good amount of trial and error. The tricks that work for one developer may not work for the next, so we highly encourage you to put the above list to the test and find out which ones work for you.
Do you have your own Git tip to share? Post it to social media, tag @GitKraken, and use the hashtag #GitTip – you might just see your tip featured on one of the GitKraken social channels!