In our projects, team members frequently generate new Git branches linked to the same Jira number, resulting in cluttered and disorganised structures that can pose challenges for both current and future developers. For instance:
Jira: ABC-123
Ticket work: Reformat headers
Over time, these branches accumulate and become disorganised, resulting in names like:
ABC-123-reformat-headers
ABC-123-format-headers
ABC-123-format-header
This can lead to a cluttered and inefficient workflow. It's preferable to consolidate this work within a single feature branch.
Correct approach
Here's the correct approach for updating a feature branch to incorporate changes from the master branch:
1. Switch to the master branch
git checkout master
2. Pull the latest changes from the remote master branch
git pull origin master
3. Switch back to your feature branch
git checkout feature/branch
4. Rebase your feature branch onto the updated master
git rebase master
During the rebase, Git will apply your feature branch's changes on top of the latest master branch changes. If conflicts arise, Git will pause and prompt you to resolve them. After resolving conflicts (if any), resume the rebase by running:
git rebase --continue
Upon completion, your feature branch will be synchronised with the latest changes from the master branch. Keep in mind that rebasing rewrites commit history, making it suitable for local branches that haven't been pushed to a shared repository.
If your feature branch is already pushed to a shared repository and others are using it, consider using the following command in step 2 to integrate master changes into your feature branch without creating a merge commit:
git pull origin master --rebase
However, be cautious as this might necessitate force-pushing your feature branch to the remote repository. Communicate with your team if such action is required.