Table of contents
No headings in the article.
If you're a developer who uses Git, branching is likely a critical aspect of your workflow. But, are you creating branches correctly? Are you naming them in a way that is consistent and clear? If not, you may be confusing yourself and your colleagues.
To create a new branch in Git, it's best to branch out from the "dev" branch before starting to work on any new feature. This ensures that your code is kept separate from the main branch until it's ready for review. Here's how you can do it:
- First things first, let's make sure that our local repository is in sync with the remote repository.
git fetch
- Next, check out the "dev" branch and pull any changes from the remote branch.
git checkout dev
git pull origin dev
- Finally, create a new branch with a descriptive name that indicates the type of work you'll be doing
git checkout -b <branch-name>
Here comes the concept that suffices the article name, let's name the branch properly.
To do that we need to identify the kind of work we are doing. Considering most scenarios, I follow three kinds of work
feature - Any enhancement to the code or the functionalities of the product
bugfix - Any fixes that were done to existing functionality.
chore - Any enhancement to processes like tech debt, hotfixes, refactoring etc
Let's name the branch based on work type. Here is the so-called syntax I follow to name the branch.
<work-identified>/<JIRA-ID>_<feature-description>
Let's look at some examples of how to name and how to not.
Correct ways to name a branch :
feature/JIR27_add_user_button
bugfix/JIR28_text_overflow_fix
chore/JIR29_adding_extra_checks_to_workflow
Wrong ways to name a branch :
test_cases_added
css_file_updated
feature/JIR27_1
Finally, if you've already committed incorrect names to your branch, let me save you. It is never too late to begin the best practices. You can still rename your existing branches to ensure that they follow consistent and clear naming conventions
Use the following command to rename the branch on which you are currently on
git branch -m <new-branch-name>
Use the following command to rename any branch locally
git branch -m <old-branch-name> <new-branch-name>
This will rename the branch locally. If you have already pushed the old branch to a remote repository, you'll also need to push the renamed branch to the remote repository and delete the old one.
To push the renamed branch to the remote repository and delete the old one use the following commands
git push origin -u <new-branch-name>
git push origin --delete <old-branch-name>
That comes to an end of my first blog :)
Remember, with GitHub, collaboration is just a commit away. Happy coding!