This is the seventeenth part of my journey to build and publish a Flutter based app for the App Store and Google Play.
Eventually you'll need to think about the fundamentals of source code control.
TL:DR – This article focuses on Git, a popular and powerful version control system
Contents
- Setting up source code control using Git for Flutter 💙 development
- Initialising a New Git Repository
- Git
- Wait, what is Source Code Control?
- Key Benefits for Developers
- Overview of Git
- Git as a Popular Version Control System
- Core Features and Functionality of Git
- Comparing Git to Other Version Control Systems
- Setting Up Git on a Mac
- Installing Git on macOS
- Verifying the Installation
- Configuring Git for the First Time
- Setting Up Your Git Identity
- Configuring Default Editor to be BBEdit and Other Preferences
- Creating and Managing Repositories
- Creating a New Git Repository
- Cloning an Existing Repository
- Working with Git on macOS
- Basic Git Commands for Daily Use
- git status, git add, git commit, git log
- Understanding Branches and Tags
- Advanced Git Techniques
- Rebasing vs. Merging: When and How
- Stashing and Cherry-Picking
- Collaborating with Git
- Pushing and Pulling Changes
- Resolving Merge Conflicts
- Transitioning from Mac to Chromebook
- Challenges of Using Git on Chromebook
- Differences in Operating Systems and Interfaces
- Limitations and Workarounds
- Setting Up a Git Environment on a Chromebook
- Enabling Linux Development Environment
- Installing and Configuring Git on Chrome OS
- Alternative Tools and Extensions
- Git Clients for Chrome OS
- Web-Based Git Management Tools
- Best Practices for Cross-Platform Git Usage
- Maintaining a Consistent Workflow
- Synchronizing Configurations and Settings
- Handling Line Endings and File Permissions
- Utilizing GitHub and Other Git Services
- Leveraging Online Platforms for Cross-Device Access
- Setting Up Continuous Integration and Deployment
- Security Considerations
- Protecting Your Git Repositories
- Using SSH Keys and Two-Factor Authentication
- Troubleshooting Common Issues
- Common Problems on macOS and Chromebook
- Authentication Errors
- File Permission Issues
- Debugging Tips and Tools
- Using Git's Built-In Diagnostic Commands
- External Tools and Resources for Troubleshooting
- Seeking Help and Community Support
- Online Forums, Communities, and Documentation
- Engaging with the Git Community
- Conclusion and Next Steps
- Recap of Key Points
- Future Learning and Resources
Setting up source code control using Git for Flutter 💙 development
Ive been working on this project on my Mac, and now need more computers so its time to manage the project properly using source code control so that I can keep my work in sync and under control. Distributed version-control systems have existed for decades, but Git is winning and used by most of the tools I am using so its the default choice. Theres a bit of a learning curve but there are also graphical tools and helpers for existing editors. So lets get started.
My case is pretty simple. I have three development machines, a Mac, a Chromebook, and a ThinkPad running Ubuntu Linux and want to keep the Flutter source code on all of them and in sync and runnable via Android Studio or the command line. Git distributed version-control is capable of far more, and of supporting teams, bit but it handles this task easily.
To set it up was just a case of going to my git service provider and setting up my user account and ssh certificates for my laptop and my mac. There are many git repo providers, you dont have to use GitHub - I often use Assembla for development management and it provides private git repositories and is phenomenally reliable. YMMV. Once set up run the following commands to instantiate the repository and push it up to the server.
Initialising a New Git Repository
Creating a new Git repository is the first step in versioning your project and is as simple as:
git init
git add -A
git commit -m "First commit"
git remote add origin your.reponame.git
git push -u origin master
But there are some steps to take before these commands will work, so lets just go over them.
Git
Git is a distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among developers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows. Git From Wikipedia, the free encyclopedia
Wait, what is Source Code Control?

