Reputation: 8020
I have a simple branch with multiplpe changes:
Master ---A---B---C
\
Foo E---F---G---H
Now I need to merge into the Master only a part of my changes. I am new to git ( currently using it with SourceCode on Windows ), and was thinking of doing it like this:
Master ---A---B---C---?---J---?---?---
\ /
Foo E---G'
\
Bar F'---H'---
I need only certain parts from the branch. I've been working on 4 different manufacturer APIs to implement. Now, only 1 needs to go in production and the rest are on hold. Let's say there are total of 19 files modified, but I only need 7 of them. Now, I need to split those 7 away, synchronize with the master and push as a different branch, so that the CTO can merge them into Master later on.
Is that the way to do it? And how do I do it correctly?
Upvotes: 0
Views: 1086
Reputation: 5514
If we breakdown your problem, you need to:
There are many ways of doing it, but I will keep it to the simplest flow that I can think of, since you are new to git. so, let us look at splitting the branch:
From your current Foo (head = H) do git checkout -b Bar
This will create the branch Bar with commits E, F, G, H
Now, there are two ways you can reset your Foo branch:
#1 Discard Foo and re-create it
git branch -D Foo
(to delete from local)
git push origin :Foo
(to delete from remote, if you have already pushed it)
Then you need to create a new Foo from commit "C" of your master:
git checkout -b Foo <CommitIdShaForC>
Then you can cherry-pick commits E and G either from git gui by doing gitk Bar
and then right clicking on the commit and choosing cherry-pick, or from console by:
git cherry-pick <Commit Id for E> <Commit Id for G>
In case of merge issues, look at this answer
#2 Revert commits from Foo If you don't mind having history of F and H in your foo, you can simply revert them in git, essentially git creates a reverse patch of your commits F and H and applies them as new commits:
Foo
and it has E -> F -> G -> H
git revert F
E -> F -> G -> H -> F'
where F' is undoing what the commit F addedgit revert H
would lead to: E -> F -> G -> H -> F' -> H'
whose end result in the source code would be: same as having only commits E and G.Alternatively still, you can use the --no-commit flag on git revert to revert both F and H in 1 commit:
git revert --no-commit F
git revert --no-commit H
git commit -a -m "reverting commits F and H"
This would lead to:
E -> F -> G -> H -> I
where I is the commit which undoes F and H
#Update 1 After writing a lengthy answer, I see that OP wants to have "part of the commits" into few branches. For this case, I would highly recommend an interactive rebase so that the commits can be split. Please refer to the official documentation about git rebase, in particular the section about "Splitting Commits". Also, I think you will find this question particularly helpful
Upvotes: 1