Git reuse branch or delete and create again

In many workflows, once a feature branch has been merged back into master it is deleted. GitHub is probably the prime example of this. If you follow this school of thought, you would delete feat/foo and create a new feature branch for your next sprint.

If you really want to keep using the branch, then you will either have to rebase feat/foo on master or merge master into feat/foo. I don't see any advantage to rebasing, which could be messy, so let's consider merging. You merged feat/foo into master at commit E. Therefore master already has all the features from feat/foo, but the reverse is not true, i.e. feat/foo is likely missing several features which have been introduced into master since commit D. To do the merge you would use this command:

git checkout feat/foo
git merge master

You may have to resolve merge conflicts arising from new features in master which are not yet in the feat/foo branch.

Now the feat/foo branch is up to date with master, and you can keep using it if you wish. Personally, I would just leave feat/foo where it is and create an entirely new feature branch. You can keep it around for a few sprints until you are sure that deleting it safe.


The question is a little bit of an oversimplification. In your case you have "1 story" with "2 tasks": refactor and feature. You already merged the refactor task and moved onto the feature task. That means we didn't consider the possibility of merge conflicts. You can simply merge/rebase to reuse the branch which is the easiest possible scenario.

Why would you even merge task 1 before moving onto task 2? Why not just do all the tasks to complete the story before you merge? It appears you already have an intuition that short, focused work is easier to manage. For most enterprise level teams, longer lived branches are worse than short lived.

In reality, when choosing to "delete or reuse" branches you should consider the possibility of complex merge conflicts. Long lived branches, poor team communication, refactoring, big teams, poorly coordinated stories...all these increase the possibility of buggy, nightmare level merge conflict resolution.

When choosing to delete or reuse a branch I always go with delete. My recommendation comes with a holistic view that other higher order problems are also being solved (again think big teams, poor communication, refactoring etc). Once a branch is merged there is no significant value add in increasing the lifetime of said branch. However, there can be a significant cost that comes with long lived branches. See also GitFlow.


Since you need to continue working on branch feat/foo, the first thing to do is to checkout it:

git checkout feat/foo

Because you didn't complete the work on the new feature you keep working and committing on the branch until you finish the work and are ready to merge it back into master.

From time to time it's good to pull into the branch the latest changes from the master branch. You do that by running:

git merge master

while you are on the feat/foo branch.

It doesn't make much sense to remove the branch just to re-create it again. git checkout feat/foo followed by git merge master produces the same result.

Tags:

Git