7 Tips for Mastering Git

Every developer today should be using a revision control system. Git is the most popular and it is not difficult to get started.

Jacob Beningo

January 18, 2021

7 Min Read
feature.jpg
Adobe Stock

One thing that every embedded software developer, and hopefully every general software developer has in common is that they use a revision control system to manage their software. There are different revision control systems out there, but the most popular system today is Git. If you have never used a revision control system or are just getting started using Git, here are a few tips and tricks for mastering Git that will help get you up to speed quickly.

240_F_321266283_9dPFvGoU8niQemU51akfgYBmsVpT8nH7.jpg

Tip #1 – Start Using Git from the Command Line

When I first started to use Git, I started out using a graphical user interface (GUI) that completely abstracted out the details of Git and what was happening behind the scenes. This was certainly convenient, but the problem was that I had no clue what was happening behind the scenes. The lack of understanding about these details created gaps in my knowledge which made managing the repository more difficult.

When getting started, the best thing to do, even though it will be slower at first, is to use Git through the command line. This forces a developer to fully understand Git, how it works, what the commands are, and get the behind the scenes knowledge. Only then, does it really make sense for a developer to abstract those details and move to a GUI.

Related:Embedded Software Trends Expand in 2020

Tip #2 – Practice with a Test Project

Developers can learn Git on the fly on a project, but the best way to learn is to take some time and create a test project. A test project provides several advantages to the development team such as:

  • Experimenting with branching

  • Experience merging branches

  • Working in a repository with multiple developers

  • Integrating continuous integration and testing

  • Figuring out the workflow and how to fix inevitable issues

A test project doesn’t have to be complicated. It can be nothing more than a few text files that developers paste random text into. Developers can play with their project organization, how to ignore specific file types such as object files, and even create submodules.

Tip #3 – Use Submodules

A Git submodule is basically another Git repository that is included as part of another repository. For example, I was recently working on a project that used a Microchip Harmony library for all the low-level drivers and some middleware support. Rather than create a single project repository, I created a repository to store the Microchip Harmony library and then another repository for my application code. The Microchip Harmony library was included in the application repository as a submodule.

The submodule in this case also has a few additional advantages. First, I can use the submodule across multiple repositories. If I’m working on several projects that use Microchip Harmony, I don’t have to keep separate copies of the libraries. I just have the Microchip Harmony repository that I add as a submodule to my application repositories. Second, when I check-out my application and make changes, by default the submodule will not be included without additional commands or command options. This is useful because if I had checked everything into a single repository, it would take git several minutes to search my project for changes due to the number of files that are contained with the Microchip Harmony library.

Related:Developing an embedded software build pipeline

Tip #4 – Leverage Git GUI’s to Simplify Software Management

I’ve always found working from a terminal to be a bit tedious. Terminals do provide an extra level of control that many developers may want to use, but once you understand what Git is doing behind the scenes and its capabilities, it is often usual and faster to abstract those details out and use a GUI tool to interact with the repository. There are many GUI tools out there that developers can use. The two that I use the most are SourceTree and TortoiseGit. SourceTree is a tool that is provided by Atlassian for free that works with Mac and Windows machines. TortoiseGit is for Windows and integrates directory into Windows Explorer which can be pretty convenient.

Tip #5 – Develop and/or Adopt a Standard Git Flow

The nice thing about getting started with Git is that there are quite a few workflows that have already been developed and used by developers for years. When getting started, you can try to figure out what works for you by trial and error, or you can use a standard Git flow. There are several different workflows that have become quite popular such as:

  • Gitflow Workflow

  • Forking Workflow

  • Feature Branch Workflow

I personally typically use a Feature Branch Workflow. A developer in this case creates a branch from the mainline that is used to develop a specific application feature such as an ADC driver, button debounce feature, etc. Other developers working on the project also create branches for themselves to work from while they develop their features. Once the feature is complete, it is merged into the mainline. This has several important advantages such as:

  • The mainline never contains experimental or broken code

  • The mainline can be used by a continuous integration server

  • Each developer works from their own branch, minimizing impact from work being performed by other developers

You can learn more about different GitFlows from the official Atlassian tutorial at https://www.atlassian.com/git/tutorials/comparing-workflows.

Tip #6 – Commit Code with Useful Comments

When committing code to a repository, developers should include useful comments that make it easy for them to go back through the repository and understand what is in that version of code. When I create a branch, I name the branch something useful like FEATURE_ADC. When I commit that code or merge the result with the mainline, there are several pieces of information I make sure that I include in my comments log:

  • The version number for the software and/or feature

  • What changes were made

  • Any known issues with that version

  • General comments

I will often include a section for each of my comments and fill in each section. For example, in the changes section, I will often comment on what I added, removed, and updated. In the issues section, I may include notes that I need to add error handling or that testing only covered 50%, or any note that lets me know what additional work I need to do. I may also include any new bugs discovered that I did not fix in that commit. Finally, I may leave general comments about the code such as the need to update documentation, an idea for an improvement, and so forth.

Comments can be extremely useful to identify what is in that version, but they can also be useful to review after lunch to remember where you left off and what needs to be tackled next in the software.

Tip #7 – Identify a Good Reference Manual

The last tip for today is that every developer needs a good Git reference. For the most part, it’s easy to just perform a search in your favorite search engine to figure out what the command was to initialize a submodule or to stash your current changes or rebase to the current mainline. For whatever reason, I also still like to keep a physical reference around in book form sometimes. I’ve found that while I can find something on the internet pretty fast, a good book works just as well and gives my eyes a break from staring at a screen. For Git, I’ve found that Jon Loeliger and Matthew McCullough’s “Version Control with Git” to be a good reference and also a good book for beginners to glean some in-depth understanding of how to set up and use their Git repositories. I suspect most developers just search the web as needed but since Git performs such an important task for developers, we really should all be digging deeper.  

Conclusions

Every developer today should be using a revision control system. Git is the most popular and it certainly is not difficult to get started. Some of the nuances can certainly be challenging for newbies. In today’s post, we’ve explored a few simple tips for getting started that should help the reader get up to speed quickly.

Jacob Beningo is an embedded software consultant who currently works with clients in more than a dozen countries to dramatically transform their businesses by improving product quality, cost, and time to market. He has published more than 200 articles on embedded software development techniques, is a sought-after speaker and technical trainer, and holds three degrees which include a Master of Engineering from the University of Michigan. Feel free to contact him at [email protected], at his website www.beningo.com, and sign-up for his monthly Embedded Bytes Newsletter.

About the Author

Jacob Beningo

Jacob Beningo is an embedded software consultant who currently works with clients in more than a dozen countries to dramatically transform their businesses by improving product quality, cost and time to market. He has published more than 300 articles on embedded software development techniques, has published several books, is a sought-after speaker and technical trainer and holds three degrees which include a Masters of Engineering from the University of Michigan.

Sign up for Design News newsletters

You May Also Like