Strategy for Merging Branchs Using Git

Here’s a strategy for merging branches:

git checkout <branch_to_merge_into>
git pull --rebase
git checkout <feature_branch>
git rebase <branch_to_merge_into>

This will update the branch that you want to merge into to the latest version. Then pull in all of those changes into the feature branch. This is getting the feature branch to align better with the branch that you are merging the feature branch into. This is also where you can fix any merge issues. I would also run tests, if you have any, and subsequently fix any features or tests that have been broken.

git checkout <branch_to_merge_into>
git merge <feature_branch>

This will switch back to the branch you want to merge your changes into and then do the merge. This should hopefully go pretty smoothly since you synced up the feature branch with the branch you want to merge stuff into. I would also rerun tests again one more time just to make sure everything still works.

Then push your changes.

git push -u <remote> <branch_to_merge_to>

Now if you want to delete the branches do

git push <remote> :<feature_branch>
git branch -d <feature_branch>

The first command deletes the branch at the remote repository. Notice that the remote delete requires a : prefixed to the branch name. The second command will delete the branch locally.

Here’s an example:

git checkout master
git pull --rebase
git checkout my_cool_new_feature
git rebase master

// fix conflicts, run tests, fix broken features or tests

git checkout master
git merge my_cool_new_feature

// re-run tests

git push -u origin master

git push origin :my_cool_new_feature
git branch -d my_cool_new_feature