ChatGPT解决这个技术问题 Extra ChatGPT

Merge up to a specific commit

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>

m
mtyaka

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.


not only a single commit but all the commits previous to commit-id
Yes, it'll merge all commits from newbranch since the time its history diverged from master up to commit-id into master branch. You can think of git merge <commit-id> as of merging some unnamed branch that ends with commit-id into your current branch.
Can I merge between two local branches other than master, using commit id?
@skt, you can, just be on the local branch you want to merge into before using git merge. ie say you want to merge commit 18a6fac from branch b2 into branch b1 just do git checkout b1; git merge 18a6fac
This does NOT seem to work, If I am on a branch I want to merge into and there s some other branch with commits A B and C, and I merge in C it only gets the changes made in C, not A and B.
t
tkruse

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.


This works well if you need a branch to be able to create a PR.
E
ElasticCode

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.


K
Kiran Mohan

Merge upto commit

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

Suppose you want to merge branch src to branch dst but only up to a particular commit id commitId

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

p
pid

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.


A
Andrey Rubshtein

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

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