Source code control, sometime referred to as version control, is a system designed to manage and track changes in codebases over time. It is a critical aspect of software development, enabling teams to collaborate, maintain historical versions, and ensure the integrity of their work. By capturing every modification made to a codebase, source code control provides a chronological history, allowing developers to revisit and review specific changes, revert to previous versions, and identify the origin of bugs.
The importance of source code control extends beyond versioning. It is instrumental in facilitating collaboration, particularly in distributed projects where multiple developers may work on the same files simultaneously on different operating systems and changes are managed in automated systems which themselves use the version control software to document their work, performing tests, or code analysis. Moreover, it acts as a safety net, preserving the project's history and protecting against accidental data loss or code corruption. In essence, source code control systems are indispensable for maintaining the stability and continuity of software development projects.
Key Benefits for Developers
For developers, the advantages of using a source code control system are manifold. First and foremost, it provides a structured approach to managing code changes, preventing issues such as code conflicts and ensuring that all team members are working with the latest version of the codebase. This is particularly beneficial in agile development environments where rapid iterations and continuous integration are the norm.
Another key benefit is the ability to experiment with new features or fixes in isolated environments, known as branches, without affecting the main codebase. This flexibility allows developers to test and refine their changes before merging them into the production environment. Additionally, source code control systems facilitate peer review processes, enabling teams to critique and improve code quality collectively.
Overview of Git
Git as a Popular Version Control System
Git is one of the most widely used version control systems today. Its popularity stems from its distributed nature, which allows every developer to have a full-fledged repository, complete with the entire history of changes. This decentralisation not only improves the system's resilience but also enhances its speed and efficiency. Developers can work offline, making commits locally and later synchronising their changes with a central repository.
The distributed model of Git also enables better collaboration, as it simplifies the process of sharing changes and contributions. Each developer can work independently, merge their changes with others, and resolve conflicts efficiently. Git's robust branching and merging capabilities further contribute to its popularity, providing a flexible and powerful framework for managing complex projects.
Core Features and Functionality of Git
Git's core features are designed to cater to the diverse needs of software development teams. Among these, branching and merging stand out as critical functionalities to learn how to use. Branching allows developers to diverge from the main codebase to work on new features, bug fixes, or experiments. Once these changes are tested and deemed stable, they can be merged back into the main branch, often historically referred to as 'master' but now more properly as 'main'.
Another fundamental feature to master is Git's commit mechanism. Each commit captures a snapshot of the repository at a given point in time, complete with a message that describes the changes made. This granular tracking enables developers to trace the evolution of a project, understand the rationale behind changes, and quickly identify when and where a particular issue was introduced.
Comparing Git to Other Version Control Systems
While Git is the dominant player in the version control arena, it is not the only option available. Systems like Subversion (SVN) and Mercurial also offer version control capabilities but differ in their architectures and workflows. Unlike Git, SVN uses a centralised model, where a single central repository serves as the source of truth. This can simplify some workflows but also introduces a single point of failure and requires a constant connection to the server.
Mercurial, similar to Git, is a distributed version control system. However, it is often praised for its user-friendly interface and straightforward commands, which can make it easier for newcomers to learn. Despite these differences, Git's comprehensive feature set, extensive community support, and integration with popular platforms like GitHub and GitLab have solidified its status as the preferred choice for modern development teams.
Setting Up Git on a Mac
Installing Git on macOS
Installing Git on macOS is straightforward. The most common method is using Homebrew, a package manager that simplifies the installation of software on macOS. To install Git using Homebrew, you simply open the Terminal and run the command brew install git. This command fetches and installs the latest version of Git, ensuring that you have access to the most recent features and security updates.
Verifying the Installation
After installation, it is essential to verify that Git has been correctly installed. This can be done by running the command git --version in the Terminal. This command will display the installed Git version, confirming that the installation was successful. Additionally, it is advisable to check the installation path by running which git to be sure that the system is using the correct version of Git.
Configuring Git for the First Time
Setting Up Your Git Identity
The initial configuration of Git involves setting up your identity. This is done by configuring your username and email address, which will be associated with all your commits. This is crucial for tracking contributions and maintaining accountability within a project. You can set these details using the following command git config --global user.email "
Configuring Default Editor to be BBEdit and Other Preferences
Apart from setting your identity, it is also beneficial to configure other preferences such as the default text editor for commit messages. For instance, to set BBEdit as the default editor, you can use the command git config --global core.editor "bbedit --wait --resume". This configuration enhances your user experience by allowing you to use familiar tools for managing commit messages and resolving merge conflicts.
Creating and Managing Repositories
At the top of this article you can see the steps to create a repository ready for use on multiple computers from your Git provider. It uses basic Git commands to achieve this.
Creating a New Git Repository
Creating a new Git repository is the first step in versioning your project. This is done by navigating to the project's directory and running the command git init. This command initializes a new repository, creating a .git directory that will contain all the version control data. Once initialized, you can start tracking changes to your files by adding them to the staging area with git add and committing them with git commit.
Cloning an Existing Repository
Cloning an existing repository allows you to create a local copy of a project, complete with its history and branches. This is particularly useful for collaborating on open-source projects or working on distributed teams. To clone a repository, you use the command git clone <repository-url>. This command copies all the files and history from the specified repository to your local machine, enabling you to work on the project offline.
Working with Git on macOS
Basic Git Commands for Daily Use
Git provides a set of basic commands that are essential for daily use. These include git status, which displays the current state of the repository, showing any changes that have been staged, modified, or are untracked. The git add command is used to stage changes, while git commit records those changes with a descriptive message. The git log command allows you to view the commit history, providing insights into the project's evolution.
git status, git add, git commit, git log
Understanding these basic commands is fundamental for efficient Git usage. The git status command is often the first step in the workflow, helping you understand what changes have been made. Following this, git add stages specific changes, preparing them for a commit. The git commit command then saves these changes to the repository, with a commit message that describes what was changed and why. The git log command is invaluable for reviewing past commits and understanding the project's development history.
Understanding Branches and Tags

