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

Re: Git checkout multiple options issue

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 28, 2019, 17:29 UTC
Message-ID
<xmqqtvhsbsia.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<20190128165515.GA25995@alpha>
Kevin Daudt <me@ikke.info> writes:
Show 16 quoted lines
> On Mon, Jan 28, 2019 at 10:20:02AM -0500, Randall S. Becker wrote:
>> On January 28, 2019 9:25, COLLOMB Joris wrote:
>> ...
>> > git checkout -f -b "branch_name"
>> > gives me " Fatal: A branch named 'branch_name' already exists."
>> 
>> Once the branch is created, you can't force its creation, because it is
>> already created. Just
>> 
>> git checkout "branch_name"
>> 
>> is sufficient at this point. git is correct to complain that you are trying
>> to create a branch that already exists.
>
> git checkout -B exists, which does exactly that: force create a branch
> at a new positon if it already exists.
Correct.

"git checkout -b/-B" is for lazy people to perform two things, i.e. "creating a branch" and "checking the branch out", at the same time.

If we did not allow "git checkout -b newbranch othercommit" and only allowed "git checkout -b newbranch", i.e. to start a branch at the current HEAD, then we could make "git checkout -f -b newbranch" to mean "git checkout -B newbranch", because creating a branch at HEAD and checking it out will not have a risk of overwriting local changes, i.e. no need to force the "checking the branch out" part, so the "--force" option can only mean "force creating the branch".

But because the new branch can begin at the commit whose tree differs at a path that has local modifications, there needs a way to tell the command which part, either "creating a branch" or "checking the branch out", is being forced. Hence, "-f" tells the "checking out the branch" part to do the forcing (e.g. allowing local changes to be overwritten), and "creating a branch" part is told to force with the distinction between "-b" and "-B".

Previous: Kevin DaudtNext: COLLOMB Joris -EXT
Message 6 of 8 in “Git checkout multiple options issue”
  1. COLLOMB Joris -EXTJan 28, 2019
  2. Randall S. BeckerJan 28, 2019
  3. COLLOMB Joris -EXTJan 28, 2019
  4. Randall S. BeckerJan 28, 2019
  5. Kevin DaudtJan 28, 2019
  6. Junio C HamanoJan 28, 2019
  7. COLLOMB Joris -EXTJan 28, 2019
  8. Randall S. BeckerJan 28, 2019

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.