Pistos (owner)

Revisions

gist: 203707 Download_button fork
public
Description:
How to rebase the last few commits elsewhere, in case you committed to the wrong branch.
Public Clone URL: git://gist.github.com/203707.git
Embed All Files: show embed
rebasing part of a branch #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# This information was provided by doener from irc.freenode.net#git
# Many thanks go to him!
 
A---B---C---D (rightbranch)
 \
  E---F---G---H---I---J (wrongbranch)
 
 
You did:
git checkout wrongbranch
git rebase --onto rightbranch HEAD~6
 
Which is equal to:
git rebase --onto rightbranch HEAD~6 wrongbranch
 
and results in:
              E'--F'--G'--H'--I'--J' (wrongbranch)
             /
A---B---C---D (rightbranch)
 \
  E---F---G---H---I---J
 
 
 
You wanted:
git checkout rightbranch
git reset --hard wrongbranch
git rebase --onto rightbranch@{1} HEAD~6
git checkout wrongbranch
git reset
 
The reset --hard wrongbranch gives us:
 
A---B---C---D
 \
  E---F---G---H---I---J (wrongbranch) (rightbranch)
 
And the reflog entry rightbranch@{1} now references D (previous state of rightbranch, before the reset)
 
Thus:
git rebase --onto rightbranch@{1} HEAD~6
Equals:
git rebase --onto D A rightbranch
 
So that gives:
 
A---B---C---D---E'--F'--G'--H'--I'--J' (rightbranch)
 \
  E---F---G---H---I---J (wrongbranch)
 
So what's left is to fixup wrong branch, and that's what the remaining checkout + reset commands do.