I accidentally made 10 commits on branch "testing" when I intended to commit them on branch "master". The other commits on the "testing" branch are garbage, so I don't want to merge it with "master". Instead, I just want to replay the last 10 commits on master.
Asked
Active
Viewed 2.6k times
3 Answers
112
Rebase should do it.
git rebase -p --onto master testing~10 testing
This will copy the last ten commits on testing to master and make that the new testing (the old testing will be an orphan). Then you can merge master to testing as a fast-forward.
git checkout master
git merge testing

Talljoe
- 14,593
- 4
- 43
- 39
-
12Perhaps worth noting that this leaves testing at the same point as master, leaving the 'garbage' commits orphaned. This may or may not be a good thing. Another possibility would be git checkout master; git reset --hard testing; git rebase --onto HEAD@{1} HEAD~10 – CB Bailey Jun 10 '09 at 07:21
-
@CharlesBailey What git flog are you intending to hit there? – Fredrick Jan 18 '16 at 14:31
-
Is it safe to say that rebase has the advantage of the changes only being in one place, but cherry-pick works better if the changes were already pushed to another repo (e.g., origin)? – LexH May 31 '18 at 17:57
-
the `-p` (preserve) flag is now deprecated (you get a `fatal: --preserve-merges was replaced by --rebase-merges` when you try to use it, my git version 2.38.0) – Sandra Aug 01 '23 at 10:19
26
- git checkout master
- git whatchanged testing
- git cherry-pick _________
?

Ron
- 1,932
- 20
- 17
-
5Just an fyi - cherry-pick will only do one commit at a time, so you'll have to cherry-picket testing~9 then testing~8 then ... testing. That's why I prefer the rebase approach that Talljoe suggested... of course the result is the same. In fact, if you do the rebase interactively, git will actually use cherry-pick under the hood. – Pat Notz Jun 10 '09 at 03:57
-
14@PatNotz `git cherry-pick` currently can currently do multiple commits at a time (e.g. `git cherry-pick testing~10..testing`). – Max Nanasy Mar 13 '13 at 00:25
-
You should never use `cherry-pick`. Read [this article](http://www.draconianoverlord.com/2013/09/07/no-cherry-picking.html) for further information. – Tim Nov 23 '15 at 15:54
-
5
-
3@prasanthv You're right, I mean: never use ``cherry-pick`` as a basis of your workflow. Git's ``cherry-pick`` is a very useful feature in other circumstances, but should not be used to move commits from one branch to another, there are better tools for that :) – Tim Feb 21 '18 at 08:40
-
-
@ThomasMcLeod sorry that the page has been made unavailable in between. The title of the article was almost self-explanatory: "If you cherry pick, your branch model is wrong". You can find it there : https://web.archive.org/web/20210507042602/https://www.draconianoverlord.com/2013/09/07/no-cherry-picking.html – Tim Oct 04 '22 at 07:17
-
@prasanthv you're wright, I mean "don't use it as a branching-model". Sometimes people cherry-pick what could be easily branched-out from a feature branch thus preserving their workflow, but they don't know it. – Tim Oct 04 '22 at 07:17
1
As said in comments, the rebase
-inspired answer is leaving the 'garbage' commits orphaned.
Just use simple tools:
git checkout master
git merge testing
git checkout testing
git reset --hard HEAD~10 # Go back 10 commits (*1)
git checkout master
(*1) You will only be "losing" commits from the testing
branch, since you'll have those commits in master
thanks to the merge
.

Tim
- 2,052
- 21
- 30
-
1Doesn't this approach end up merging the garbage commits into `master` as well as the 10 the OP wants to merge in? – mpavey Apr 30 '18 at 15:43
-
1... and _now_ I understand what the OP asked for about these garbage commits. You are right, these are merged in master, forget my suggestion. The question is then does the OP want these garbage commits deleted but the resulting code kept, or totally forget this work? I think that the latter requires cherry-picking while the other needs a rebase. – Tim May 01 '18 at 21:11