Skip to content

Commit cd67e4d

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d25430c commit cd67e4d

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

Documentation/config.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,13 @@ branch.<name>.mergeoptions::
346346
option values containing whitespace characters are currently not
347347
supported.
348348

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+
349356
clean.requireForce::
350357
A boolean to make git-clean do nothing unless given -f
351358
or -n. Defaults to true.

Documentation/git-pull.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ include::urls-remotes.txt[]
3333

3434
include::merge-strategies.txt[]
3535

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+
3646
DEFAULT BEHAVIOUR
3747
-----------------
3848

git-pull.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ test -z "$(git ls-files -u)" ||
1717
die "You are in the middle of a conflicted merge."
1818

1919
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)
2023
while :
2124
do
2225
case "$1" in
@@ -52,6 +55,12 @@ do
5255
esac
5356
strategy_args="${strategy_args}-s $strategy "
5457
;;
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+
;;
5564
-h|--h|--he|--hel|--help)
5665
usage
5766
;;
@@ -95,7 +104,6 @@ merge_head=$(sed -e '/ not-for-merge /d' \
95104

96105
case "$merge_head" in
97106
'')
98-
curr_branch=$(git symbolic-ref -q HEAD)
99107
case $? in
100108
0) ;;
101109
1) echo >&2 "You are not currently on a branch; you must explicitly"
@@ -142,5 +150,6 @@ then
142150
fi
143151

144152
merge_name=$(git fmt-merge-msg <"$GIT_DIR/FETCH_HEAD") || exit
153+
test true = "$rebase" && exec git-rebase $merge_head
145154
exec git-merge $no_summary $no_commit $squash $no_ff $strategy_args \
146155
"$merge_name" HEAD $merge_head

t/t5520-pull.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,26 @@ test_expect_success 'the default remote . should not break explicit pull' '
5353
test `cat file` = modified
5454
'
5555

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+
5678
test_done

0 commit comments

Comments
 (0)