Skip to content

Commit 318d132

Browse files
committed
Merge branch 'je/doc-rebase' into seen
* je/doc-rebase: doc: git-rebase: update discussion of internals doc: git-rebase: move --onto explanation down doc: git rebase: clarify arguments syntax doc: git rebase: dedup merge conflict discussion doc: git-rebase: start with an example
2 parents 375f520 + 161247b commit 318d132

File tree

1 file changed

+137
-148
lines changed

1 file changed

+137
-148
lines changed

Documentation/git-rebase.adoc

Lines changed: 137 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -16,74 +16,79 @@ SYNOPSIS
1616

1717
DESCRIPTION
1818
-----------
19-
If `<branch>` is specified, `git rebase` will perform an automatic
20-
`git switch <branch>` before doing anything else. Otherwise
21-
it remains on the current branch.
19+
Transplant a series of commits onto a different starting point.
2220

23-
If `<upstream>` is not specified, the upstream configured in
21+
For example, imagine that you have been working on the `topic` branch in this
22+
history, and you want to "catch up" to the work done on the `master` branch.
23+
24+
------------
25+
A---B---C topic
26+
/
27+
D---E---F---G master
28+
------------
29+
30+
You want to transplant the commits you made on `topic` since it diverged from
31+
`master` (i.e. A, B, and C), on top of the current `master`. You can do this
32+
by running `git rebase master` while the `topic` branch is checked out. If you
33+
want to rebase `topic` while on another branch, `git rebase master topic` is a
34+
short-cut for `git switch topic && git rebase master`.
35+
36+
------------
37+
A'--B'--C' topic
38+
/
39+
D---E---F---G master
40+
------------
41+
42+
If there is a merge conflict during this process, `git rebase` will stop at the
43+
first problematic commit and leave conflict markers. If this happens, you can do
44+
one of these things:
45+
46+
1. Resolve the conflict. You can use `git diff` to find the markers (<<<<<<)
47+
and make edits to resolve the conflict. For each file you edit, you need to
48+
tell Git that the conflict has been resolved. You can mark the conflict as
49+
resolved with `git add <filename>`. After resolving all of the conflicts,
50+
you can continue the rebasing process with
51+
52+
git rebase --continue
53+
54+
2. Stop the `git rebase` and return your branch to its original state with
55+
56+
git rebase --abort
57+
58+
3. Skip the commit that caused the merge conflict with
59+
60+
git rebase --skip
61+
62+
If you don't specify an `<upstream>` to rebase onto, the upstream configured in
2463
`branch.<name>.remote` and `branch.<name>.merge` options will be used (see
2564
linkgit:git-config[1] for details) and the `--fork-point` option is
2665
assumed. If you are currently not on any branch or if the current
2766
branch does not have a configured upstream, the rebase will abort.
2867

29-
All changes made by commits in the current branch but that are not
68+
Here is a more detailed description of what `git rebase <upstream>` does:
69+
70+
First, all changes made by commits in the current branch but that are not
3071
in `<upstream>` are saved to a temporary area. This is the same set
3172
of commits that would be shown by `git log <upstream>..HEAD`; or by
3273
`git log 'fork_point'..HEAD`, if `--fork-point` is active (see the
3374
description on `--fork-point` below); or by `git log HEAD`, if the
3475
`--root` option is specified.
3576

36-
The current branch is reset to `<upstream>` or `<newbase>` if the
77+
Then the current branch is reset to `<upstream>` or `<newbase>` if the
3778
`--onto` option was supplied. This has the exact same effect as
3879
`git reset --hard <upstream>` (or `<newbase>`). `ORIG_HEAD` is set
3980
to point at the tip of the branch before the reset.
4081

