Losing code that you have spent hours writing can be painful, which is why we use version control (or source control) to store our code and manage changes.
Version control is even more important if you are working in a team, without it code, changes can get messy quickly.
Version control has been around for a while in various forms but it seems most of the development community has settled on Git as the defacto way of managing code changes.
Git has been around since 2005 and has become the predominant version control used amongst developers.
Git works on branches. You have your working deployable code in the
main branch (sometimes called master branch) and then you can branch off copies of the code to allow you to make changes.
This allows multiple developers to work on the same codebase without treading on each other’s toes.
All the work you are doing stays on this separate branch until you are happy with the changes, it has been reviewed if you are working in a team, it has been tested and then it can be merged back into the main branch.
There are a lot of different commands for git but the mains ones are:
git clone https://….git used for cloning a repository onto your local machine
git branch <branch> used to create a new branch on your local machine
git checkout <branch> used to switch to the new branch.
git add <file> add a file to the staging area ready to commit.
git commit -m “commit comment goes here" commit your changes to the branch.
git push -u origin <branch> used to push your changes to the remote server.
git pull used to pull down other people’s changes from that branch.
When you are working with other people on an application you need a shared location to store all the code.
A lot of developers now use GitHub to host their code. Hosting code on GitHub is completely free for both public and private repositories.
It is also a great resource for developers. If you have spent any time in software development I am sure you are already familiar with GitHub already.
When working in a team there are 2 main branching strategies that are used, Git Flow and GitHub Flow.
Git Flow is the most commonly used branching strategy. Git Flow is particularly useful when your development cycle resolves around releases.
If you work using Scrum and expect to do a single release at the end of the sprint then you will want to use Git Flow. Also if you rely on QA to do manual testing of your code before it goes to production, then this could be another reason why you might want to use Git Flow.
Git Flow works around several branches that need to be set up.
main branch with all additional changes that have been added since the last release.
develop and create feature branches for any new features they are working on. There isn’t a single branch called feature unlike
develop. Branch names will generally be named based on the change that is being made
bug/StackOverflowFixInService. If you are using JIRA and have the GitHub or Bitbucket integration set up then you might see the JIRA issue number in the branch as well so that the branch gets shown on the JIRA ticket
develop to create the release. The branch will generally be named after the release number
hotfix branch is created. This is branched directly off of
main and will usually be named after the incident
When using GitFlow this is the general flow of how the branches are used:
main branch is created first and left empty, maybe apart from a
develop branch is immediately branched off of
main. No changes are ever made directly to
develop unless you are a cowboy.
develop into a feature branch to make their changes. Changes from
develop will occasionally need to be merged into the
feature branch depending on how long it takes to develop the feature.
develop. Any conflicting changes that have happened on
develop will need to be resolved before the feature can be merged.
develop. Normally a QA team will run their tests on this release before it is ready for production. In the meantime, the developers can continue working on the
develop branch on features for the next release.
main branch after the release is merged. If you release from
main there is a risk that what you are releasing is different from what has been tested by QA. Although, this shouldn’t happen if you follow the process correctly. Either way, the release should be tagged with the release number so you know exactly what has been released and when.
main branch will be merged into
develop so any changes made during testing are accounted for.
hotfix branch will usually be created off of the
main branch in order to apply the fix.
develop or by branching again off of a feature branch.
GitHub flow, as the name suggests is the branching strategy used by GitHub. You don’t have to be using GitHub though in order to use this branching strategy.
With the GitHub flow, you only ever have 2 branches:
main branch contains all the deployable code for the project.
main to work on new features.
For GitHub flow, the general process is as follows:
main branch is created at the start of a new project.
main into a
feature branch and the code is reviewed before being merged to
main it should be released to production immediately.
Unlike Git Flow there is no concept of a
release branch. Every feature makes up its own release and therefore everything merged should be in a deployable state.
Similarly, with GitHub Flow, hotfixes are treated the same as feature branches as they branch off of
Generally, teams using Git Flow have a large number of automated tests that run against each feature before it is merged in.
Handling features and releases in this way allow teams to release multiple times a day instead of at end of a sprint.
In order to be able to release quickly you need to have a solid automated test suite and hands-off automated deployments.
main will go straight into production.
Git branching strategies are a matter of preference. If you don’t have a solid automation suite and completely hands-off automated deployments then GitHub Flow isn’t going to work for you.
Git Flow is generally the preferred branching strategy for companies that need a lot of quality control in place before features are released to production.
For example, financial institutions, where large amounts of money are at risk if a bug is released, are unlikely to use GitHub Flow. Although all teams can benefit from solid automated testing and automated deployments.
GitHub flow is great if you want to move fast and get things into production quickly.
It reminds me of Mark Zuckerberg’s old motto “Move fast and break things”. If breaking things isn’t going to cost you millions of dollars in damages then GitHub flow could be a simpler approach for your team.
If you found this article useful please let me know and feel free to share it with your friends.