Skip to content

Commit d2db8f7

Browse files
committed
Merge branch 'jk/pull-into-dirty-unborn' into maint
"git pull" into nothing trashed "local changes" that were in the index. * jk/pull-into-dirty-unborn: pull: merge into unborn by fast-forwarding from empty tree pull: update unborn branch tip after index
2 parents 1f101bf + b4dc085 commit d2db8f7

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

git-pull.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,17 @@ case "$merge_head" in
266266
;;
267267
esac
268268

269+
# Pulling into unborn branch: a shorthand for branching off
270+
# FETCH_HEAD, for lazy typers.
269271
if test -z "$orig_head"
270272
then
271-
git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
272-
git read-tree -m -u HEAD || exit 1
273+
# Two-way merge: we claim the index is based on an empty tree,
274+
# and try to fast-forward to HEAD. This ensures we will not
275+
# lose index/worktree changes that the user already made on
276+
# the unborn branch.
277+
empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
278+
git read-tree -m -u $empty_tree $merge_head &&
279+
git update-ref -m "initial pull" HEAD $merge_head "$curr_head"
273280
exit
274281
fi
275282

t/t5520-pull.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,35 @@ test_expect_success 'pulling into void does not overwrite untracked files' '
5757
)
5858
'
5959

60+
test_expect_success 'pulling into void does not overwrite staged files' '
61+
git init cloned-staged-colliding &&
62+
(
63+
cd cloned-staged-colliding &&
64+
echo "alternate content" >file &&
65+
git add file &&
66+
test_must_fail git pull .. master &&
67+
echo "alternate content" >expect &&
68+
test_cmp expect file &&
69+
git cat-file blob :file >file.index &&
70+
test_cmp expect file.index
71+
)
72+
'
73+
74+
75+
test_expect_success 'pulling into void does not remove new staged files' '
76+
git init cloned-staged-new &&
77+
(
78+
cd cloned-staged-new &&
79+
echo "new tracked file" >newfile &&
80+
git add newfile &&
81+
git pull .. master &&
82+
echo "new tracked file" >expect &&
83+
test_cmp expect newfile &&
84+
git cat-file blob :newfile >newfile.index &&
85+
test_cmp expect newfile.index
86+
)
87+
'
88+
6089
test_expect_success 'test . as a remote' '
6190
6291
git branch copy master &&

0 commit comments

Comments
 (0)