J. Reku
J. Reku

Reputation: 529

GIT how to split up commits from branch into different branches?

I have a single branch with commits for feature A, B, C, D. I have about 40 commits, and each of the commits are in mixed order. I have made a pull request, but I need to split up this branch into branch A, branch B, etc. with the commits associated with each feature. So feature A commits in branch A.

Unfortunately, i'm new to git and it still confuses me. How do you pull out the commits from a single branch and put them inside their own branch?

Upvotes: 3

Views: 915

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

Try using this syntax:

git checkout -b A_branch <SHA-1 hash for commit A>
git push origin A_branch

The first command creates a new branch called A_branch based on the A commit. It also checks out that branch. Then, we push your new branch to the remote. Just repeat the above two commands for each of the four commits and you are done.

Note: To find out the SHA-1 hash for each of the four commits, you may run git log from the HEAD of your current branch. SHA-1 hashes look something like this:

3dk92dk2lvnkls ...

This is the commit hash for some commit.

Edit:

Note that these operations do not affect your original branch. After you have created and pushed everything you need, you may return to your original branch via:

git checkout original_branch

Upvotes: 1

Ivan Sheihets
Ivan Sheihets

Reputation: 830

You can use git cherry-pick Create new branch for feature A, checkout on it and use: git cherry-pick a_commit_hash. Repeat it for all your commits

Upvotes: 3

Related Questions