@matheusbsbs / matheus-bagliano / commits / 7d0c68871a
Fork 0

git-merge --squash

Some people tend to do many little commits on a topic branch, recording all the trials and errors, and when the topic is reasonably cooked well, would want to record the net effect of the series as one commit on top of the mainline, removing the cruft from the history. The topic is then abandoned or forked off again from that point at the mainline. The barebone porcelainish that comes with core git tools does not officially support such operation, but you can fake it by using "git pull --no-merge" when such a topic branch is not a strict superset of the mainline, like this: git checkout mainline git pull --no-commit . that-topic-branch : fix conflicts if any rm -f .git/MERGE_HEAD git commit -a -m 'consolidated commit log message' git branch -f that-topic-branch ;# now fully merged This however does not work when the topic branch is a fast forward of the mainline, because normal "git pull" will never create a merge commit in such a case, and there is nothing special --no-commit could do to begin with. This patch introduces a new option, --squash, to support such a workflow officially in both fast-forward case and true merge case. The user-level operation would be the same in both cases: git checkout mainline git pull --squash . that-topic-branch : fix conflicts if any -- naturally, there would be : no conflict if fast forward. git commit -a -m 'consolidated commit log message' git branch -f that-topic-branch ;# now fully merged When the current branch is already up-to-date with respect to the other branch, there truly is nothing to do, so the new option does not have any effect. This was brought up in #git IRC channel recently. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jun 23, 2006 at 01:37 UTC 7d0c68871a86df0654454df047458836afa13129
5 files changed +72 -24
Documentation/merge-options.txt
+8
@@ -6,6 +6,14 @@
6 not autocommit, to give the user a chance to inspect and
7 further tweak the merge result before committing.
8
9 +--squash::
10 + Produce the working tree and index state as if a real
11 + merge happened, but do not actually make a commit or
12 + move the `HEAD`, nor record `$GIT_DIR/MERGE_HEAD` to
13 + cause the next `git commit` command to create a merge
14 + commit. This allows you to create a single commit on
15 + top of the current branch whose effect is the same as
16 + merging another branch (or more in case of an octopus).
17
18 -s <strategy>, \--strategy=<strategy>::
19 Use the given merge strategy; can be supplied more than
git-commit.sh
+5 -2
@@ -566,6 +566,9 @@ then
566 elif test -f "$GIT_DIR/MERGE_HEAD" && test -f "$GIT_DIR/MERGE_MSG"
567 then
568 cat "$GIT_DIR/MERGE_MSG"
569 +elif test -f "$GIT_DIR/SQUASH_MSG"
570 +then
571 + cat "$GIT_DIR/SQUASH_MSG"
572 fi | git-stripspace >"$GIT_DIR"/COMMIT_EDITMSG
573
574 case "$signoff" in
@@ -663,7 +666,7 @@ else
666 fi
667 if [ "$?" != "0" -a ! -f "$GIT_DIR/MERGE_HEAD" -a -z "$amend" ]
668 then
666 - rm -f "$GIT_DIR/COMMIT_EDITMSG"
669 + rm -f "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
670 run_status
671 exit 1
672 fi
@@ -729,7 +732,7 @@ else
732 false
733 fi
734 ret="$?"
732 -rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG"
735 +rm -f "$GIT_DIR/COMMIT_MSG" "$GIT_DIR/COMMIT_EDITMSG" "$GIT_DIR/SQUASH_MSG"
736 if test -d "$GIT_DIR/rr-cache"
737 then
738 git-rerere
git-merge.sh
+53 -19
@@ -3,8 +3,7 @@
3 # Copyright (c) 2005 Junio C Hamano
4 #
5
6 -
7 -USAGE='[-n] [--no-commit] [-s <strategy>]... <merge-message> <head> <remote>+'
6 +USAGE='[-n] [--no-commit] [--squash] [-s <strategy>]... <merge-message> <head> <remote>+'
7 . git-sh-setup
8
9 LF='
@@ -42,20 +41,49 @@ restorestate() {
41 fi
42 }
43
44 +finish_up_to_date () {
45 + case "$squash" in
46 + t)
47 + echo "$1 (nothing to squash)" ;;
48 + '')
49 + echo "$1" ;;
50 + esac
51 + dropsave
52 +}
53 +
54 +squash_message () {
55 + echo Squashed commit of the following:
56 + echo
57 + git-log --no-merges ^"$head" $remote
58 +}
59 +
60 finish () {
61 test '' = "$2" || echo "$2"
47 - case "$merge_msg" in
48 - '')
49 - echo "No merge message -- not updating HEAD"
62 + case "$squash" in
63 + t)
64 + echo "Squash commit -- not updating HEAD"
65 + squash_message >"$GIT_DIR/SQUASH_MSG"
66 ;;
51 - *)
52 - git-update-ref HEAD "$1" "$head" || exit 1
67 + '')
68 + case "$merge_msg" in
69 + '')
70 + echo "No merge message -- not updating HEAD"
71 + ;;
72 + *)
73 + git-update-ref HEAD "$1" "$head" || exit 1
74 + ;;
75 + esac
76 ;;
77 esac
55 -
56 - case "$no_summary" in
78 + case "$1" in
79 '')
58 - git-diff-tree --stat --summary -M "$head" "$1"
80 + ;;
81 + ?*)
82 + case "$no_summary" in
83 + '')
84 + git-diff-tree --stat --summary -M "$head" "$1"
85 + ;;
86 + esac
87 ;;
88 esac
89 }
@@ -66,6 +94,8 @@ do
94 -n|--n|--no|--no-|--no-s|--no-su|--no-sum|--no-summ|\
95 --no-summa|--no-summar|--no-summary)
96 no_summary=t ;;
97 + --sq|--squ|--squa|--squas|--squash)
98 + squash=t no_commit=t ;;
99 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
100 no_commit=t ;;
101 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
@@ -152,8 +182,7 @@ f,*)
182 ?,1,"$1",*)
183 # If head can reach all the merge then we are up to date.
184 # but first the most common case of merging one remote.
155 - echo "Already up-to-date."
156 - dropsave
185 + finish_up_to_date "Already up-to-date."
186 exit 0
187 ;;
188 ?,1,"$head",*)
@@ -205,8 +234,7 @@ f,*)
234 done
235 if test "$up_to_date" = t
236 then
208 - echo "Already up-to-date. Yeeah!"
209 - dropsave
237 + finish_up_to_date "Already up-to-date. Yeeah!"
238 exit 0
239 fi
240 ;;
@@ -310,11 +338,17 @@ case "$best_strategy" in
338 git-merge-$best_strategy $common -- "$head_arg" "$@"
339 ;;
340 esac
313 -for remote
314 -do
315 - echo $remote
316 -done >"$GIT_DIR/MERGE_HEAD"
317 -echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
341 +
342 +if test "$squash" = t
343 +then
344 + finish
345 +else
346 + for remote
347 + do
348 + echo $remote
349 + done >"$GIT_DIR/MERGE_HEAD"
350 + echo "$merge_msg" >"$GIT_DIR/MERGE_MSG"
351 +fi
352
353 if test "$merge_was_ok" = t
354 then
git-pull.sh
+5 -2
@@ -8,7 +8,7 @@ USAGE='[-n | --no-summary] [--no-commit] [-s strategy]... [<fetch-options>] <rep
8 LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.'
9 . git-sh-setup
10
11 -strategy_args= no_summary= no_commit=
11 +strategy_args= no_summary= no_commit= squash=
12 while case "$#,$1" in 0) break ;; *,-*) ;; *) break ;; esac
13 do
14 case "$1" in
@@ -17,6 +17,8 @@ do
17 no_summary=-n ;;
18 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
19 no_commit=--no-commit ;;
20 + --sq|--squ|--squa|--squas|--squash)
21 + squash=--squash ;;
22 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
23 --strateg=*|--strategy=*|\
24 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
@@ -100,4 +102,5 @@ case "$strategy_args" in
102 esac
103
104 merge_name=$(git-fmt-merge-msg <"$GIT_DIR/FETCH_HEAD") || exit
103 -git-merge $no_summary $no_commit $strategy_args "$merge_name" HEAD $merge_head
105 +git-merge $no_summary $no_commit $squash $strategy_args \
106 + "$merge_name" HEAD $merge_head
git-reset.sh
+1 -1
@@ -61,4 +61,4 @@ case "$reset_type" in
61 ;;
62 esac
63
64 -rm -f "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/rr-cache/MERGE_RR"
64 +rm -f "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/rr-cache/MERGE_RR" "$GIT_DIR/SQUASH_MSG"