Skip to main content

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.

Related articles

Andrew Fletcher12 Oct 2023
What the following git commands do...
git reset, git revert, and git cherry-pick are three Git commands used for different purposes related to managing your version control history. Here's a brief overview of each command:git reset git revert git cherry-pick Snapshotgit revertis to roll back to a previous version of the repo...
Andrew Fletcher29 Sep 2023
Adding a Git repo to a server
git clone git@bitbucket.org:{username}/{repo}.gitAnd I was unceremoniously delivered the following errorfatal: could not create work tree dir '{project}': Permission deniedMy initial thought was the error due to server permission... being sudo.  This was tested by running the commandsudo git...
Andrew Fletcher26 Sep 2023
Git changing local and remote branch name
Something I haven't had to do in a while is to change the name of a Git branch both local and remote. Steps to renaming a branchRename your local branch:If you are on the branch you want to rename:git branch -m new-nameWhereas, if you're on a different branch:git branch -m old-name...