How to push & merge specific commit history?

2019-08-14 13:16发布

问题:

I cloned a Repo from Perfoce into Git locally with all its history, call it SubProj, and then I pushed it to a remote repo, and used that remote repo to merge SubProj under a SuperProj.

There is no way to import part of the history from Perforce to Git, either @all or none. Anyway, It went fine and I deleted the local repo, and its remote.

Now, there are changes on the Perforce depot SubProj, so I cloned SubProj again with all the history, I want to push then merge with SuperProj but I do not want the whole history (only the new changes).

Say SubProj has this history, and it is local:

A--B--C--D--E--F--G--H

I know we can push starting from the oldest commit, and until a specific commit. Ex:

git push SubProjRemote <commit C on master>:master

Will push A--B--C to the remote.

But:

Can I push only E--F--G--H to the remote repo?

Can I push all, but then merge only E--F--G--H?

回答1:

If you want to merge a specific section of a branch into another branch, you can use rebase's onto feature. Once you are on the branch with the commits you want + the extra commits, you can run:

git rebase --onto < branch_to_rebase_off_of > < commit_to_stop_before > < first_commit_to_include >

For your example taking commits E through H but not A-D, you can run the following, substituting the commit hash for D and H:

git rebase --onto other_branch D H

The branch should now have the base of other_branch, but it should have also applied commits E through H. You can then push this branch wherever it needs to go.