jason328 jason328 - 20 days ago 7
Git Question

Breaking a branch off into separate branches for pull requests

I have a git branch called

shared_pictures_feature
. This branch has a bunch of new files that allows users to upload photos, share photos among friends, and rate photos. To ease my fellow developer's sanity during a code review, I want to break the
shared_pictures_feature
branch into three separate branches for PR review.

Each branch should only contain the relevant files I need for a particular sub-feature. For example, the
upload_photos_feature
branch should only contain code from the
shared_pictures_feature
that deals with uploading photos. Here's my question, how do I do this with git? Is this even possible?

Answer

There are a few different ways to do this, but here is what I would suggest.

First, create and switch to your first branch.

git checkout -b upload_photos_feature

Next, find the commit where you branched shared_pictures_feature off of. I would suggest reading this StackOverflow question for details on how to do that. Then, in order to uncommit all of your files in that branch, you will want to do:

git reset --mixed <parent commit of both branches>

The next step will be to commit the relevant files. Just add and commit the files that you want in your upload_photos_feature branch, leaving the rest untouched.

Now, you will want to switch back to your original branch and restore the state you were in before.

git reset --hard HEAD
git checkout shared_pictures_feature

Now, you will want to go through those steps again for each new branch. Just make sure that you do the first step of creating the new branch each time since you don't want to lose anything on shared_pictures_feature.