GIT Branching Strategy
As a professional developer, you’ll probably be working with git branches every day, coding a new feature, refactoring code, or testing an app or game. Branching allows you to work in parallel with other people on your team.
Types of Branches
When creating a game or application, you should divide your work into different branches. The most commonly used branch types are:
- main
- dev
Additional branches include:
- feature
- release
- hotfix
Let’s review the most commonly used commands for managing branches:
:'cerate a new branch'
git branch branch_name
:'switch between branches'
git switch branch_name
:'merge the provided branch with the active one'
git merge branch_name
:'list available branches'
git branch
Workflow
The workflow might look like this for a project with main and development branches:
- create a feature branch
- switch to feature branch
- merge with dev branch
- develop the new feature, add and commit changes for feature branch
- switch to dev branch
- merge the dev branch with feature branch
- push the dev brunch to the remote server
- switch to main branch
- merge the main branch with dev branch
- push the main brunch to the remote server
git branch feat/weapon
git merge dev
:'develop the new feature'
git add .
git commit -m "feat: new weapon"
git switch dev
git merge feat/weapon
git push origin dev
git switch main
git merge dev
git push origin main
Resetting by Branch
If you decide you need to reset your project to a different past state, it’s good practice to test it with branches first. Using the log command find the commit hash code and go to that state as it would be a different branch. It is possible to create a branch with a custom name from an older commit using a special command.
git switch main
git log
git switch hash_code
git switch -b branch_name hash_code
If you are sure to restore the project to its previous state, you can use a hard reset. First, log the commits, copy the hash code of the selected commit, and restore the project.
git revert -hard hash_code
After the reset you need to push the project to the remote server with the ‘force’ parameter.
git push --force origin main
Another important thing when working with Git is proper naming of commits and branches using semantic messages and names, which I will cover in my next article.