Merge up to a specific commit

GitGit MergeGit Cherry-Pick

Git Problem Overview


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>

Git Solutions


Solution 1 - Git

Sure, being in master branch all you need to do is:

git merge <commit-id>

where commit-id is hash of the last commit from newbranch that you want to get in your master branch.

You can find out more about any git command by doing git help <command>. It that case it's git help merge. And docs are saying that the last argument for merge command is <commit>..., so you can pass reference to any commit or even multiple commits. Though, I never did the latter myself.

Solution 2 - Git

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.

Solution 3 - Git

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.

Solution 4 - Git

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.

Solution 5 - Git

To merge a specific branch to your master just do this:

git reset --hard <commit Id> <-- the commit at which you want to merge

Solution 6 - Git

Merge upto commit

I find this to be a very reliable way to merge upto a commit. The "from" and "to" branches are never modified to do the merge (except for the merge on "to" branch).

Suppose you want to merge branch A to branch B but only up to a particular commit id commitId

git checkout A
git switch -c temp commitId        # create temporary branch at commitId
git switch -c B             
git merge temp                     # merge temp branch to B
git branch -d temp                 # remove temp branch as we dont need it anymore.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDauView Question on Stackoverflow
Solution 1 - GitKL-7View Answer on Stackoverflow
Solution 2 - GittkruseView Answer on Stackoverflow
Solution 3 - GitElasticCodeView Answer on Stackoverflow
Solution 4 - GitpidView Answer on Stackoverflow
Solution 5 - GitJanis Basis BasovsView Answer on Stackoverflow
Solution 6 - GitKiran MohanView Answer on Stackoverflow