Skip to content

Commit 3497438

Browse files
committed
Merge branch 'je/doc-rebase' into jch
Documentation for "git rebase" has been updated. * 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 72cb8e4 + af89d74 commit 3497438

File tree

1 file changed

+133
-165
lines changed

1 file changed

+133
-165
lines changed

Documentation/git-rebase.adoc

Lines changed: 133 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -16,197 +16,78 @@ 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.
20+
You can also use `git rebase` to reorder or combine commits: see INTERACTIVE
21+
MODE below for how to do that.
2222

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":
23+
For example, imagine that you have been working on the `topic` branch in this
24+
history, and you want to "catch up" to the work done on the `master` branch.
6225

6326
------------
6427
A---B---C topic
6528
/
6629
D---E---F---G master
6730
------------
6831

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:
32+
You want to transplant the commits you made on `topic` since it diverged from
33+
`master` (i.e. A, B, and C), on top of the current `master`. You can do this
34+
by running `git rebase master` while the `topic` branch is checked out. If you
35+
want to rebase `topic` while on another branch, `git rebase master topic` is a
36+
shortcut for `git checkout topic && git rebase master`.
7637

7738
------------
7839
A'--B'--C' topic
7940
/
8041
D---E---F---G master
8142
------------
8243

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-
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):
93-
94-
------------
95-
A---B---C topic
96-
/
97-
D---E---A'---F master
98-
------------
99-
100-
will result in:
101-
102-
------------
103-
B'---C' topic
104-
/
105-
D---E---A'---F master
106-
------------
107-
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:
17044

171-
------------
172-
E---F---G---H---I---J topicA
173-
------------
45+
If there is a merge conflict during this process, `git rebase` will stop at the
46+
first problematic commit and leave conflict markers. If this happens, you can do
47+
one of these things:
17448

175-
then the command
49+
1. Resolve the conflict. You can use `git diff` to find the markers (<<<<<<)
50+
and make edits to resolve the conflict. For each file you edit, you need to
51+
tell Git that the conflict has been resolved. You can mark the conflict as
52+
resolved with `git add <filename>`. After resolving all of the conflicts,
53+
you can continue the rebasing process with
17654
177-
git rebase --onto topicA~5 topicA~3 topicA
55+
git rebase --continue
17856
179-
would result in the removal of commits F and G:
57+
2. Stop the `git rebase` and return your branch to its original state with
18058
181-
------------
182-
E---H'---I'---J' topicA
183-
------------
59+
git rebase --abort
18460
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.
61+
3. Skip the commit that caused the merge conflict with
18862
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
63+
git rebase --skip
20464
65+
If you don't specify an `<upstream>` to rebase onto, the upstream configured in
66+
`branch.<name>.remote` and `branch.<name>.merge` options will be used (see
67+
linkgit:git-config[1] for details) and the `--fork-point` option is
68+
assumed. If you are currently not on any branch or if the current
69+
branch does not have a configured upstream, the rebase will abort.
20570

206-
Alternatively, you can undo the 'git rebase' with
71+
Here is a simplified description of what `git rebase <upstream>` does:
20772

73+
1. Make a list of all commits on your current branch since it branched
74+
off from `<upstream>` that do not have an equivalent commit in
75+
`<upstream>`.
76+
2. Check out `<upstream>` with the equivalent of
77+
`git checkout --detach <upstream>`.
78+
3. Replay 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.
81+
4. Update your branch to point to the final commit with the equivalent
82+
of `git checkout -B <branch>`.
20883
209-
git rebase --abort
84+
[NOTE]
85+
When starting the rebase, `ORIG_HEAD` is set to point to the commit at the tip
86+
of the to-be-rebased branch. However, `ORIG_HEAD` is not guaranteed to still
87+
point to that commit at the end of the rebase if other commands that change
88+
`ORIG_HEAD` (like `git reset`) are used during the rebase. The previous branch
89+
tip, however, is accessible using the reflog of the current branch (i.e. `@{1}`,
90+
see linkgit:gitrevisions[7].
21091

21192
MODE OPTIONS
21293
------------
@@ -253,6 +134,8 @@ As a special case, you may use "A\...B" as a shortcut for the
253134
merge base of A and B if there is exactly one merge base. You can
254135
leave out at most one of A and B, in which case it defaults to HEAD.
255136

137+
See TRANSPLANTING A TOPIC BRANCH WITH --ONTO below for examples.
138+
256139
--keep-base::
257140
Set the starting point at which to create the new commits to the
258141
merge base of `<upstream>` and `<branch>`. Running
@@ -1031,6 +914,91 @@ consistent (they compile, pass the testsuite, etc.) you should use
1031914
after each commit, test, and amend the commit if fixes are necessary.
1032915
1033916
917+
TRANSPLANTING A TOPIC BRANCH WITH --ONTO
918+
----------------------------------------
919+
920+
Here is how you would transplant a topic branch based on one
921+
branch to another, to pretend that you forked the topic branch
922+
from the latter branch, using `rebase --onto`.
923+
924+
First let's assume your 'topic' is based on branch 'next'.
925+
For example, a feature developed in 'topic' depends on some
926+
functionality which is found in 'next'.
927+
928+
------------
929+
o---o---o---o---o master
930+
\
931+
o---o---o---o---o next
932+
\
933+
o---o---o topic
934+
------------
935+
936+
We want to make 'topic' forked from branch 'master'; for example,
937+
because the functionality on which 'topic' depends was merged into the
938+
more stable 'master' branch. We want our tree to look like this:
939+
940+
------------
941+
o---o---o---o---o master
942+
| \
943+
| o'--o'--o' topic
944+
\
945+
o---o---o---o---o next
946+
------------
947+
948+
We can get this using the following command:
949+
950+
git rebase --onto master next topic
951+
952+
953+
Another example of --onto option is to rebase part of a
954+
branch. If we have the following situation:
955+
956+
------------
957+
H---I---J topicB
958+
/
959+
E---F---G topicA
960+
/
961+
A---B---C---D master
962+
------------
963+
964+
then the command
965+
966+
git rebase --onto master topicA topicB
967+
968+
would result in:
969+
970+
------------
971+
H'--I'--J' topicB
972+
/
973+
| E---F---G topicA
974+
|/
975+
A---B---C---D master
976+
------------
977+
978+
This is useful when topicB does not depend on topicA.
979+
980+
A range of commits could also be removed with rebase. If we have
981+
the following situation:
982+
983+
------------
984+
E---F---G---H---I---J topicA
985+
------------
986+
987+
then the command
988+
989+
git rebase --onto topicA~5 topicA~3 topicA
990+
991+
would result in the removal of commits F and G:
992+
993+
------------
994+
E---H'---I'---J' topicA
995+
------------
996+
997+
This is useful if F and G were flawed in some way, or should not be
998+
part of topicA. Note that the argument to `--onto` and the `<upstream>`
999+
parameter can be any valid commit-ish.
1000+
1001+
10341002
RECOVERING FROM UPSTREAM REBASE
10351003
-------------------------------
10361004

0 commit comments

Comments
 (0)