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

[PATCH 3/3] git-worktree.txt: discuss branch-based vs. throwaway worktrees

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Sep 4, 2020, 07:07 UTC
Message-ID
<20200904070703.47898-4-sunshine@sunshineco.com>
In-Reply-To
<20200904070703.47898-1-sunshine@sunshineco.com>

By default, "git worktree add" creates a new worktree associated with a particular branch (which may have been created automatically if not specified explicitly on the command-line). It is also convenient to create throwaway worktrees not associated with any branch, which can be handy when making experimental changes or doing testing. However, the latter use-case may not be obvious to newcomers since the high-level description of worktrees talks only about checking out "more than one branch at a time". Therefore, enhance the description to to discuss both use-cases.

A secondary goal of highlighting the distinction between branch-based and throwaway worktrees is to help newcomers understand that the simplest form `git worktree add <path>` automatically creates a new branch. Stating this early in the description, may help newcomers avoid creating unwanted new branches without being aware that they are doing so, and later wondering why `git branch --list` shows branches the user did not intentionally create.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 Documentation/git-worktree.txt | 12 ++++++++++++
 1 file changed, 12 insertions(+)
diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index d252b6873b..1449491c1b 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -31,6 +31,18 @@ A repository has one main working tree (if it's not a
 bare repository) and zero or more linked working trees. When you are done
 with a linked working tree, remove it with `git worktree remove`.
 
+In its simplest form, `git worktree add <path>` automatically creates a
+new branch whose name is the final component of `<path>`, which is
+convenient if you plan to work on a new topic. For instance, `git
+worktree add ../hotfix` creates new branch `hotfix` and checks it out at
+path `../hotfix`. To instead work on an existing branch in a new working
+tree, use `git worktree add <path> <branch>`. On the other hand, if you
+just plan to make some experimental changes or do testing without
+disturbing existing development, it is often convenient to create a
+'throwaway' working tree not associated with any branch. For instance,
+`git worktree add -d <path>` creates a new working tree with a detached
+`HEAD` at the same commit as the current branch.
+
 If a working tree is deleted without using `git worktree remove`, then
 its associated administrative files, which reside in the repository
 (see "DETAILS" below), will eventually be removed automatically (see
-- 
2.28.0.618.gf4bc123cb7
Previous: Eric SunshineNext: Junio C Hamano
Message 4 of 9 in “worktree: add -d shorthand for --detach & improve documentation”
  1. 0/3 worktree: add -d shorthand for --detach & improve documentationEric Sunshine, Sep 4, 2020
  2. 2/3 worktree: add -d short option for --detachEric Sunshine, Sep 4, 2020
  3. 1/3 git-checkout.txt: document -d short option for --detachEric Sunshine, Sep 4, 2020
  4. 3/3 git-worktree.txt: discuss branch-based vs. throwaway worktreesEric Sunshine, Sep 4, 2020
  5. Junio C HamanoSep 6, 2020
  6. 0/3 worktree: teach `add` -d for --detach & improve documentationEric Sunshine, Sep 7, 2020
  7. 2/3 worktree: teach `add` to recognize -d as shorthand for --detachEric Sunshine, Sep 7, 2020
  8. 1/3 git-checkout.txt: document -d short option for --detachEric Sunshine, Sep 7, 2020
  9. 3/3 git-worktree.txt: discuss branch-based vs. throwaway worktreesEric Sunshine, Sep 7, 2020

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.