@@ -186,14 +186,15 @@ static int error_dirty_index(struct replay_opts *opts)
186186 return -1 ;
187187}
188188
189- static int fast_forward_to (const unsigned char * to , const unsigned char * from )
189+ static int fast_forward_to (const unsigned char * to , const unsigned char * from ,
190+ int unborn )
190191{
191192 struct ref_lock * ref_lock ;
192193
193194 read_cache ();
194195 if (checkout_fast_forward (from , to , 1 ))
195196 exit (1 ); /* the callee should have complained already */
196- ref_lock = lock_any_ref_for_update ("HEAD" , from , 0 );
197+ ref_lock = lock_any_ref_for_update ("HEAD" , unborn ? null_sha1 : from , 0 );
197198 return write_ref_sha1 (ref_lock , to , "cherry-pick" );
198199}
199200
@@ -390,7 +391,7 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
390391 struct commit_message msg = { NULL , NULL , NULL , NULL , NULL };
391392 char * defmsg = NULL ;
392393 struct strbuf msgbuf = STRBUF_INIT ;
393- int res ;
394+ int res , unborn = 0 ;
394395
395396 if (opts -> no_commit ) {
396397 /*
@@ -402,9 +403,10 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
402403 if (write_cache_as_tree (head , 0 , NULL ))
403404 die (_ ("Your index file is unmerged." ));
404405 } else {
405- if (get_sha1 ("HEAD" , head ))
406- return error (_ ("You do not have a valid HEAD" ));
407- if (index_differs_from ("HEAD" , 0 ))
406+ unborn = get_sha1 ("HEAD" , head );
407+ if (unborn )
408+ hashcpy (head , EMPTY_TREE_SHA1_BIN );
409+ if (index_differs_from (unborn ? EMPTY_TREE_SHA1_HEX : "HEAD" , 0 ))
408410 return error_dirty_index (opts );
409411 }
410412 discard_cache ();
@@ -435,8 +437,10 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
435437 else
436438 parent = commit -> parents -> item ;
437439
438- if (opts -> allow_ff && parent && !hashcmp (parent -> object .sha1 , head ))
439- return fast_forward_to (commit -> object .sha1 , head );
440+ if (opts -> allow_ff &&
441+ ((parent && !hashcmp (parent -> object .sha1 , head )) ||
442+ (!parent && unborn )))
443+ return fast_forward_to (commit -> object .sha1 , head , unborn );
440444
441445 if (parent && parse_commit (parent ) < 0 )
442446 /* TRANSLATORS: The first %s will be "revert" or
0 commit comments