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://….gitused 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 pullused 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.
mainbranch with all additional changes that have been added since the last release.
developand 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
developto create the release. The branch will generally be named after the release number
hotfixbranch is created. This is branched directly off of
mainand will usually be named after the incident
When using GitFlow this is the general flow of how the branches are used:
mainbranch is created first and left empty, maybe apart from a
developbranch is immediately branched off of
main. No changes are ever made directly to
developunless you are a cowboy.
developinto a feature branch to make their changes. Changes from
developwill occasionally need to be merged into the
featurebranch depending on how long it takes to develop the feature.
develop. Any conflicting changes that have happened on
developwill 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
developbranch on features for the next release.
mainbranch after the release is merged. If you release from
mainthere 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.
mainbranch will be merged into
developso any changes made during testing are accounted for.
hotfixbranch will usually be created off of the
mainbranch in order to apply the fix.
developor 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:
mainbranch contains all the deployable code for the project.
mainto work on new features.
For GitHub flow, the general process is as follows:
mainbranch is created at the start of a new project.
featurebranch and the code is reviewed before being merged to
mainit 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.
mainwill 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.