From: Harald Nordgren via GitGitGadget Date: Sat, 27 Jun 2026 18:02:23 GMT Subject: [PATCH v3 0/2] branch/push: suggest intended form when remote/branch slip given Message-ID: In-Reply-To: When the repository or upstream argument is a slip like "origin/main" or "origin main", suggest the intended "git push origin main" or "git branch --set-upstream-to=origin/main" form instead of failing with an unrelated error. Changes in v3: * Reworded the branch commit message to also show the second confusing error (the requested upstream branch 'origin' does not exist) for when the operated-on branch does exist. * Converted the new t5529 tests from bare grep/! grep to test_grep to fix the test-lint failures. * Added a brief call-site comment explaining the up-front advice_enabled() gate (skips the lookups when the hint is off, while advise_if_enabled() still prints the disable-message footer). Changes in v2: * Rewrote both commit messages to lead with the intended command, the easy slip, and the resulting error, instead of the terse original. * Gated each suggestion on advice_enabled() up front, so a user who silenced the hint pays no remote/ref lookups and falls through to the original error. Extracted the detection logic into helpers (die_if_repo_looks_like_ref, die_if_upstream_looks_like_remote) so each call site reads as a single guarded line. Harald Nordgren (2): branch: suggest / on upstream slip push: suggest for a slash slip Documentation/config/advice.adoc | 5 +++++ advice.c | 1 + advice.h | 1 + builtin/branch.c | 32 +++++++++++++++++++++++++++ builtin/push.c | 37 ++++++++++++++++++++++++++++++- t/t3200-branch.sh | 38 ++++++++++++++++++++++++++++++++ t/t5529-push-errors.sh | 31 ++++++++++++++++++++++++++ 7 files changed, 144 insertions(+), 1 deletion(-) base-commit: ab776a62a78576513ee121424adb19597fbb7613 Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2331%2FHaraldNordgren%2Fsuggest-remote-branch-slips-v3 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2331/HaraldNordgren/suggest-remote-branch-slips-v3 Pull-Request: https://github.com/git/git/pull/2331 Range-diff vs v2: 1: 11bcecebf4 ! 1: 9883c28482 branch: suggest / on upstream slip @@ Commit message fatal: branch 'main' does not exist - pointing at a branch the user never meant to name. + pointing at a branch the user never meant to name. When 'main' does + exist, it instead dies with: + + fatal: the requested upstream branch 'origin' does not exist + + leaving the user equally puzzled. When the operated-on branch is missing and '/' names a real remote-tracking ref, suggest the intended form: @@ builtin/branch.c: int cmd_branch(int argc, if (!refs_ref_exists(get_main_ref_store(the_repository), branch->refname)) { if (!argc || branch_checked_out(branch->refname)) die(_("no commit on branch '%s' yet"), branch->name); ++ /* ++ * Check the advice up front to avoid the ref ++ * lookups when the hint is off. The helper still ++ * calls advise_if_enabled() so the hint carries the ++ * standard "disable this message" instructions. ++ */ + if (argc == 1 && + advice_enabled(ADVICE_SET_UPSTREAM_FAILURE)) + die_if_upstream_looks_like_remote(new_upstream, argv[0]); 2: 49de5a925d ! 2: dbe4dbc346 push: suggest for a slash slip @@ builtin/push.c: int cmd_push(int argc, if (!add_remote_or_group(repo, &remote_group)) { + struct remote *r; + ++ /* ++ * Check the advice up front to avoid the remote ++ * lookup when the hint is off. The helper still ++ * calls advise_if_enabled() so the hint carries the ++ * standard "disable this message" instructions. ++ */ + if (advice_enabled(ADVICE_PUSH_REPO_LOOKS_LIKE_REF)) + die_if_repo_looks_like_ref(repo); + @@ t/t5529-push-errors.sh: test_expect_success 'detect empty remote with targeted r +test_expect_success 'suggest for a / slip' ' + test_must_fail git push origin/main 2>stderr && -+ grep "${SQ}origin/main${SQ} is not a valid push target" stderr && -+ grep "hint: Did you mean to use: git push origin main?" stderr && ++ test_grep "${SQ}origin/main${SQ} is not a valid push target" stderr && ++ test_grep "hint: Did you mean to use: git push origin main?" stderr && + test_must_fail git -c advice.pushRepoLooksLikeRef=false push origin/main 2>stderr && -+ ! grep "Did you mean" stderr ++ test_grep ! "Did you mean" stderr +' + +test_expect_success 'suggest when the branch has slashes' ' + test_must_fail git push origin/feature/x 2>stderr && -+ grep "hint: Did you mean to use: git push origin feature/x?" stderr ++ test_grep "hint: Did you mean to use: git push origin feature/x?" stderr +' + +test_expect_success 'no suggestion when prefix is not a configured remote' ' + test_must_fail git push not-a-remote/main 2>stderr && -+ ! grep "Did you mean" stderr ++ test_grep ! "Did you mean" stderr +' + +test_expect_success 'no suggestion for a trailing slash with no branch' ' + test_must_fail git push origin/ 2>stderr && -+ ! grep "Did you mean" stderr ++ test_grep ! "Did you mean" stderr +' + +test_expect_success 'no suggestion when the argument is an existing path' ' + test_when_finished "rm -rf origin" && + git init --bare origin/main && + git push origin/main HEAD:refs/heads/pushed 2>stderr && -+ ! grep "Did you mean" stderr && ++ test_grep ! "Did you mean" stderr && + git -C origin/main rev-parse --verify refs/heads/pushed +' + -- gitgitgadget