git cherry-pick should be your answer here.
Apply the change introduced by an existing commit.
Do not forget to read bdonlan's answer about the consequence of cherry-picking in this post:
"Pull all commits from a branch, push specified commits to another", where:
A-----B------C
\
\
D
becomes:
A-----B------C
\
\
D-----C'
The problem with this commit is that git considers commits to include all history before them
Where C' has a different
SHA-1ID.
Likewise, cherry picking a commit from one branch to another basically involves generating a patch, then applying it, thus losing history that way as well.
Answer from VonC on Stack OverflowThis changing of commit IDs breaks git's merging functionality among other things (though if used sparingly there are heuristics that will paper over this).
More importantly though, it ignores functional dependencies - if C actually used a function defined in B, you'll never know.
git cherry-pick should be your answer here.
Apply the change introduced by an existing commit.
Do not forget to read bdonlan's answer about the consequence of cherry-picking in this post:
"Pull all commits from a branch, push specified commits to another", where:
A-----B------C
\
\
D
becomes:
A-----B------C
\
\
D-----C'
The problem with this commit is that git considers commits to include all history before them
Where C' has a different
SHA-1ID.
Likewise, cherry picking a commit from one branch to another basically involves generating a patch, then applying it, thus losing history that way as well.
This changing of commit IDs breaks git's merging functionality among other things (though if used sparingly there are heuristics that will paper over this).
More importantly though, it ignores functional dependencies - if C actually used a function defined in B, you'll never know.
You can use git cherry-pick to apply a single commit by itself to your current branch.
Example: git cherry-pick d42c389f
git - Merge up to a specific commit - Stack Overflow
How to merge branches to a specific commit
git - How merge branch with specific commit - Stack Overflow
git - How to merge specific commit id from different branch in same repo - Stack Overflow
The git cherry-pick <commit> command allows you to take a single commit (from whatever branch) and, essentially, rebase it in your working branch.
Chapter 5 of the Pro Git book explains it better than I can, complete with diagrams and such. (The chapter on Rebasing is also good reading.)
Lastly, there are some good comments on the cherry-picking vs merging vs rebasing in another SO question.
If BranchA has not been pushed to a remote then you can reorder the commits using rebase and then simply merge. It's preferable to use merge over rebase when possible because it doesn't create duplicate commits.
git checkout BranchA
git rebase -i HEAD~113
... reorder the commits so the 10 you want are first ...
git checkout BranchB
git merge [the 10th commit]
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.
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.
I have two branches, main and develop. Lets say that this is my current commit history:
A - B - C - D (main) (tag 1.0.0) - E - F - G (tag 1.0.1) - H (develop)
I want to bring main to G (tag 1.0.1) so that it is one commit behind develop. Note that, I want all commits between main and G to also be included. Would I use cherry-pick or is there a better way?