@matheusbsbs / matheus-bagliano / commits / b499549401
Fork 0

Teach rebase the --no-ff option.

For git-rebase.sh, --no-ff is a synonym for --force-rebase. For git-rebase--interactive.sh, --no-ff cherry-picks all the commits in the rebased branch, instead of fast-forwarding over any unchanged commits. --no-ff offers an alternative way to deal with reverted merges. Instead of "reverting the revert" you can use "rebase --no-ff" to recreate the branch with entirely new commits (they're new because at the very least the committer time is different). This obviates the need to revert the reversion, as you can re-merge the new topic branch directly. Added an addendum to revert-a-faulty-merge.txt describing the situation and how to use --no-ff to handle it. Signed-off-by: Marc Branchaud <marcnarc@xiplink.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Marc Branchaud committed Mar 24, 2010 at 16:34 UTC b499549401cb2b1f6c30d09681380fd519938eb0
5 files changed +152 -10
Documentation/git-rebase.txt
+21 -2
@@ -274,9 +274,16 @@ which makes little sense.
274 -f::
275 --force-rebase::
276 Force the rebase even if the current branch is a descendant
277 - of the commit you are rebasing onto. Normally the command will
277 + of the commit you are rebasing onto. Normally non-interactive rebase will
278 exit with the message "Current branch is up to date" in such a
279 situation.
280 + Incompatible with the --interactive option.
281 ++
282 +You may find this (or --no-ff with an interactive rebase) helpful after
283 +reverting a topic branch merge, as this option recreates the topic branch with
284 +fresh commits so it can be remerged successfully without needing to "revert
285 +the reversion" (see the
286 +link:howto/revert-a-faulty-merge.txt[revert-a-faulty-merge How-To] for details).
287
288 --ignore-whitespace::
289 --whitespace=<option>::
@@ -316,7 +323,19 @@ which makes little sense.
323 commit to be modified, and change the action of the moved
324 commit from `pick` to `squash` (or `fixup`).
325 +
319 -This option is only valid when '--interactive' option is used.
326 +This option is only valid when the '--interactive' option is used.
327 +
328 +--no-ff::
329 + With --interactive, cherry-pick all rebased commits instead of
330 + fast-forwarding over the unchanged ones. This ensures that the
331 + entire history of the rebased branch is composed of new commits.
332 ++
333 +Without --interactive, this is a synonym for --force-rebase.
334 ++
335 +You may find this helpful after reverting a topic branch merge, as this option
336 +recreates the topic branch with fresh commits so it can be remerged
337 +successfully without needing to "revert the reversion" (see the
338 +link:howto/revert-a-faulty-merge.txt[revert-a-faulty-merge How-To] for details).
339
340 include::merge-strategies.txt[]
341
Documentation/howto/revert-a-faulty-merge.txt
+90
@@ -142,6 +142,8 @@ different resolution strategies:
142 revert of a merge was rebuilt from scratch (i.e. rebasing and fixing,
143 as you seem to have interpreted), then re-merging the result without
144 doing anything else fancy would be the right thing to do.
145 + (See the ADDENDUM below for how to rebuild a branch from scratch
146 + without changing its original branching-off point.)
147
148 However, there are things to keep in mind when reverting a merge (and
149 reverting such a revert).
@@ -177,3 +179,91 @@ the answer is: "oops, I really shouldn't have merged it, because it wasn't
179 ready yet, and I really need to undo _all_ of the merge"). So then you
180 really should revert the merge, but when you want to re-do the merge, you
181 now need to do it by reverting the revert.
182 +
183 +ADDENDUM
184 +
185 +Sometimes you have to rewrite one of a topic branch's commits *and* you can't
186 +change the topic's branching-off point. Consider the following situation:
187 +
188 + P---o---o---M---x---x---W---x
189 + \ /
190 + A---B---C
191 +
192 +where commit W reverted commit M because it turned out that commit B was wrong
193 +and needs to be rewritten, but you need the rewritten topic to still branch
194 +from commit P (perhaps P is a branching-off point for yet another branch, and
195 +you want be able to merge the topic into both branches).
196 +
197 +The natural thing to do in this case is to checkout the A-B-C branch and use
198 +"rebase -i P" to change commit B. However this does not rewrite commit A,
199 +because "rebase -i" by default fast-forwards over any initial commits selected
200 +with the "pick" command. So you end up with this:
201 +
202 + P---o---o---M---x---x---W---x
203 + \ /
204 + A---B---C <-- old branch
205 + \
206 + B'---C' <-- naively rewritten branch
207 +
208 +To merge A-B'-C' into the mainline branch you would still have to first revert
209 +commit W in order to pick up the changes in A, but then it's likely that the
210 +changes in B' will conflict with the original B changes re-introduced by the
211 +reversion of W.
212 +
213 +However, you can avoid these problems if you recreate the entire branch,
214 +including commit A:
215 +
216 + A'---B'---C' <-- completely rewritten branch
217 + /
218 + P---o---o---M---x---x---W---x
219 + \ /
220 + A---B---C
221 +
222 +You can merge A'-B'-C' into the mainline branch without worrying about first
223 +reverting W. Mainline's history would look like this:
224 +
225 + A'---B'---C'------------------
226 + / \
227 + P---o---o---M---x---x---W---x---M2
228 + \ /
229 + A---B---C
230 +
231 +But if you don't actually need to change commit A, then you need some way to
232 +recreate it as a new commit with the same changes in it. The rebase commmand's
233 +--no-ff option provides a way to do this:
234 +
235 + $ git rebase [-i] --no-ff P
236 +
237 +The --no-ff option creates a new branch A'-B'-C' with all-new commits (all the
238 +SHA IDs will be different) even if in the interactive case you only actually
239 +modify commit B. You can then merge this new branch directly into the mainline
240 +branch and be sure you'll get all of the branch's changes.
241 +
242 +You can also use --no-ff in cases where you just add extra commits to the topic
243 +to fix it up. Let's revisit the situation discussed at the start of this howto:
244 +
245 + P---o---o---M---x---x---W---x
246 + \ /
247 + A---B---C----------------D---E <-- fixed-up topic branch
248 +
249 +At this point, you can use --no-ff to recreate the topic branch:
250 +
251 + $ git checkout E
252 + $ git rebase --no-ff P
253 +
254 +yielding
255 +
256 + A'---B'---C'------------D'---E' <-- recreated topic branch
257 + /
258 + P---o---o---M---x---x---W---x
259 + \ /
260 + A---B---C----------------D---E
261 +
262 +You can merge the recreated branch into the mainline without reverting commit W,
263 +and mainline's history will look like this:
264 +
265 + A'---B'---C'------------D'---E'
266 + / \
267 + P---o---o---M---x---x---W---x---M2
268 + \ /
269 + A---B---C
git-rebase--interactive.sh
+7 -2
@@ -20,6 +20,7 @@ v,verbose display a diffstat of what changed upstream
20 onto= rebase onto given branch instead of upstream
21 p,preserve-merges try to recreate merges instead of ignoring them
22 s,strategy= use the given merge strategy
23 +no-ff cherry-pick all commits, even if unchanged
24 m,merge always used (no-op)
25 i,interactive always used (no-op)
26 Actions:
@@ -103,6 +104,7 @@ VERBOSE=
104 OK_TO_SKIP_PRE_REBASE=
105 REBASE_ROOT=
106 AUTOSQUASH=
107 +NEVER_FF=
108
109 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
110 mark the corrected paths with 'git add <paths>', and
@@ -222,7 +224,7 @@ do_with_author () {
224 }
225
226 pick_one () {
225 - no_ff=
227 + no_ff=$NEVER_FF
228 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
229 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
230 test -d "$REWRITTEN" &&
@@ -742,6 +744,9 @@ first and then run 'git rebase --continue' again."
744 -i)
745 # yeah, we know
746 ;;
747 + --no-ff)
748 + NEVER_FF=t
749 + ;;
750 --root)
751 REBASE_ROOT=t
752 ;;
@@ -927,7 +932,7 @@ EOF
932 has_action "$TODO" ||
933 die_abort "Nothing to do"
934
930 - test -d "$REWRITTEN" || skip_unnecessary_picks
935 + test -d "$REWRITTEN" || test -n "$NEVER_FF" || skip_unnecessary_picks
936
937 git update-ref ORIG_HEAD $HEAD
938 output git checkout $ONTO && do_rest
git-rebase.sh
+2 -2
@@ -3,7 +3,7 @@
3 # Copyright (c) 2005 Junio C Hamano.
4 #
5
6 -USAGE='[--interactive | -i] [-v] [--force-rebase | -f] [--onto <newbase>] [<upstream>|--root] [<branch>] [--quiet | -q]'
6 +USAGE='[--interactive | -i] [-v] [--force-rebase | -f] [--no-ff] [--onto <newbase>] [<upstream>|--root] [<branch>] [--quiet | -q]'
7 LONG_USAGE='git-rebase replaces <branch> with a new branch of the
8 same name. When the --onto option is provided the new branch starts
9 out with a HEAD equal to <newbase>, otherwise it is equal to <upstream>
@@ -347,7 +347,7 @@ do
347 --root)
348 rebase_root=t
349 ;;
350 - -f|--f|--fo|--for|--forc|force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase)
350 + -f|--f|--fo|--for|--forc|force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase|--no-ff)
351 force_rebase=t
352 ;;
353 --rerere-autoupdate|--no-rerere-autoupdate)
t/t3404-rebase-interactive.sh
+32 -4
@@ -22,12 +22,18 @@ set_fake_editor
22 # | \
23 # | F - G - H (branch1)
24 # | \
25 -# \ I (branch2)
26 -# \
27 -# J - K - L - M (no-conflict-branch)
25 +# |\ I (branch2)
26 +# | \
27 +# | J - K - L - M (no-conflict-branch)
28 +# \
29 +# N - O - P (no-ff-branch)
30 #
31 # where A, B, D and G all touch file1, and one, two, three, four all
32 # touch file "conflict".
33 +#
34 +# WARNING: Modifications to the initial repository can change the SHA ID used
35 +# in the expect2 file for the 'stop on conflicting pick' test.
36 +
37
38 test_expect_success 'setup' '
39 test_commit A file1 &&
@@ -48,6 +54,11 @@ test_expect_success 'setup' '
54 done &&
55 git checkout -b no-conflict-branch A &&
56 for n in J K L M
57 + do
58 + test_commit $n file$n
59 + done &&
60 + git checkout -b no-ff-branch A &&
61 + for n in N O P
62 do
63 test_commit $n file$n
64 done
@@ -113,7 +124,7 @@ cat > expect2 << EOF
124 D
125 =======
126 G
116 ->>>>>>> 51047de... G
127 +>>>>>>> 5d18e54... G
128 EOF
129
130 test_expect_success 'stop on conflicting pick' '
@@ -553,4 +564,21 @@ test_expect_success 'reword' '
564 git show HEAD~2 | grep "C changed"
565 '
566
567 +test_tick # Ensure that the rebased commits get a different timestamp.
568 +test_expect_success 'always cherry-pick with --no-ff' '
569 + git checkout no-ff-branch &&
570 + git tag original-no-ff-branch &&
571 + git rebase -i --no-ff A &&
572 + touch empty &&
573 + for p in 0 1 2
574 + do
575 + test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
576 + git diff HEAD~$p original-no-ff-branch~$p > out &&
577 + test_cmp empty out
578 + done &&
579 + test $(git rev-parse HEAD~3) = $(git rev-parse original-no-ff-branch~3) &&
580 + git diff HEAD~3 original-no-ff-branch~3 > out &&
581 + test_cmp empty out
582 +'
583 +
584 test_done