Why use Git?
For those unfamiliar, Git is a version control tool, not only is it used to control versions but also to collaboratively contribute to open source projects and collaborate within organisations.
Git is an important tool as it allows developers to keep track of the changes made within their code, work together and keep different versions of their code. It’s important to use Git as it enables you as a developer to be able to roll back and revert changes to any stage to avoid those endless bugs you accidentally embedded deep within that 1000-line script that has taken you forever to write. It is also important to use Git so that you will always be able to roll back to the past versions of that 100-line website you wrote in case of a bug or security breach or anything else that would cause you to want to revert to a past version. You get the gist - pun intended - here, Git allows you to work more flexibly with more options for collaboration, etc.
You're not making commits..?
One issue I see regularly, too regularly I would say, is people putting their project on a platform using Git, such as GitHub, and then after that, they don’t commit any of the changes they make. It makes the code confusing. Why does it appear differently in the live version on my localhost? Well, it would seem you’ve made 100 changes since you put it on GitHub and then never committed another change!
Make sure if you’re using Git (which 99% of the time, why wouldn’t you be?) you keep your commits regular when you make changes, this avoids confusion, allows you to manage versions, track issues, and retrieve genuine bug reports, plus it only takes a few seconds to write a commit.
Stop pushing all your changes in one commit.
Made 5 different changes in your code? Make 5 different commits.
All too often I see developers making the same mistake, whether it be in open-source projects or private repos I’m collaborating on; developers are always putting as many changes as possible into one Git commit.
Putting each change in a separate commit makes your changes clear, makes them accessible, and makes it easy to revert in case you make a big error!
For those of you looking to brighten things up… you’ll be making more commits, which means more green squares. 🟩🟩🟩
Stop writing non-explanative commits!
Ok, every developer to ever use Git is guilty of this one, sure. But that doesn’t mean we should just settle for non-explanative commits.
Commits with non-explanative messages attached to them make them extremely difficult to identify amongst the hundreds of commits we make to every project as developers. Making a commit with a message like ‘made changes’ is unclear and difficult to understand which changes were made; commits with messages like ‘give input elements in login form margin-block 5em’ are clear and easy to identify in the crowd.
Do keep in mind, however, commits can only have 50 chars in their title message.
Don't commit secrets...
It’s always a dangerous game, playing around with secrets near Git, especially with an open-source project - it tends to tell everyone, shhh.
Commits exist forever, they don’t go away! Make sure to use .gitignore to ignore any private files and try to use .env or equivalents for secrets and private tokens!
If you do commit a token, reset it as soon as you can, you could be providing access to some important account-related data, or even your users’ information. Keep that safe.
Stop committing direct to master.
Sure, committing to the master branch is easy, but the problem with this is that you make immediate changes to the main branch that all forks are created from, commit a bug and everyone’s syncing it! Use separate branches for each new feature to keep them in a controlled environment where bugs won't later be reflected throughout pull requests!
Write better commits.
Hopefully, this helps you to write better commits. High-quality commits are essential in high-quality projects so make your commits and your projects count.
You can find me on GitHub at @immattdavison!