Reputation: 797
Suppose I have a repo, and there are two branches, one is master, the other one is branch2. All the commits will be into master, but part of the commits will be in branch2.
For example, master's commit: c1, c2, c3, e1, c4, ..., e2, cn..., e3, .... If I use git merge, then all the commit will be in branch2. But I want to excludes e1, e2, e3, etc.
Is it possible to have an effective way? Thanks.
Upvotes: 1
Views: 588
Reputation: 520978
There are two ways that you can go about doing this. The first way is to use git-rebase
in interactive mode.
git checkout branch2
git rebase -i master
You will be prompted with a something looking like this:
pick a8xl3do Comment for commit c1
pick 49Un2Sa Comment for commit c2
pick d0a9iUl Comment for commit c3
pick G38c8oA Comment for commit e1
Git will give you a list of options, including the following comment:
# If you remove a line here THAT COMMIT WILL BE LOST.
You can delete the lines corresponding to the commits you don't want (e1, e2, e3, etc.) and then finish the rebase.
The other option for your situation is to use git cherry-pick
. Using cherry-pick
you would basically hand-pick the commits from master
which you want. Continuing with our example from above:
git checkout branch2
git cherry-pick a8xl3do
git cherry-pick 49Un2Sa
git cherry-pick d0a9iUl
git cherry-pick G38c8oA
# But don't cherry-pick the SHA1 hashes for commits e1, e2, e3, etc.
If you are dealing with a lot of commits, then rebase
is probably the way to go. Cherry-picking many commits is error prone, because you have to manually specify all of them, in the right order, and you have to remember which ones to exclude.
Upvotes: 3