21

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 ?

johnode
  • 720
  • 3
  • 12
  • 31
  • 3
    In this case you don't have to move commits because staging branch can be fast-forwarded to master. `git checkout staging`, `git merge master`, `git checkout master`, `git reset --hard c7-hash` – tewe Feb 04 '13 at 23:48
  • possible duplicate of [How to move certain commits to another branch in git?](http://stackoverflow.com/questions/2369426/how-to-move-certain-commits-to-another-branch-in-git) – Kevin Sep 05 '13 at 20:04
  • possible duplicate of [How can I move recent commit(s) to a new branch with git?](http://stackoverflow.com/questions/1628563/how-can-i-move-recent-commits-to-a-new-branch-with-git) – Aaron Digulla Jan 28 '14 at 08:06

1 Answers1

47

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

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • After doing this let's say you commit `x` to the master branch. And then you merge master into staging. Would that merge into staging correctly merge `x` into staging while _not_ reverting c8...c15 in staging due to the fact those commits were already reset in master? Want to ensure c8...c15 stay in staging after that merge. – Marcus Leon Apr 09 '16 at 12:28
  • @MarcusLeon, merging won't revert those commits. Those commits were not reverted in master, they were removed completely. A reset is not a revert. It's easy to create test commits and test scenarios like that, try it. – Jonathan Wakely Apr 11 '16 at 20:14