Branches in Git are separate lines of development, allowing teams to work on different features or fixes simultaneously. The git branch command lists all the branches in a repository, while git checkout allows you to switch between them. This branching model is powerful, enabling isolated development environments where changes can be made and tested without affecting the main codebase.
Tags, on the other hand, are used to mark specific points in the repository's history, often for releases or significant milestones. Unlike branches, tags are immutable, serving as fixed references to a particular commit. The git tag command can create and list tags, making it easy to identify and return to important points in the project's history.
Advanced Git Techniques
Rebasing vs. Merging: When and How
Rebasing and merging are two techniques for integrating changes from different branches. Merging combines the changes from one branch into another, preserving the commit history but potentially resulting in a more complex history with multiple branches. Rebasing, however, rewrites the commit history by placing your changes on top of another branch's commits, resulting in a linear and cleaner history.
While merging is generally safer and more straightforward, rebasing can be advantageous for maintaining a tidy history. It is important to note, however, that rebasing should be done with caution, especially when collaborating with others, as it rewrites the commit history.
Stashing and Cherry-Picking
Stashing allows you to temporarily save changes that are not yet ready to be committed. This is useful when you need to switch branches or work on something else without committing incomplete work. The git stash command saves your changes, and git stash apply restores them later.
Cherry-picking, on the other hand, involves selecting specific commits from one branch and applying them to another. This is useful when you need to bring in changes from one branch without merging the entire branch. The git cherry-pick command enables this selective integration, providing flexibility in managing changes.
Collaborating with Git
Pushing and Pulling Changes
Collaboration in Git revolves around pushing and pulling changes. The git push command uploads your local commits to a remote repository, making them available to others. Conversely, git pull fetches the latest changes from a remote repository and integrates them into your local branch. These commands are essential for synchronising work across team members and keeping the codebase up-to-date.
Resolving Merge Conflicts
Merge conflicts occur when changes in different branches conflict with each other, preventing Git from automatically merging them. Resolving these conflicts involves manually editing the affected files to combine the changes. The git merge command attempts to merge branches, and if conflicts arise, Git marks the conflicting sections, allowing you to resolve them. Once resolved, the changes can be committed and the merge completed.
Transitioning from Mac to Chromebook
Challenges of Using Git on Chromebook
Transitioning from macOS to Chromebook presents unique challenges, particularly due to the differences in operating systems and interfaces. Chromebooks, running Chrome OS, are primarily designed for web-based tasks and lack the native support and tools available on macOS. This can affect the ease of setting up and using Git, especially for users accustomed to a more traditional desktop environment.
Differences in Operating Systems and Interfaces
The primary difference lies in the operating system's architecture. Chrome OS is a lightweight, browser-centric OS that differs significantly from macOS's Unix-like system. This can impact the availability and installation of software, including Git. Additionally, the user interface and workflows may differ, requiring adjustments in how tasks are performed.
Limitations and Workarounds
One of the main limitations is the lack of native support for Linux-based applications on older Chromebooks. However, all current Chromebooks support enabling Linux and its applications, including Git. While this adds a layer of complexity, it provides a viable solution for using Git on Chrome OS.
Setting Up a Git Environment on a Chromebook

