git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 3/3] builtin/checkout: DWIM with -f -b

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 21, 2010, 05:15 UTC
Message-ID
<7vpqzlrmo4.fsf@alter.siamese.dyndns.org>
In-Reply-To
<AANLkTik3QZBpOjwZHGwIXphXnaHLaBV4pUSb1OVAau9V@mail.gmail.com>
Tay Ray Chuan <rctay89@gmail.com> writes:
Show 11 quoted lines
>> Yes; I thought the implication of "-f" to be destructive would be a
>> justification enough, but I agree with you that conflating the two may be
>> a bad idea.  When a user says "git checkout -f -b jch" after seeing the
>> command without "-f" fail due to existing "jch", it is quite clear that
>> the user wants to clobber the history of existing "jch" branch (why else
>> would he giving "-f"), but it is not a justification to clobber local
>> changes he has in the index and the work tree.
>
> How about doing
>
>   git checkout -f -f -b <branch>

That is not the problem I want you to solve. What your patch does solves only half of my issue as Peff pointed out (it lets me reuse the name of the existing branch for an unrelated history, but it loses the local changes I may have in my working tree). IOW, I want to start <branch> from scratch, based on an half-done work I started from my current branch in my work tree.

That is what "git checkout -b <branch>" usually gives us, if the name <branch> is not in use.

I think you were suggesting not to nuke local change with a single -f only when -b is also in use, but I think that makes things even more confusing to the users. Sometimes a single -f discards local changes, but some other times you would need double -f. That will lead to insanity.

Perhaps we would want another option that is similar to -b but lets us discard the named branch if it exists. Let's call it tentatively -B.

 * git checkout -b <branch> [<start-point>]
   - starts a new branch <branch> at <start-point> commit;
   - attempts three-way merge to keep the local change forward while doing
     so, but aborts if it needs a real file-level merge;
   - complains and aborts if <branch> already exists.
   When -f is used, instead of attempting three-way merge to keep the local
   changes, it discards them.
   When -m is used, instead of failing when it needs a real file-level merge,
   it will attempt CVS/SVN "update" style merge (and can cause conflicts).
   When -B is used instead of -b, it does the three-way merge and the usual
   rejection upon a file-level merge unless -m is given.  If it can go ahead,
   but <branch> already exists, then the existing <branch> is discarded and
   replaced with the <start-point> commit.

Something like that would reduce the confusion factor and would help the "recreate an existing branch from scratch" (or "reuse the name of an existing branch") workflow a bit.

Previous: Tay Ray ChuanNext: Tay Ray Chuan
Message 12 of 29 in “DWIM with "git checkout -f -b"”
  1. 0/3 DWIM with "git checkout -f -b"Tay Ray Chuan, Jun 20, 2010
  2. 1/3 add tests for checkout -bTay Ray Chuan, Jun 20, 2010
  3. 2/3 t2018-checkout-branch.sh: show that checkout -f -b doesn't DWIMTay Ray Chuan, Jun 20, 2010
  4. 3/3 builtin/checkout: DWIM with -f -bTay Ray Chuan, Jun 20, 2010
  5. Junio C HamanoJun 20, 2010
  6. Jeff KingJun 20, 2010
  7. Clemens BuchacherJun 20, 2010
  8. Junio C HamanoJun 20, 2010
  9. Jeff KingJun 20, 2010
  10. Junio C HamanoJun 21, 2010
  11. Tay Ray ChuanJun 21, 2010
  12. Junio C HamanoJun 21, 2010
  13. 0/3 teach git-checkout -B WAS builtin/checkout: DWIM with -f -bTay Ray Chuan, Jun 21, 2010
  14. 1/3 add tests for checkout -bTay Ray Chuan, Jun 21, 2010
  15. 2/3 builtin/checkout: change -b from an OPTION_STRING to a OPTION_SET_INTTay Ray Chuan, Jun 21, 2010
  16. 3/3 builtin/checkout: learn -BTay Ray Chuan, Jun 21, 2010
  17. Junio C HamanoJun 23, 2010
  18. Tay Ray ChuanJun 23, 2010
  19. Erick MattosJun 21, 2010
  20. Tay Ray ChuanJun 23, 2010
  21. Erick MattosJun 23, 2010
  22. Tay Ray ChuanJun 21, 2010
  23. Erick MattosJun 21, 2010
  24. 0/3 teach git-checkout -B WAS builtin/checkout: DWIM with -f -bTay Ray Chuan, Jun 23, 2010
  25. 1/3 add tests for checkout -bTay Ray Chuan, Jun 23, 2010
  26. 2/3 builtin/checkout: reword hint for -bTay Ray Chuan, Jun 23, 2010
  27. 3/3 builtin/checkout: learn -BTay Ray Chuan, Jun 23, 2010
  28. Michel LespinasseJun 21, 2010
  29. Michel LespinasseJun 21, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.