Skip to content

Commit 4dd0469

Browse files
committed
Merge branch 'ja/worktree-orphan'
'git worktree add' learned how to create a worktree based on an orphaned branch with `--orphan`. * ja/worktree-orphan: worktree add: emit warn when there is a bad HEAD worktree add: extend DWIM to infer --orphan worktree add: introduce "try --orphan" hint worktree add: add --orphan flag t2400: add tests to verify --quiet t2400: refactor "worktree add" opt exclusion tests t2400: cleanup created worktree in test worktree add: include -B in usage docs
2 parents 6640c2d + 926c40d commit 4dd0469

File tree

6 files changed

+735
-21
lines changed

6 files changed

+735
-21
lines changed

Documentation/config/advice.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,8 @@ advice.*::
138138
checkout.
139139
diverging::
140140
Advice shown when a fast-forward is not possible.
141+
worktreeAddOrphan::
142+
Advice shown when a user tries to create a worktree from an
143+
invalid reference, to instruct how to create a new orphan
144+
branch instead.
141145
--

Documentation/git-worktree.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SYNOPSIS
1010
--------
1111
[verse]
1212
'git worktree add' [-f] [--detach] [--checkout] [--lock [--reason <string>]]
13-
[-b <new-branch>] <path> [<commit-ish>]
13+
[--orphan] [(-b | -B) <new-branch>] <path> [<commit-ish>]
1414
'git worktree list' [-v | --porcelain [-z]]
1515
'git worktree lock' [--reason <string>] <worktree>
1616
'git worktree move' <worktree> <new-path>
@@ -95,6 +95,16 @@ exist, a new branch based on `HEAD` is automatically created as if
9595
`-b <branch>` was given. If `<branch>` does exist, it will be checked out
9696
in the new worktree, if it's not checked out anywhere else, otherwise the
9797
command will refuse to create the worktree (unless `--force` is used).
98+
+
99+
If `<commit-ish>` is omitted, neither `--detach`, or `--orphan` is
100+
used, and there are no valid local branches (or remote branches if
101+
`--guess-remote` is specified) then, as a convenience, the new worktree is
102+
associated with a new orphan branch named `<branch>` (after
103+
`$(basename <path>)` if neither `-b` or `-B` is used) as if `--orphan` was
104+
passed to the command. In the event the repository has a remote and
105+
`--guess-remote` is used, but no remote or local branches exist, then the
106+
command fails with a warning reminding the user to fetch from their remote
107+
first (or override by using `-f/--force`).
98108

99109
list::
100110

@@ -222,6 +232,10 @@ This can also be set up as the default behaviour by using the
222232
With `prune`, do not remove anything; just report what it would
223233
remove.
224234

235+
--orphan::
236+
With `add`, make the new worktree and index empty, associating
237+
the worktree with a new orphan/unborn branch named `<new-branch>`.
238+
225239
--porcelain::
226240
With `list`, output in an easy-to-parse format for scripts.
227241
This format will remain stable across Git versions and regardless of user

advice.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ static struct {
7878
[ADVICE_SUBMODULES_NOT_UPDATED] = { "submodulesNotUpdated", 1 },
7979
[ADVICE_UPDATE_SPARSE_PATH] = { "updateSparsePath", 1 },
8080
[ADVICE_WAITING_FOR_EDITOR] = { "waitingForEditor", 1 },
81+
[ADVICE_WORKTREE_ADD_ORPHAN] = { "worktreeAddOrphan", 1 },
8182
};
8283

8384
static const char turn_off_instructions[] =

advice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct string_list;
4949
ADVICE_UPDATE_SPARSE_PATH,
5050
ADVICE_WAITING_FOR_EDITOR,
5151
ADVICE_SKIPPED_CHERRY_PICKS,
52+
ADVICE_WORKTREE_ADD_ORPHAN,
5253
};
5354

5455
int git_default_advice_config(const char *var, const char *value);

0 commit comments

Comments
 (0)