path: root/builtin/worktree.c
diff options
authorThomas Gummerer <>2017-11-29 20:04:50 (GMT)
committerJunio C Hamano <>2017-12-06 17:47:35 (GMT)
commit71d6682d8ca9870cbe457cf55c83402c33b41030 (patch)
tree23fe7176c9dc953f64acb55d96b852aded935a57 /builtin/worktree.c
parent4e8533319760c1e9255c56c2059c721286dc8dab (diff)
worktree: add --guess-remote flag to add subcommand
Currently 'git worktree add <path>' creates a new branch named after the basename of the <path>, that matches the HEAD of whichever worktree we were on when calling "git worktree add <path>". It's sometimes useful to have 'git worktree add <path> behave more like the dwim machinery in 'git checkout <new-branch>', i.e. check if the new branch name, derived from the basename of the <path>, uniquely matches the branch name of a remote-tracking branch, and if so check out that branch and set the upstream to the remote-tracking branch. Add a new --guess-remote option that enables exactly that behaviour. Signed-off-by: Thomas Gummerer <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'builtin/worktree.c')
1 files changed, 10 insertions, 0 deletions
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 7021d02..15cb160 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -343,6 +343,7 @@ static int add(int ac, const char **av, const char *prefix)
char *path;
const char *branch;
const char *opt_track = NULL;
+ int guess_remote = 0;
struct option options[] = {
OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
@@ -355,6 +356,8 @@ static int add(int ac, const char **av, const char *prefix)
OPT_PASSTHRU(0, "track", &opt_track, NULL,
N_("set up tracking mode (see git-branch(1))"),
+ OPT_BOOL(0, "guess-remote", &guess_remote,
+ N_("try to match the new branch name with a remote-tracking branch")),
@@ -389,6 +392,13 @@ static int add(int ac, const char **av, const char *prefix)
int n;
const char *s = worktree_basename(path, &n);
opts.new_branch = xstrndup(s, n);
+ if (guess_remote) {
+ struct object_id oid;
+ const char *remote =
+ unique_tracking_name(opts.new_branch, &oid);
+ if (remote)
+ branch = remote;
+ }
if (ac == 2 && !opts.new_branch && !opts.detach) {