header-digital-transformation

A hub for technology and sourcing thought leadership

About us

Best practices for using Git

17 Sep 2018
by Martin Ivanov

Git4_linkedIn

Primarily used for source code management in Linux-related projects, Git has quickly become widely recognized by the programming community and today is considered the most popular tool for version control.

How is Git different compared to SVN and how to use it effectively? In this article, we’ll walk you through the pros and cons of both version control systems and share best practices to help you make the most of using Git.

Why Git?

Git was designed to support various project and team setups by swiftly coordinating the workflow on those projects. Is the tool right for you and your team? Here are some common scenarios where Git can apply:

You are working on a large project. Git can be setup on different operating systems at the same time, so you don’t need to worry about what operating system you are using

You need to collaborate with multiple people. Projects that required collaboration among a great number of people can be executed more effectively with Git because most of the operations are executed on the local machine.

You are a technology newbie. Git is easy to work with for unexperienced people because it incorporates a set of UI tools and you do not need to learn all Git commands. Git i is an open-source system which is used by a large community, there are thousands of articles and books to help you get started with Git.

Git over SVN

There are number of reasons for choosing Git over another version control system, however, it all comes to your team and project set-ups. To help you make a wise choice, below we compare Git to Subversion (SVN) and put together some striking differences.


Git1


Distributed Workflows. Unlike centralized version control systems, the distributed nature of Git enables programmers to be way too flexible in their collaborations. Here are some of the advantages that comes with it:

  • It’s no canonical — there is no main copy of the project, every user stores their own copy locally
  • Common operations (such as commits, viewing history, and reverting changes) are speedy, because there is no need to communicate with a central server.
  • Instead, communication is only necessary when altering among other peers.
  • Every working copy functions effectively as a remote backup of the codebase and its history, providing natural protection of data loss.
  • In distributed version control systems, you can create a number of “central” repositories.
  • Various development models are in hand, such as Commander / Lieutenant model, enabling effective delegation of topical developments in larger projects.
  • Internet connection is not required for most operations.
  • An additional set of “sync” operations are at disposal for committing or receiving changes with remote repositories.

Though just very few, the distributed version control systems also have some disadvantages. Here we have listed the two major ones:

  • Slower cloning of a repository compared to centralized checkout, because branches and revision history are duplicated and saved. This applies if the access speed is low and the project is large.
  • Lack of locking mechanisms that are part of most centralized VCS and are still essential when it comes to binary files that cannot be merged, such as graphic assets.


Git2Branching.
Branches in Git are a main feature that is used by users on daily basis. This is because in Git every developer’s working directory is itself a branch. Even if two developers are working on two different unrelated files simultaneously, you can view these directories as different branches emerging from the same common base revision of the project. In comparison, in SVN they are larger and difficult to manage, so they are rarely used.

Git is very fast to work with mainly because almost all operations are performed locally and there is no network latency involved to perform a diff, view file history, commit changes, merge or switch branches, etc.

Smaller space requirements. Git’s smaller space requirements are based on its repository and working directory sizes which are quite smaller than those in SVN. This is mainly because a SVN working directory always contains two copies of each file — one to be worked on and another in “.svn/” to facilitate operations, such as status, diff and commit. On another hand, a Git working directory requires just one small index file of about 100 bytes of data per tracked file. This can be a major difference in the disk space that is required for projects with numerous files.


SVN over Git

Often overlooked, SVN hides some pros that could make you reconsider your choice of version control system for your next big project. Keep reading for the top advantages that you might want to take into consideration.

Single repository. SVN only supports a single repository and repository location is all a user needs to know to access all materials and branches related to a specific project. Backup is also made simple as there is exactly one location that needs to be backed up.
 

Git3Access control. The single central repository in SVN allows the user to specify read and write access controls in a single location and get them enforced across the entire project.

Binary files. SVN can be used with binary files. In an earlier version of Git some minor changes to binary files, such as adjusting image brightness, could make a big enough difference that Git considers them as a new file, causing split in the content history. In SVN, history for such changes is maintained because the tool tracks changes by files.


Partial checkout/ Bandwidth requirements.
SVN allows you to check out just a subdirectory of a repository which cannot be performed with Git. For a large project, you need to always download the whole repository, even if you only need the current version of some sub-directory. Тhis is especially useful when there is a lack of fast Internet connections. In such scenario, Git can cost more time and money which is arguably mitigated by the small size of Git repositories.

Overall, both VC systems can deliver almost everything a modern developer need. So, before making up your mind about which tool to use, make sure to think about your project purposes and see what different tools have to offer.

Best practices for using Git

As it was already stated, the biggest strength of Git is enabling parallel collaboration by number of people via branch creation. Yet, to be able to use it correctly and save ourselves a headache, it is recommended that we follow some best practices:


Git4Commit regularly
. Git only takes full responsibility for your data when you commit. If you fail to commit and then do something that hasn’t been well-thought out, you can go into trouble. Additionally, having periodic checkpoints can help you understand the reasons behind some errors in the code.

Keep history consistent. When working on a branch and creating N number of commits, it is good to squash the commits into one before merging the branch into its parent. This will provide for a consistent and thus, easily readable history.

Always pull the latest updates. Every time you start working on something new make sure that you are onto the latest version of the repository. That way you will avoid resolving unnecessary merge conflicts.

Git5Rebase your branches periodically. When working on a big feature, rebase your branches regularly to avoid resolving conflicts when performing a merge.

Protect your project. Make sure to merge features only via pull requests in order to protect your master branch

Choose the right developers for the job. Give rights for merging pull requests only to the most experienced developers in the team. The eye that’s trained can spot a problem much more easily.



The more approvals, the better.
Allow merging of pull requests only after two or more developers have approved them. Double-verification can save you a big headache.

Make time tracking easier. When using project tracking software like JIRA, it makes sense to name each branch with the tickets number you work on because it’s easier to track progress.

Final thoughts

Whether Git is the right version control system for your current project or not, its advanced features have gained the tool such popularity that makes it a must-know for every geek nowadays. With the right knowledge, everyone can master Git and once you understand how to use it properly, it can make a great contribution to your future project success.

Martin is a Software Consultant at Accedia. Passionate about taking on challenging projects and developing end-to-end software solutions.

Leave a comment