From 07351d98992ba17ee9249b80f325f7f2c56a2251 Mon Sep 17 00:00:00 2001 From: Eric Sunshine Date: Sun, 6 Sep 2020 20:02:20 -0400 Subject: [PATCH 1/3] git-checkout.txt: document -d short option for --detach `git checkout` learned -d as short option for --detach in 163e3b2975 (switch: add short option for --detach, 2019-03-29) but the documentation was never updated to reflect the change. Signed-off-by: Eric Sunshine Signed-off-by: Junio C Hamano --- Documentation/git-checkout.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt index 5b697eee1b..afa5c11fd3 100644 --- a/Documentation/git-checkout.txt +++ b/Documentation/git-checkout.txt @@ -198,6 +198,7 @@ Use `--no-guess` to disable this. Create the new branch's reflog; see linkgit:git-branch[1] for details. +-d:: --detach:: Rather than checking out a branch to work on it, check out a commit for inspection and discardable experiments. From c670aa47dff9829360cc69174f8dd0d82f096011 Mon Sep 17 00:00:00 2001 From: Eric Sunshine Date: Sun, 6 Sep 2020 20:02:21 -0400 Subject: [PATCH 2/3] worktree: teach `add` to recognize -d as shorthand for --detach Like `git switch` and `git checkout`, `git worktree add` can check out a branch or set up a detached HEAD. However, unlike those other commands, `git worktree add` does not understand -d as shorthand for --detach, which may confound users accustomed to using -d for this purpose. Address this shortcoming by teaching `add` to recognize -d for --detach, thus bringing it in line with the other commands. Signed-off-by: Eric Sunshine Signed-off-by: Junio C Hamano --- Documentation/git-worktree.txt | 1 + builtin/worktree.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt index 6ee6ec7982..d252b6873b 100644 --- a/Documentation/git-worktree.txt +++ b/Documentation/git-worktree.txt @@ -143,6 +143,7 @@ To remove a locked working tree, specify `--force` twice. exists. `-B` overrides this safeguard, resetting `` to ``. +-d:: --detach:: With `add`, detach `HEAD` in the new working tree. See "DETACHED HEAD" in linkgit:git-checkout[1]. diff --git a/builtin/worktree.c b/builtin/worktree.c index 378f332b5d..1737165d2d 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -555,7 +555,7 @@ static int add(int ac, const char **av, const char *prefix) N_("create a new branch")), OPT_STRING('B', NULL, &new_branch_force, N_("branch"), N_("create or reset a branch")), - OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")), + OPT_BOOL('d', "detach", &opts.detach, N_("detach HEAD at named commit")), OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")), OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")), OPT__QUIET(&opts.quiet, N_("suppress progress reporting")), From dccadad736c801e924c7c14e8c6bd1a6e15e70e5 Mon Sep 17 00:00:00 2001 From: Eric Sunshine Date: Sun, 6 Sep 2020 20:02:22 -0400 Subject: [PATCH 3/3] git-worktree.txt: discuss branch-based vs. throwaway worktrees 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 ` automatically creates a new branch. Stating this early in the description, may help newcomers avoid creating branches without realizing they are doing so, and later wondering why `git branch --list` shows branches the user did not intentionally create. Signed-off-by: Eric Sunshine Signed-off-by: Junio C Hamano --- 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 ` automatically creates a +new branch whose name is the final component of ``, 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 `. 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 ` 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