82+
Then the commits that were previously saved into the temporary area are
83+
reapplied to the current branch, one by one, in order.
84+
4185
[NOTE]
4286
`ORIG_HEAD` is not guaranteed to still point to the previous branch tip
4387
at the end of the rebase if other commands that write that pseudo-ref
4488
(e.g. `git reset`) are used during the rebase. The previous branch tip,
4589
however, is accessible using the reflog of the current branch
4690
(i.e. `@{1}`, see linkgit:gitrevisions[7]).
4791

48-
The commits that were previously saved into the temporary area are
49-
then reapplied to the current branch, one by one, in order. Note that
50-
any commits in `HEAD` which introduce the same textual changes as a commit
51-
in `HEAD..<upstream>` are omitted (i.e., a patch already accepted upstream
52-
with a different commit message or timestamp will be skipped).
53-
54-
It is possible that a merge failure will prevent this process from being
55-
completely automatic. You will have to resolve any such merge failure
56-
and run `git rebase --continue`. Another option is to bypass the commit
57-
that caused the merge failure with `git rebase --skip`. To check out the
58-
original `<branch>` and remove the `.git/rebase-apply` working files, use
59-
the command `git rebase --abort` instead.
60-
61-
Assume the following history exists and the current branch is "topic":
62-
63-
------------
64-
A---B---C topic
65-
/
66-
D---E---F---G master
67-
------------
68-
69-
From this point, the result of either of the following commands:
70-
71-
72-
git rebase master
73-
git rebase master topic
74-
75-
would be:
76-
77-
------------
78-
A'--B'--C' topic
79-
/
80-
D---E---F---G master
81-
------------
82-
83-
*NOTE:* The latter form is just a short-hand of `git checkout topic`
84-
followed by `git rebase master`. When rebase exits `topic` will
85-
remain the checked-out branch.
86-
8792
If the upstream branch already contains a change you have made (e.g.,
8893
because you mailed a patch which was applied upstream), then that commit
8994
will be skipped and warnings will be issued (if the 'merge' backend is
@@ -105,109 +110,6 @@ will result in:
105110
D---E---A'---F master
106111
------------
107112

108-
Here is how you would transplant a topic branch based on one
109-
branch to another, to pretend that you forked the topic branch
110-
from the latter branch, using `rebase --onto`.
111-
112-
First let's assume your 'topic' is based on branch 'next'.
113-
For example, a feature developed in 'topic' depends on some
114-
functionality which is found in 'next'.
115-
116-
------------
117-
o---o---o---o---o master
118-
\
119-
o---o---o---o---o next
120-
\
121-
o---o---o topic
122-
------------
123-
124-
We want to make 'topic' forked from branch 'master'; for example,
125-
because the functionality on which 'topic' depends was merged into the
126-
more stable 'master' branch. We want our tree to look like this:
127-
128-
------------
129-
o---o---o---o---o master
130-
| \
131-
| o'--o'--o' topic
132-
\
133-
o---o---o---o---o next
134-
------------
135-
136-
We can get this using the following command:
137-
138-
git rebase --onto master next topic
139-
140-
141-
Another example of --onto option is to rebase part of a
142-
branch. If we have the following situation:
143-
144-
------------
145-
H---I---J topicB
146-
/
147-
E---F---G topicA
148-
/
149-
A---B---C---D master
150-
------------
151-
152-
then the command
153-
154-
git rebase --onto master topicA topicB
155-
156-
would result in:
157-
158-
------------
159-
H'--I'--J' topicB
160-
/
161-
| E---F---G topicA
162-
|/
163-
A---B---C---D master
164-
------------
165-
166-
This is useful when topicB does not depend on topicA.
167-
168-
A range of commits could also be removed with rebase. If we have
169-
the following situation:
170-
171-
------------
172-
E---F---G---H---I---J topicA
173-
------------
174-
175-
then the command
176-
177-
git rebase --onto topicA~5 topicA~3 topicA
178-
179-
would result in the removal of commits F and G:
180-
181-
------------
182-
E---H'---I'---J' topicA
183-
------------
184-
185-
This is useful if F and G were flawed in some way, or should not be
186-
part of topicA. Note that the argument to `--onto` and the `<upstream>`
187-
parameter can be any valid commit-ish.
188-
189-
In case of conflict, `git rebase` will stop at the first problematic commit
190-
and leave conflict markers in the tree. You can use `git diff` to locate
191-
the markers (<<<<<<) and make edits to resolve the conflict. For each
192-
file you edit, you need to tell Git that the conflict has been resolved,
193-
typically this would be done with
194-
195-
196-
git add <filename>
197-
198-
199-
After resolving the conflict manually and updating the index with the
200-
desired resolution, you can continue the rebasing process with
201-
202-
203-
git rebase --continue
204-
205-
206-
Alternatively, you can undo the 'git rebase' with
207-
208-
209-
git rebase --abort
210-
211113
MODE OPTIONS
212114
------------
213115

@@ -253,6 +155,8 @@ As a special case, you may use "A\...B" as a shortcut for the
253155
merge base of A and B if there is exactly one merge base. You can
254156
leave out at most one of A and B, in which case it defaults to HEAD.
255157

158+
See TRANSPLANTING A TOPIC BRANCH WITH --ONTO below for examples.
159+
256160
--keep-base::
257161
Set the starting point at which to create the new commits to the
258162
merge base of `<upstream>` and `<branch>`. Running
@@ -1038,6 +942,91 @@ consistent (they compile, pass the testsuite, etc.) you should use
1038942
after each commit, test, and amend the commit if fixes are necessary.
1039943
1040944
945+
TRANSPLANTING A TOPIC BRANCH WITH --ONTO
946+
----------------------------------------
947+
948+
Here is how you would transplant a topic branch based on one
949+
branch to another, to pretend that you forked the topic branch
950+
from the latter branch, using `rebase --onto`.
951+
952+
First let's assume your 'topic' is based on branch 'next'.
953+
For example, a feature developed in 'topic' depends on some
954+
functionality which is found in 'next'.
955+
956+
------------
957+
o---o---o---o---o master
958+
\
959+
o---o---o---o---o next
960+
\
961+
o---o---o topic
962+
------------
963+
964+
We want to make 'topic' forked from branch 'master'; for example,
965+
because the functionality on which 'topic' depends was merged into the
966+
more stable 'master' branch. We want our tree to look like this:
967+
968+
------------
969+
o---o---o---o---o master
970+
| \
971+
| o'--o'--o' topic
972+
\
973+
o---o---o---o---o next
974+
------------
975+
976+
We can get this using the following command:
977+
978+
git rebase --onto master next topic
979+
980+
981+
Another example of --onto option is to rebase part of a
982+
branch. If we have the following situation:
983+
984+
------------
985+
H---I---J topicB
986+
/
987+
E---F---G topicA
988+
/
989+
A---B---C---D master
990+
------------
991+
992+
then the command
993+
994+
git rebase --onto master topicA topicB
995+
996+
would result in:
997+
998+
------------
999+
H'--I'--J' topicB
1000+
/
1001+
| E---F---G topicA
1002+
|/
1003+
A---B---C---D master
1004+
------------
1005+
1006+
This is useful when topicB does not depend on topicA.
1007+
1008+
A range of commits could also be removed with rebase. If we have
1009+
the following situation:
1010+
1011+
------------
1012+
E---F---G---H---I---J topicA
1013+
------------
1014+
1015+
then the command
1016+
1017+
git rebase --onto topicA~5 topicA~3 topicA
1018+
1019+
would result in the removal of commits F and G:
1020+
1021+
------------
1022+
E---H'---I'---J' topicA
1023+
------------
1024+
1025+
This is useful if F and G were flawed in some way, or should not be
1026+
part of topicA. Note that the argument to `--onto` and the `<upstream>`
1027+
parameter can be any valid commit-ish.
1028+
1029+
10411030
RECOVERING FROM UPSTREAM REBASE
10421031
-------------------------------
10431032

0 commit comments

Comments
 (0)