Teach 'git pull' about --rebase
When calling 'git pull' with the '--rebase' option, it performs a fetch + rebase instead of a fetch + merge. This behavior is more desirable than fetch + pull when a topic branch is ready to be submitted and needs to be update. fetch + rebase might also be considered a better workflow with shared repositories in any case, or for contributors to a centrally managed repository, such as WINE's. As a convenience, you can set the default behavior for a branch by defining the config variable branch.<name>.rebase, which is interpreted as a bool. This setting can be overridden on the command line by --rebase and --no-rebase. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Nov 28, 2007 at 13:11 UTC
cd67e4d46b122b161f2ad7d943e4ae7aa8df6cf5
4 files changed
+49
-1
Documentation/config.txt
+7
@@ -346,6 +346,13 @@ branch.<name>.mergeoptions::
346
option values containing whitespace characters are currently not
347
supported.
348
349
+branch.<name>.rebase::
350
+ When true, rebase the branch <name> on top of the fetched branch,
351
+ instead of merging the default branch from the default remote.
352
+ *NOTE*: this is a possibly dangerous operation; do *not* use
353
+ it unless you understand the implications (see gitlink:git-rebase[1]
354
+ for details).
355
+
356
clean.requireForce::
357
A boolean to make git-clean do nothing unless given -f
358
or -n. Defaults to true.
Documentation/git-pull.txt
+10
@@ -33,6 +33,16 @@ include::urls-remotes.txt[]
33
34
include::merge-strategies.txt[]
35
36
+\--rebase::
37
+ Instead of a merge, perform a rebase after fetching.
38
+ *NOTE:* This is a potentially _dangerous_ mode of operation.
39
+ It rewrites history, which does not bode well when you
40
+ published that history already. Do *not* use this option
41
+ unless you have read gitlink:git-rebase[1] carefully.
42
+
43
+\--no-rebase::
44
+ Override earlier \--rebase.
45
+
46
DEFAULT BEHAVIOUR
47
-----------------
48
git-pull.sh
+10
-1
@@ -17,6 +17,9 @@ test -z "$(git ls-files -u)" ||
17
die "You are in the middle of a conflicted merge."
18
19
strategy_args= no_summary= no_commit= squash= no_ff=
20
+curr_branch=$(git symbolic-ref -q HEAD)
21
+curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||")
22
+rebase=$(git config --bool branch.$curr_branch_short.rebase)
23
while :
24
do
25
case "$1" in
@@ -52,6 +55,12 @@ do
55
esac
56
strategy_args="${strategy_args}-s $strategy "
57
;;
58
+ -r|--r|--re|--reb|--reba|--rebas|--rebase)
59
+ rebase=true
60
+ ;;
61
+ --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
62
+ rebase=false
63
+ ;;
64
-h|--h|--he|--hel|--help)
65
usage
66
;;
@@ -95,7 +104,6 @@ merge_head=$(sed -e '/ not-for-merge /d' \
104
105
case "$merge_head" in
106
'')
98
- curr_branch=$(git symbolic-ref -q HEAD)
107
case $? in
108
0) ;;
109
1) echo >&2 "You are not currently on a branch; you must explicitly"
@@ -142,5 +150,6 @@ then
150
fi
151
152
merge_name=$(git fmt-merge-msg <"$GIT_DIR/FETCH_HEAD") || exit
153
+test true = "$rebase" && exec git-rebase $merge_head
154
exec git-merge $no_summary $no_commit $squash $no_ff $strategy_args \
155
"$merge_name" HEAD $merge_head
t/t5520-pull.sh
+22
@@ -53,4 +53,26 @@ test_expect_success 'the default remote . should not break explicit pull' '
53
test `cat file` = modified
54
'
55
56
+test_expect_success '--rebase' '
57
+ git branch to-rebase &&
58
+ echo modified again > file &&
59
+ git commit -m file file &&
60
+ git checkout to-rebase &&
61
+ echo new > file2 &&
62
+ git add file2 &&
63
+ git commit -m "new file" &&
64
+ git tag before-rebase &&
65
+ git pull --rebase . copy &&
66
+ test $(git rev-parse HEAD^) = $(git rev-parse copy) &&
67
+ test new = $(git show HEAD:file2)
68
+'
69
+
70
+test_expect_success 'branch.to-rebase.rebase' '
71
+ git reset --hard before-rebase &&
72
+ git config branch.to-rebase.rebase 1 &&
73
+ git pull . copy &&
74
+ test $(git rev-parse HEAD^) = $(git rev-parse copy) &&
75
+ test new = $(git show HEAD:file2)
76
+'
77
+
78
test_done