Enabling Linux Development Environment
The article Flutter development on a Chromebook shows how to set up a Chromebook Linux Development environment. By enabling this feature, users can install a Linux distribution alongside Chrome OS, providing a more traditional desktop environment. This setup allows for the installation of development tools, including Git, and all the Flutter tools, using standard Linux package managers.
Installing and Configuring Git on Chrome OS
Once the Linux Development Environment is enabled, installing Git on Chrome OS follows similar steps to other Linux distributions. The command sudo apt-get install git installs Git, and the configuration steps, such as setting up your identity and editor preferences, are identical to those on macOS. This setup enables a full-featured Git environment, allowing for seamless development workflows.
Alternative Tools and Extensions
Git Clients for Chrome OS
In addition to the command-line interface, several Git clients are available for Chrome OS, providing graphical interfaces for managing repositories. These clients offer a more user-friendly experience, making it easier to visualise branches, commits, and changes. Popular options include GitKraken and GitHub Desktop, which offer cross-platform support and integration with online services.
Web-Based Git Management Tools
Web-based Git management tools like GitHub and GitLab provide powerful platforms for managing repositories directly from a browser. These platforms offer comprehensive features, including issue tracking, pull requests, and CI/CD pipelines, making them ideal for Chromebooks. They also provide built-in editors, allowing users to make quick changes without leaving the browser.
Best Practices for Cross-Platform Git Usage
Maintaining a Consistent Workflow
Consistency is key when working across different platforms. Maintaining a consistent workflow ensures that all team members are on the same page, regardless of their operating system. This includes using the same set of tools, following the same branching and commit strategies, and adhering to a unified code style. Consistency not only improves collaboration but also reduces the risk of errors and miscommunication.
Synchronizing Configurations and Settings
To maintain consistency, it is important to synchronise configurations and settings across different platforms. This includes Git configuration settings, such as user identity and default editors, as well as development environment settings like linters and formatters. Using dotfiles and configuration management tools can help automate this process, ensuring that all team members have a consistent setup.
Handling Line Endings and File Permissions
Line endings and file permissions can differ between operating systems, potentially causing issues when collaborating on code. For example, Windows uses different line endings than Unix-based systems like macOS and Linux. Git provides mechanisms to handle these differences, such as the core.autocrlf setting, which automatically converts line endings as needed. Similarly, file permissions should be carefully managed to avoid conflicts and ensure that scripts and executables work correctly across platforms.
Utilizing GitHub and Other Git Services
Leveraging Online Platforms for Cross-Device Access
Online platforms like GitHub, GitLab, Assembla and Bitbucket provide invaluable services for hosting Git repositories and facilitating collaboration. These platforms offer features like pull requests, code reviews, and issue tracking, making it easier to manage projects across devices. They also provide cloud-based repositories, ensuring that your code is always accessible, regardless of your location or device.
Setting Up Continuous Integration and Deployment
Continuous integration and deployment (CI/CD) are essential practices for modern software development. CI/CD pipelines automate the process of testing, building, and deploying code, ensuring that changes are integrated smoothly and efficiently. Platforms like GitHub Actions, GitLab CI, and Jenkins offer robust CI/CD solutions, integrating seamlessly with Git repositories. Setting up these pipelines can greatly enhance productivity and code quality, enabling rapid and reliable releases.
Security Considerations
Protecting Your Git Repositories
Securing your Git repositories is crucial to protecting your code and intellectual property. This includes setting appropriate access controls, using secure communication protocols, and regularly monitoring for vulnerabilities. Private repositories provide an additional layer of security, restricting access to authorised users only.
Using SSH Keys and Two-Factor Authentication
SSH keys and two-factor authentication (2FA) are effective measures for enhancing the security of Git repositories. SSH keys provide a secure method of authenticating to remote servers, while 2FA adds an extra layer of protection by requiring a second factor, such as a phone app or hardware token, to access your account. Implementing these measures significantly reduces the risk of unauthorised access.
Troubleshooting Common Issues
Common Problems on macOS and Chromebook
Authentication Errors
Authentication errors can occur when there are issues with credentials or access permissions. Common causes include incorrect SSH key configurations, expired access tokens, or mismatched usernames and emails. Resolving these errors typically involves verifying and updating your credentials, ensuring that your Git configuration matches your account settings, and checking the permissions of your repositories.
File Permission Issues
File permission issues can arise due to differences in how operating systems handle file permissions. These issues can prevent scripts from running or files from being accessed. On macOS, file permissions can be managed using the chmod command, while on Chrome OS, they may require adjustments in the Linux Development environment. Ensuring that your Git configuration respects file permissions can help prevent these issues.
Debugging Tips and Tools
Using Git's Built-In Diagnostic Commands
Git provides several built-in commands for diagnosing and resolving issues. The git fsck command checks the integrity of the repository, while git bisect helps identify the commit that introduced a bug. The git blame command shows who last modified each line of a file, useful for tracking down problematic changes. These tools are invaluable for debugging and maintaining a healthy repository.
External Tools and Resources for Troubleshooting
In addition to Git's built-in commands, there are numerous external tools and resources available for troubleshooting. Online forums, Stack Overflow, and GitHub issues can provide insights and solutions to common problems. Tools like GitLens for Visual Studio Code offer advanced visualisation and analysis features, making it easier to understand and manage complex repositories.
Seeking Help and Community Support
Online Forums, Communities, and Documentation
The Git community is vast and active, providing a wealth of resources for learning and troubleshooting. Online forums, such as Stack Overflow and Reddit, offer platforms for asking questions and sharing knowledge. Official documentation, available on the Git website, provides comprehensive guides and references for all Git commands and features.
Engaging with the Git Community
Engaging with the Git community can be beneficial for both beginners and experienced developers. Contributing to open-source projects, participating in discussions, and attending Git-related events and workshops can enhance your skills and knowledge. The community is also a great place to stay updated with the latest developments and best practices in version control.
Conclusion and Next Steps
Recap of Key Points
In this article, we explored the fundamentals of source code control, focusing on Git as a popular and powerful version control system. We discussed how to set up Git on macOS and Chrome OS, covered essential and advanced Git commands, and highlighted best practices for cross-platform usage. We also addressed common issues and provided troubleshooting tips, underscoring the importance of security and community engagement.
Future Learning and Resources
Version control is in continuous development, and continuous learning is key to staying proficient. There are numerous resources available for further learning, including books, tutorials, and online courses. Books like "Pro Git" by Scott Chacon and Ben Straub provide in-depth coverage of Git, while online help on Git and version control is also helpful. Staying updated with trends and new tools in the version control landscape will ensure that you continue to develop your skills and knowledge enough to make your software development more robust.
