简体   繁体   中英

Git: move specific commits to another branch

A have repository with two branches.

Master branch commits:

c1, c2, c3, c4, c5, c6, c7, ..., c15, ...

Staging Branch commits:

c1, c2, c3, c4, c5, c6, c7

I want to move all commits from Master branch after c7 to staging branch

and then revert Master branch

with

git reset --hard c7-hash

How to move/copy specific commits from one branch to another ?

In the case you've described, where all commits on the staging branch are also on the master branch, it's very easy:

git checkout staging
git merge master
git checkout master
git reset --hard c7-hash

The merge will be a fast-forward.

In the general case, you can use git cherry-pick c8 c9 c10 c11 c12 c13 c14 c15 to cherry pick individual commits to the current branch. A shorter way to cherry pick all commits that are on master but not the current branch is git cherry-pick ..master , and there are other examples shown by git help cherry-pick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM