Skip to content

Commit 19a7fcb

Browse files
peffgitster
authored andcommitted
allow pull --rebase on branch yet to be born
When doing a "pull --rebase", we check to make sure that the index and working tree are clean. The index-clean check compares the index against HEAD. The test erroneously reports dirtiness if we don't have a HEAD yet. In such an "unborn branch" case, by definition, a non-empty index won't be based on whatever we are pulling down from the remote, and will lose the local change. Just check if $GIT_DIR/index exists and error out. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent efd1796 commit 19a7fcb

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

git-pull.sh

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,19 @@ error_on_no_merge_candidates () {
119119
}
120120

121121
test true = "$rebase" && {
122-
git update-index --ignore-submodules --refresh &&
123-
git diff-files --ignore-submodules --quiet &&
124-
git diff-index --ignore-submodules --cached --quiet HEAD -- ||
125-
die "refusing to pull with rebase: your working tree is not up-to-date"
126-
122+
if ! git rev-parse -q --verify HEAD >/dev/null
123+
then
124+
# On an unborn branch
125+
if test -f "$GIT_DIR/index"
126+
then
127+
die "updating an unborn branch with changes added to the index"
128+
fi
129+
else
130+
git update-index --ignore-submodules --refresh &&
131+
git diff-files --ignore-submodules --quiet &&
132+
git diff-index --ignore-submodules --cached --quiet HEAD -- ||
133+
die "refusing to pull with rebase: your working tree is not up-to-date"
134+
fi
127135
oldremoteref= &&
128136
. git-parse-remote &&
129137
remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&

t/t5520-pull.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,15 @@ test_expect_success 'pull --rebase dies early with dirty working directory' '
149149
150150
'
151151

152+
test_expect_success 'pull --rebase works on branch yet to be born' '
153+
git rev-parse master >expect &&
154+
mkdir empty_repo &&
155+
(cd empty_repo &&
156+
git init &&
157+
git pull --rebase .. master &&
158+
git rev-parse HEAD >../actual
159+
) &&
160+
test_cmp expect actual
161+
'
162+
152163
test_done

0 commit comments

Comments
 (0)