Skip to content

Commit 8b979a6

Browse files
committed
Merge branch 'je/doc-rebase' into jch
* 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 ec9709c + 99795a1 commit 8b979a6

File tree

1 file changed

+145
-155
lines changed

1 file changed

+145
-155
lines changed

Documentation/git-rebase.adoc

Lines changed: 145 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -16,198 +16,101 @@ 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
24-
`branch.<name>.remote` and `branch.<name>.merge` options will be used (see
25-
linkgit:git-config[1] for details) and the `--fork-point` option is
26-
assumed. If you are currently not on any branch or if the current
27-
branch does not have a configured upstream, the rebase will abort.
28-
29-
All changes made by commits in the current branch but that are not
30-
in `<upstream>` are saved to a temporary area. This is the same set
31-
of commits that would be shown by `git log <upstream>..HEAD`; or by
32-
`git log 'fork_point'..HEAD`, if `--fork-point` is active (see the
33-
description on `--fork-point` below); or by `git log HEAD`, if the
34-
`--root` option is specified.
35-
36-
The current branch is reset to `<upstream>` or `<newbase>` if the
37-
`--onto` option was supplied. This has the exact same effect as
38-
`git reset --hard <upstream>` (or `<newbase>`). `ORIG_HEAD` is set
39-
to point at the tip of the branch before the reset.
40-
41-
[NOTE]
42-
`ORIG_HEAD` is not guaranteed to still point to the previous branch tip
43-
at the end of the rebase if other commands that write that pseudo-ref
44-
(e.g. `git reset`) are used during the rebase. The previous branch tip,
45-
however, is accessible using the reflog of the current branch
46-
(i.e. `@{1}`, see linkgit:gitrevisions[7]).
47-
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":
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.
6223

6324
------------
6425
A---B---C topic
6526
/
6627
D---E---F---G master
6728
------------
6829

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:
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+
shortcut for `git checkout topic && git rebase master`.
7635

7736
------------
7837
A'--B'--C' topic
7938
/
8039
D---E---F---G master
8140
------------
8241

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.
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:
8645

87-
If the upstream branch already contains a change you have made (e.g.,
88-
because you mailed a patch which was applied upstream), then that commit
89-
will be skipped and warnings will be issued (if the 'merge' backend is
90-
used). For example, running `git rebase master` on the following
91-
history (in which `A'` and `A` introduce the same set of changes, but
92-
have different committer information):
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
9351
94-
------------
95-
A---B---C topic
96-
/
97-
D---E---A'---F master
98-
------------
52+
git rebase --continue
9953
100-
will result in:
54+
2. Stop the `git rebase` and return your branch to its original state with
10155
102-
------------
103-
B'---C' topic
104-
/
105-
D---E---A'---F master
106-
------------
56+
git rebase --abort
10757
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`.
58+
3. Skip the commit that caused the merge conflict with
11159
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'.
60+
git rebase --skip
11561
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:
62+
If you don't specify an `<upstream>` to rebase onto, the upstream configured in
63+
`branch.<name>.remote` and `branch.<name>.merge` options will be used (see
64+
linkgit:git-config[1] for details) and the `--fork-point` option is
65+
assumed. If you are currently not on any branch or if the current
66+
branch does not have a configured upstream, the rebase will abort.
14367

144-
------------
145-
H---I---J topicB
146-
/
147-
E---F---G topicA
148-
/
149-
A---B---C---D master
150-
------------
68+
Here is a more detailed description of what `git rebase <upstream>` does:
15169

152-
then the command
70+
First, it makes a list of all commits in the current branch that are not in
71+
`<upstream>`. This is the same set of commits that would be shown by `git log
72+
<upstream>..HEAD`. You can use `--fork-point` or `--root` to change how this
73+
list of commits is constructed.
15374

154-
git rebase --onto master topicA topicB
75+
Then it checks out `<upstream>` (or `<newbase>` if the `--onto` option was
76+
supplied) with the equivalent of `git switch --detach <upstream>`.
15577

156-
would result in:
78+
Then it replays the commits, one by one, in order. This is similar to running
79+
`git cherry-pick <commit>` for each commit. See REBASING MERGES for how merges
80+
are handled.
15781

158-
------------
159-
H'--I'--J' topicB
160-
/
161-
| E---F---G topicA
162-
|/
163-
A---B---C---D master
164-
------------
82+
Finally, it updates your branch to point to the final commit with the equivalent
83+
of `git switch -C <branch>`.
16584

166-
This is useful when topicB does not depend on topicA.
85+
[NOTE]
86+
`ORIG_HEAD` is set to point at the tip of the branch before the rebase.
87+
`ORIG_HEAD` is not guaranteed to still point to the previous branch tip
88+
at the end of the rebase if other commands that write that pseudo-ref
89+
(e.g. `git reset`) are used during the rebase. The previous branch tip,
90+
however, is accessible using the reflog of the current branch
91+
(i.e. `@{1}`, see linkgit:gitrevisions[7]).
16792

168-
A range of commits could also be removed with rebase. If we have
169-
the following situation:
93+
If the upstream branch already contains a change you have made (e.g.,
94+
because you mailed a patch which was applied upstream), then that commit
95+
will be skipped and warnings will be issued (if the 'merge' backend is
96+
used). For example, running `git rebase master` on the following
97+
history (in which `A'` and `A` introduce the same set of changes, but
98+
have different committer information):
17099

171100
------------
172-
E---F---G---H---I---J topicA
101+
A---B---C topic
102+
/
103+
D---E---A'---F master
173104
------------
174105

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:
106+
will result in:
180107

181108
------------
182-
E---H'---I'---J' topicA
109+
B'---C' topic
110+
/
111+
D---E---A'---F master
183112
------------
184113

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-
211114
MODE OPTIONS
212115
------------
213116

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

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

0 commit comments

Comments
 (0)