I created a new branch named newbranch
from the master
branch in git. Now I have done some work and want to merge newbranch
to master
; however, I have made some extra changes to newbranch
and I want to merge newbranch
up to the fourth-from-the-last commit to master
.
I used cherry-pick
but it shows the message to use the right options:
git checkout master
git cherry-pick ^^^^HEAD newbranch
Can I use git merge
to do it instead?
git merge newbranch <commitid>
This question is related to
git
git-merge
git-cherry-pick
Recently we had a similar problem and had to solve it in a different way. We had to merge two branches up to two commits, which were not the heads of either branches:
branch A: A1 -> A2 -> A3 -> A4
branch B: B1 -> B2 -> B3 -> B4
branch C: C1 -> A2 -> B3 -> C2
For example, we had to merge branch A up to A2 and branch B up to B3. But branch C had cherry-picks from A and B. When using the SHA of A2 and B3 it looked like there was confusion because of the local branch C which had the same SHA.
To avoid any kind of ambiguity we removed branch C locally, and then created a branch AA starting from commit A2:
git co A
git co SHA-of-A2
git co -b AA
Then we created a branch BB from commit B3:
git co B
git co SHA-of-B3
git co -b BB
At that point we merged the two branches AA and BB. By removing branch C and then referencing the branches instead of the commits it worked.
It's not clear to me how much of this was superstition or what actually made it work, but this "long approach" may be helpful.
Run below command into the current branch folder to merge from this <commit-id>
to current branch, --no-commit
do not make a new commit automatically
git merge --no-commit <commit-id>
git merge --continue
can only be run after the merge has resulted in conflicts.
git merge --abort
Abort the current conflict resolution process, and try to reconstruct the pre-merge state.
To keep the branching clean, you could do this:
git checkout newbranch
git branch newbranch2
git reset --hard <commit Id> # the commit at which you want to merge
git checkout master
git merge newbranch
git checkout newbranch2
This way, newbranch will end where it was merged into master, and you continue working on newbranch2.
Source: Stackoverflow.com