Mark
Mark

Reputation: 6445

github - checked out from wrong branch

I have two branches, my master A, my staging B, and my changes, C.

I branched C from B and made various commits, however I now need to merge in all changes on branch C into master rather than merging in all of B itself.

Is there an easy way to do this? I've checked stack overflow and apparently git checkout will only work if the base files are identical, which in this case they aren't...

Upvotes: 1

Views: 1569

Answers (2)

Sajib Khan
Sajib Khan

Reputation: 24156

You can create a new branch from master. Then cherry-pick the range of commits of C.

Copy the oldest and newest commit hash of C branch from git log output.

$ git checkout C
$ git log --oneline --decorate --all --graph

Checkout to a new branch (say, newC) from master branch and cherry-pick the range of commits.

$ git checkout master
$ git pull origin master
$ git checkout -b newC

$ git cherry-pick <oldest-commit>^..<newest-commit>

Note: if oldest-commit = P and newest-commit = Z (P < Z) then cherry-pick command should be git cherry-pick P^..Z

Upvotes: 1

Aleksandar Zoric
Aleksandar Zoric

Reputation: 1483

"You use the cherry-pick command to get individual commits from one branch.

If the change(s) you want are not in individual commits, then use the method shown here to split the commit into individual commits. Roughly speaking, you use git rebase -i to get the original commit to edit, then git reset HEAD^ to selectively revert changes, then git commit to commit that bit as a new commit in the history.

There is another nice method here in Red Hat Magazine, where they use git add --patch or possibly git add --interactive which allows you to add just parts of a hunk, if you want to split different changes to an individual file (search in that page for "split").

Having split the changes, you can now cherry-pick just the ones you want." - Direct Quote from How do you merge selective files with git-merge?

Upvotes: 0

Related Questions