diff mbox series

[2/2] push: add an advice on unqualified <dst> push

Message ID 20181010104145.25610-3-avarab@gmail.com (mailing list archive)
State New, archived
Headers show
Series add an advice on unqualified <dst> push | expand

Commit Message

Ævar Arnfjörð Bjarmason Oct. 10, 2018, 10:41 a.m. UTC
Improve the error message added in f8aae12034 ("push: allow
unqualified dest refspecs to DWIM", 2008-04-23), which before this
change looks like this:

    $ git push avar v2.19.0^{commit}:newbranch -n
    error: unable to push to unqualified destination: newbranch
    The destination refspec neither matches an existing ref on the remote nor
    begins with refs/, and we are unable to guess a prefix based on the source ref.
    error: failed to push some refs to 'git@github.com:avar/git.git'

This message needed to be read very carefully to spot how to fix the
error, i.e. to push to refs/heads/newbranch, and it didn't use the
advice system (since initial addition of the error predated it).

Fix both of those, now the message will look like this instead:

    $ ./git-push avar v2.19.0^{commit}:newbranch -n
    error: unable to push to unqualified destination: newbranch
    hint: The destination refspec neither matches an existing
    hint: ref on the remote nor begins with refs/, and we are
    hint: unable to guess a prefix based on the source ref.
    hint:
    hint: The <src> part of the refspec is a commit object.
    hint: Did you mean to create a new branch by pushing to
    hint: 'v2.19.0^{commit}:refs/heads/newbranch'?
    error: failed to push some refs to 'git@github.com:avar/git.git'

When trying to push a tag, tree or a blob we suggest that perhaps the
user meant to push them to refs/tags/ instead.

The if/else duplication for all of OBJ_{COMMIT,TAG,TREE,BLOB} is
unfortunate, but is required to correctly mark the messages for
translation.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 Documentation/config.txt |  7 +++++
 advice.c                 |  2 ++
 advice.h                 |  1 +
 remote.c                 | 62 +++++++++++++++++++++++++++++++++++-----
 t/t5505-remote.sh        | 25 ++++++++++++++++
 5 files changed, 90 insertions(+), 7 deletions(-)

Comments

Jeff King Oct. 10, 2018, 8:55 p.m. UTC | #1
On Wed, Oct 10, 2018 at 10:41:45AM +0000, Ævar Arnfjörð Bjarmason wrote:

> Improve the error message added in f8aae12034 ("push: allow
> unqualified dest refspecs to DWIM", 2008-04-23), which before this
> change looks like this:
> 
>     $ git push avar v2.19.0^{commit}:newbranch -n
>     error: unable to push to unqualified destination: newbranch
>     The destination refspec neither matches an existing ref on the remote nor
>     begins with refs/, and we are unable to guess a prefix based on the source ref.
>     error: failed to push some refs to 'git@github.com:avar/git.git'

Thanks for looking into this. Despite being largely responsible for that
message myself, I always cringe when I see it because it's so opaque.

> This message needed to be read very carefully to spot how to fix the
> error, i.e. to push to refs/heads/newbranch, and it didn't use the
> advice system (since initial addition of the error predated it).
> 
> Fix both of those, now the message will look like this instead:
> 
>     $ ./git-push avar v2.19.0^{commit}:newbranch -n
>     error: unable to push to unqualified destination: newbranch
>     hint: The destination refspec neither matches an existing
>     hint: ref on the remote nor begins with refs/, and we are
>     hint: unable to guess a prefix based on the source ref.
>     hint:
>     hint: The <src> part of the refspec is a commit object.
>     hint: Did you mean to create a new branch by pushing to
>     hint: 'v2.19.0^{commit}:refs/heads/newbranch'?
>     error: failed to push some refs to 'git@github.com:avar/git.git'
> 
> When trying to push a tag, tree or a blob we suggest that perhaps the
> user meant to push them to refs/tags/ instead.

This is much better, and I love the customized behavior based on the
object type.

I wonder if we could reword the first paragraph to be a little less
confusing, and spell out what we tried already. E.g., something like:

  The destination you provided is not a full refname (i.e., starting
  with "ref"). Git tried to guess what you meant by:

    - looking for a matching branch or tag on the remote side

    - looking at the refname of the local source

  but neither worked.

  The <src> part of the refspec is a commit object.
  Did you mean...

I'm not sure about saying "branch or tag" in the first bullet. It's
friendlier to most users, but less technically correct (if you said
"notes/foo", I believe we'd match an existing "refs/notes/foo", because
it's really just using the normal lookup rules).

Also, as an aside, I wonder if we should allow "heads/foo" to work as
"refs/heads/foo" (even when no such ref already exists). But that is
totally orthogonal to changing the message.

> The if/else duplication for all of OBJ_{COMMIT,TAG,TREE,BLOB} is
> unfortunate, but is required to correctly mark the messages for
> translation.

I think it would probably be OK to put the first paragraph in its own
variable. I know we try to avoid translation lego, but I'd think
paragraphs are separate units. Or are you worried about how to get them
into the same advise() call? I don't know that we need to, but we could
also plug one into the other with a "%s" (and leave a translator note).

> diff --git a/Documentation/config.txt b/Documentation/config.txt
> index 1546833213..fd455e2739 100644
> --- a/Documentation/config.txt
> +++ b/Documentation/config.txt
> @@ -320,6 +320,13 @@ advice.*::
>  		tries to overwrite a remote ref that points at an
>  		object that is not a commit-ish, or make the remote
>  		ref point at an object that is not a commit-ish.
> +	pushAmbigiousRefName::
> +		Shown when linkgit:git-push[1] gives up trying to
> +		guess based on the source and destination refs what
> +		remote ref namespace the source belongs in, but where
> +		we can still suggest that the user push to either
> +		refs/heads/* or refs/tags/* based on the type of the
> +		source object.

I guess you could argue that this is "ambiguous", but usually we'd use
that term to mean "there were two branches that matched on the other
side". But here it's actually "no branches matched" (actually, it makes
me wonder what we'd do pushing "foo" when that name is ambiguous on the
other side).

So I wonder if this ought to be pushUnqualifiedRefname or something.

> @@ -1046,13 +1047,60 @@ static int match_explicit(struct ref *src, struct ref *dst,
>  		else if ((dst_guess = guess_ref(dst_value, matched_src))) {
>  			matched_dst = make_linked_ref(dst_guess, dst_tail);
>  			free(dst_guess);
> -		} else
> -			error(_("unable to push to unqualified destination: %s\n"
> -				"The destination refspec neither matches an "
> -				"existing ref on the remote nor\n"
> -				"begins with refs/, and we are unable to "
> -				"guess a prefix based on the source ref."),
> -			      dst_value);
> +		} else {
> +			struct object_id oid;
> +			enum object_type type;
> +
> +			error("unable to push to unqualified destination: %s", dst_value);
> +			if (!advice_push_ambiguous_ref_name)
> +				break;

This break feels funny, because it controls flow much larger than this
if/else. It does the right thing now, but it must remain in sync with
what comes at the end of that long string of advise() messages.

Can we just do it as:

  if (advice_push_ambiguous_ref_name) {
	struct object_id oid;
	enum object_type type;

	if (get_oid(...))
	   etc...
  }

instead? That pushes your indentation one level in, but I think the
whole conditional body might be cleaner in a helper function anyway.

-Peff
Ævar Arnfjörð Bjarmason Oct. 10, 2018, 9:23 p.m. UTC | #2
On Wed, Oct 10 2018, Jeff King wrote:

> On Wed, Oct 10, 2018 at 10:41:45AM +0000, Ævar Arnfjörð Bjarmason wrote:
>
>> Improve the error message added in f8aae12034 ("push: allow
>> unqualified dest refspecs to DWIM", 2008-04-23), which before this
>> change looks like this:
>>
>>     $ git push avar v2.19.0^{commit}:newbranch -n
>>     error: unable to push to unqualified destination: newbranch
>>     The destination refspec neither matches an existing ref on the remote nor
>>     begins with refs/, and we are unable to guess a prefix based on the source ref.
>>     error: failed to push some refs to 'git@github.com:avar/git.git'
>
> Thanks for looking into this. Despite being largely responsible for that
> message myself, I always cringe when I see it because it's so opaque.
>
>> This message needed to be read very carefully to spot how to fix the
>> error, i.e. to push to refs/heads/newbranch, and it didn't use the
>> advice system (since initial addition of the error predated it).
>>
>> Fix both of those, now the message will look like this instead:
>>
>>     $ ./git-push avar v2.19.0^{commit}:newbranch -n
>>     error: unable to push to unqualified destination: newbranch
>>     hint: The destination refspec neither matches an existing
>>     hint: ref on the remote nor begins with refs/, and we are
>>     hint: unable to guess a prefix based on the source ref.
>>     hint:
>>     hint: The <src> part of the refspec is a commit object.
>>     hint: Did you mean to create a new branch by pushing to
>>     hint: 'v2.19.0^{commit}:refs/heads/newbranch'?
>>     error: failed to push some refs to 'git@github.com:avar/git.git'
>>
>> When trying to push a tag, tree or a blob we suggest that perhaps the
>> user meant to push them to refs/tags/ instead.
>
> This is much better, and I love the customized behavior based on the
> object type.
>
> I wonder if we could reword the first paragraph to be a little less
> confusing, and spell out what we tried already. E.g., something like:
>
>   The destination you provided is not a full refname (i.e., starting
>   with "ref"). Git tried to guess what you meant by:
>
>     - looking for a matching branch or tag on the remote side
>
>     - looking at the refname of the local source
>
>   but neither worked.
>
>   The <src> part of the refspec is a commit object.
>   Did you mean...

Yeah that makes sense. I was trying to avoid touching the existing
wording to make this more surgical, but you came up with it, and since
you don't like it I'll just change that too.

> I'm not sure about saying "branch or tag" in the first bullet. It's
> friendlier to most users, but less technically correct (if you said
> "notes/foo", I believe we'd match an existing "refs/notes/foo", because
> it's really just using the normal lookup rules).
>
> Also, as an aside, I wonder if we should allow "heads/foo" to work as
> "refs/heads/foo" (even when no such ref already exists). But that is
> totally orthogonal to changing the message.
>
>> The if/else duplication for all of OBJ_{COMMIT,TAG,TREE,BLOB} is
>> unfortunate, but is required to correctly mark the messages for
>> translation.
>
> I think it would probably be OK to put the first paragraph in its own
> variable. I know we try to avoid translation lego, but I'd think
> paragraphs are separate units. Or are you worried about how to get them
> into the same advise() call? I don't know that we need to, but we could
> also plug one into the other with a "%s" (and leave a translator note).

To be honest from being on the code side of a much bigger i18n effort
(the MediaWiki/WikiMedia software) back in the early days of my career I
just do this sort of thing reflexively, because from experience when I
started trying to simplify stuff by making assumptions I was wrong every
time.

Although in that case there were >100+ languages, so maybe we can get
away with this.

In this case one red flag I see is that we make a reference to "the
source ref" in the first paragraph, and then in the second we'll either
talk about "commit", "tag" or "blob" etc. Now imagine a language where
those words have different genders, and where even secondary references
to those things ("the source ref") spill over and need to be changed
too.

You also get languages where a message that stretches multiple
paragraphs flows more naturally if the wording is re-arranged, even
between paragraphs. This is why document translation systems generally
split things by sections at best (not paragraphs), or just by whole
documents.

>> diff --git a/Documentation/config.txt b/Documentation/config.txt
>> index 1546833213..fd455e2739 100644
>> --- a/Documentation/config.txt
>> +++ b/Documentation/config.txt
>> @@ -320,6 +320,13 @@ advice.*::
>>  		tries to overwrite a remote ref that points at an
>>  		object that is not a commit-ish, or make the remote
>>  		ref point at an object that is not a commit-ish.
>> +	pushAmbigiousRefName::
>> +		Shown when linkgit:git-push[1] gives up trying to
>> +		guess based on the source and destination refs what
>> +		remote ref namespace the source belongs in, but where
>> +		we can still suggest that the user push to either
>> +		refs/heads/* or refs/tags/* based on the type of the
>> +		source object.
>
> I guess you could argue that this is "ambiguous", but usually we'd use
> that term to mean "there were two branches that matched on the other
> side". But here it's actually "no branches matched" (actually, it makes
> me wonder what we'd do pushing "foo" when that name is ambiguous on the
> other side).
>
> So I wonder if this ought to be pushUnqualifiedRefname or something.

Yeah that sounds better. Will change it.

>> @@ -1046,13 +1047,60 @@ static int match_explicit(struct ref *src, struct ref *dst,
>>  		else if ((dst_guess = guess_ref(dst_value, matched_src))) {
>>  			matched_dst = make_linked_ref(dst_guess, dst_tail);
>>  			free(dst_guess);
>> -		} else
>> -			error(_("unable to push to unqualified destination: %s\n"
>> -				"The destination refspec neither matches an "
>> -				"existing ref on the remote nor\n"
>> -				"begins with refs/, and we are unable to "
>> -				"guess a prefix based on the source ref."),
>> -			      dst_value);
>> +		} else {
>> +			struct object_id oid;
>> +			enum object_type type;
>> +
>> +			error("unable to push to unqualified destination: %s", dst_value);
>> +			if (!advice_push_ambiguous_ref_name)
>> +				break;
>
> This break feels funny, because it controls flow much larger than this
> if/else. It does the right thing now, but it must remain in sync with
> what comes at the end of that long string of advise() messages.
>
> Can we just do it as:
>
>   if (advice_push_ambiguous_ref_name) {
> 	struct object_id oid;
> 	enum object_type type;
>
> 	if (get_oid(...))
> 	   etc...
>   }
>
> instead? That pushes your indentation one level in, but I think the
> whole conditional body might be cleaner in a helper function anyway.

I started out with that and found myself really constrained by the 72
char ceiling which I'm already smashing through with these ~95 character
lines (but at least it's under 100!). But sure, we can do with 8 more.
Junio C Hamano Oct. 10, 2018, 9:54 p.m. UTC | #3
Jeff King <peff@peff.net> writes:

>> Fix both of those, now the message will look like this instead:
>> 
>>     $ ./git-push avar v2.19.0^{commit}:newbranch -n
>>     error: unable to push to unqualified destination: newbranch
>>     hint: The destination refspec neither matches an existing
>>     hint: ref on the remote nor begins with refs/, and we are
>>     hint: unable to guess a prefix based on the source ref.
>>     hint:
>>     hint: The <src> part of the refspec is a commit object.
>>     hint: Did you mean to create a new branch by pushing to
>>     hint: 'v2.19.0^{commit}:refs/heads/newbranch'?
>>     error: failed to push some refs to 'git@github.com:avar/git.git'
>> 
>> When trying to push a tag, tree or a blob we suggest that perhaps the
>> user meant to push them to refs/tags/ instead.
>
> This is much better, and I love the customized behavior based on the
> object type.
>
> I wonder if we could reword the first paragraph to be a little less
> confusing, and spell out what we tried already. E.g., something like:
>
>   The destination you provided is not a full refname (i.e., starting
>   with "ref"). Git tried to guess what you meant by:

s|ref|refs/|; I fully agree that "unqualified destination" was a
poor way to communicate the failure to those who would likely hit
this error path, because somebody who can ell what's qualified and
what's not would not be triggering the error in the first place.

>     - looking for a matching branch or tag on the remote side
>
>     - looking at the refname of the local source
>
>   but neither worked.
>
>   The <src> part of the refspec is a commit object.
>   Did you mean...

Looks great.

> I'm not sure about saying "branch or tag" in the first bullet. It's
> friendlier to most users, but less technically correct (if you said
> "notes/foo", I believe we'd match an existing "refs/notes/foo", because
> it's really just using the normal lookup rules).

An alternative may be "looking for a ref that matches %s on the
remote side".  I am no longer a total newbie, so I cannot tell how
well that message would help one to connect notes/foo one just typed
with refs/notes/foo that potentially exists on the remote side.

> Also, as an aside, I wonder if we should allow "heads/foo" to work as
> "refs/heads/foo" (even when no such ref already exists). But that is
> totally orthogonal to changing the message.

I am neutral on this point but agree that it is better done outside
this patch.
Jeff King Oct. 11, 2018, 12:16 a.m. UTC | #4
On Wed, Oct 10, 2018 at 11:23:25PM +0200, Ævar Arnfjörð Bjarmason wrote:

> > I wonder if we could reword the first paragraph to be a little less
> > confusing, and spell out what we tried already. E.g., something like:
> >
> >   The destination you provided is not a full refname (i.e., starting
> >   with "ref"). Git tried to guess what you meant by:
> >
> >     - looking for a matching branch or tag on the remote side
> >
> >     - looking at the refname of the local source
> >
> >   but neither worked.
> >
> >   The <src> part of the refspec is a commit object.
> >   Did you mean...
> 
> Yeah that makes sense. I was trying to avoid touching the existing
> wording to make this more surgical, but you came up with it, and since
> you don't like it I'll just change that too.

I certainly know the feeling of trying to avoid wording bikeshed
discussions. But in this instance, please feel free to aggressively
rewrite that old message. ;)

What I wrote above was off-the-cuff, and I also do not mind if you use
it as a starting point to make improvements (or take it wholesale if you
really like it).

> > I think it would probably be OK to put the first paragraph in its own
> > variable. I know we try to avoid translation lego, but I'd think
> > paragraphs are separate units. Or are you worried about how to get them
> > into the same advise() call? I don't know that we need to, but we could
> > also plug one into the other with a "%s" (and leave a translator note).
> 
> To be honest from being on the code side of a much bigger i18n effort
> (the MediaWiki/WikiMedia software) back in the early days of my career I
> just do this sort of thing reflexively, because from experience when I
> started trying to simplify stuff by making assumptions I was wrong every
> time.
> [...]

OK, I'm happy to defer to your judgement here. I have very little
translation experience myself.

> > Can we just do it as:
> >
> >   if (advice_push_ambiguous_ref_name) {
> > 	struct object_id oid;
> > 	enum object_type type;
> >
> > 	if (get_oid(...))
> > 	   etc...
> >   }
> >
> > instead? That pushes your indentation one level in, but I think the
> > whole conditional body might be cleaner in a helper function anyway.
> 
> I started out with that and found myself really constrained by the 72
> char ceiling which I'm already smashing through with these ~95 character
> lines (but at least it's under 100!). But sure, we can do with 8 more.

That's why I suggested the helper function. :) I'm also not opposed to
pulling messages out to static file-level variables, even if they're
only used once. Sometimes it's nice to have them left-aligned (or close
to it) to see how they'll actually look in a terminal.

-Peff
Jeff King Oct. 11, 2018, 12:19 a.m. UTC | #5
On Thu, Oct 11, 2018 at 06:54:15AM +0900, Junio C Hamano wrote:

> > I'm not sure about saying "branch or tag" in the first bullet. It's
> > friendlier to most users, but less technically correct (if you said
> > "notes/foo", I believe we'd match an existing "refs/notes/foo", because
> > it's really just using the normal lookup rules).
> 
> An alternative may be "looking for a ref that matches %s on the
> remote side".  I am no longer a total newbie, so I cannot tell how
> well that message would help one to connect notes/foo one just typed
> with refs/notes/foo that potentially exists on the remote side.

Yeah. Really, it would be nice to imply that it somehow does the same
DWIM lookup that we do for local refs. But I didn't know how to say
that. Possibly we could refer to the documentation, but it's buried in
git-rev-parse.

> > Also, as an aside, I wonder if we should allow "heads/foo" to work as
> > "refs/heads/foo" (even when no such ref already exists). But that is
> > totally orthogonal to changing the message.
> 
> I am neutral on this point but agree that it is better done outside
> this patch.

Yeah, definitely. I would almost call it a leftover bit, but I think the
subtlety is not in the code, but in whether it is a good thing to be
doing (i.e., too many false positives).

-Peff
Junio C Hamano Oct. 11, 2018, 10:45 p.m. UTC | #6
Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:

> On Wed, Oct 10 2018, Jeff King wrote:
>
>> This is much better, and I love the customized behavior based on the
>> object type.
>>
>> I wonder if we could reword the first paragraph to be a little less
>> confusing, and spell out what we tried already. E.g., something like:
>> ...
>
> Yeah that makes sense. I was trying to avoid touching the existing
> wording to make this more surgical, but you came up with it, and since
> you don't like it I'll just change that too.

OK, for now I'll mark these two patches "read" in my inbox and
forget about them, expecting that a reroll of 2/2 with improved
messages would appear not in too distant future.

Thanks, both.
Ævar Arnfjörð Bjarmason Oct. 26, 2018, 3:45 p.m. UTC | #7
On Thu, Oct 11 2018, Junio C Hamano wrote:

> Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
>
>> On Wed, Oct 10 2018, Jeff King wrote:
>>
>>> This is much better, and I love the customized behavior based on the
>>> object type.
>>>
>>> I wonder if we could reword the first paragraph to be a little less
>>> confusing, and spell out what we tried already. E.g., something like:
>>> ...
>>
>> Yeah that makes sense. I was trying to avoid touching the existing
>> wording to make this more surgical, but you came up with it, and since
>> you don't like it I'll just change that too.
>
> OK, for now I'll mark these two patches "read" in my inbox and
> forget about them, expecting that a reroll of 2/2 with improved
> messages would appear not in too distant future.

I was going to submit an update to this, as an additional improvement
can anyone think of a reason not to always infer that we'd like a new
branch if the LHS of the refspec starts with refs/remotes/* ?

    u git (push-advice-on-unqualified-src-2 $>) $ ./git-push avar refs/remotes/origin/master:newbranch -n
    To github.com:avar/git.git
     * [new branch]            origin/master -> newbranch
    u git (push-advice-on-unqualified-src-2 $>) $ git diff
    diff --git a/remote.c b/remote.c
    index 5b679df02d..949a9bd079 100644
    --- a/remote.c
    +++ b/remote.c
    @@ -969,7 +969,8 @@ static char *guess_ref(const char *name, struct ref *peer)
            if (!r)
                    return NULL;

    -       if (starts_with(r, "refs/heads/"))
    +       if (starts_with(r, "refs/heads/") ||
    +           starts_with(r, "refs/remotes/"))
                    strbuf_addstr(&buf, "refs/heads/");
            else if (starts_with(r, "refs/tags/"))
                    strbuf_addstr(&buf, "refs/tags/");

Maybe we need to be really paranoid here and also check if it's a
"commit", i.e. you could setup a refspec like:

    fetch = +refs/tags/*:refs/remotes/origin-tags/*
Junio C Hamano Oct. 29, 2018, 1 a.m. UTC | #8
Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:

> I was going to submit an update to this, as an additional improvement
> can anyone think of a reason not to always infer that we'd like a new
> branch if the LHS of the refspec starts with refs/remotes/* ?

Depends on what purpose the remote you are pushing into serves.  My
instinct tells me that it may be more likely to be emulating the
case where the remote, which is hosted on a box on which for some
reason it is cumbersome for you to get an interactive shell prompt,
did the same fetch as your local repository and stored the same
value in its remote-tracking branch than creating a local branch.  I
do not say it is entirely unlikely that the push wants to create a
local branch there, though.  It can be a way to "reprint" what
somebody else published as their local branch, which you copied to
your remote-tracking branches, to the destination of your push.  I
just felt that it is less likely.

To put it another way, I would think both of these two have at most
the same probability that the push wants to go to a local branch:

	git push refs/remotes/foo:foo
	git push <any random sha1 expression>:foo

and I would further say that the former is less likely than the
latter that it wants to create a local branch, because it is more
plausible that it wants to create a similar remote-tracking branch
there.
Junio C Hamano Oct. 29, 2018, 4:17 a.m. UTC | #9
Junio C Hamano <gitster@pobox.com> writes:

> To put it another way, I would think both of these two have at most
> the same probability that the push wants to go to a local branch:
>
> 	git push refs/remotes/foo:foo
> 	git push <any random sha1 expression>:foo
>
> and I would further say that the former is less likely than the
> latter that it wants to create a local branch, because it is more
> plausible that it wants to create a similar remote-tracking branch
> there.

This needs clarification.  I do not mean "it is more plausible that
it wants remote-tracking rather than local".  What I meant was that
between the two cases, pushing refs/remotes/foo:foo is more likely a
sign that the user wants to create a local branch than pushing other
random sha1 expression, like 40eaf9377fe649:foo, is.
diff mbox series

Patch

diff --git a/Documentation/config.txt b/Documentation/config.txt
index 1546833213..fd455e2739 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -320,6 +320,13 @@  advice.*::
 		tries to overwrite a remote ref that points at an
 		object that is not a commit-ish, or make the remote
 		ref point at an object that is not a commit-ish.
+	pushAmbigiousRefName::
+		Shown when linkgit:git-push[1] gives up trying to
+		guess based on the source and destination refs what
+		remote ref namespace the source belongs in, but where
+		we can still suggest that the user push to either
+		refs/heads/* or refs/tags/* based on the type of the
+		source object.
 	statusHints::
 		Show directions on how to proceed from the current
 		state in the output of linkgit:git-status[1], in
diff --git a/advice.c b/advice.c
index 3561cd64e9..84e9d0168d 100644
--- a/advice.c
+++ b/advice.c
@@ -9,6 +9,7 @@  int advice_push_non_ff_matching = 1;
 int advice_push_already_exists = 1;
 int advice_push_fetch_first = 1;
 int advice_push_needs_force = 1;
+int advice_push_ambiguous_ref_name = 1;
 int advice_status_hints = 1;
 int advice_status_u_option = 1;
 int advice_commit_before_merge = 1;
@@ -62,6 +63,7 @@  static struct {
 	{ "pushAlreadyExists", &advice_push_already_exists },
 	{ "pushFetchFirst", &advice_push_fetch_first },
 	{ "pushNeedsForce", &advice_push_needs_force },
+	{ "pushAmbigiousRefName", &advice_push_ambiguous_ref_name },
 	{ "statusHints", &advice_status_hints },
 	{ "statusUoption", &advice_status_u_option },
 	{ "commitBeforeMerge", &advice_commit_before_merge },
diff --git a/advice.h b/advice.h
index ab24df0fd0..d2445cab8b 100644
--- a/advice.h
+++ b/advice.h
@@ -9,6 +9,7 @@  extern int advice_push_non_ff_matching;
 extern int advice_push_already_exists;
 extern int advice_push_fetch_first;
 extern int advice_push_needs_force;
+extern int advice_push_ambiguous_ref_name;
 extern int advice_status_hints;
 extern int advice_status_u_option;
 extern int advice_commit_before_merge;
diff --git a/remote.c b/remote.c
index cc5553acc2..78fa2d9aff 100644
--- a/remote.c
+++ b/remote.c
@@ -13,6 +13,7 @@ 
 #include "mergesort.h"
 #include "argv-array.h"
 #include "commit-reach.h"
+#include "advice.h"
 
 enum map_direction { FROM_SRC, FROM_DST };
 
@@ -1046,13 +1047,60 @@  static int match_explicit(struct ref *src, struct ref *dst,
 		else if ((dst_guess = guess_ref(dst_value, matched_src))) {
 			matched_dst = make_linked_ref(dst_guess, dst_tail);
 			free(dst_guess);
-		} else
-			error(_("unable to push to unqualified destination: %s\n"
-				"The destination refspec neither matches an "
-				"existing ref on the remote nor\n"
-				"begins with refs/, and we are unable to "
-				"guess a prefix based on the source ref."),
-			      dst_value);
+		} else {
+			struct object_id oid;
+			enum object_type type;
+
+			error("unable to push to unqualified destination: %s", dst_value);
+			if (!advice_push_ambiguous_ref_name)
+				break;
+			if (get_oid(matched_src->name, &oid))
+				BUG("'%s' is not a valid object, "
+				    "match_explicit_lhs() should catch this!",
+				    matched_src->name);
+			type = oid_object_info(the_repository, &oid, NULL);
+			if (type == OBJ_COMMIT) {
+
+				advise(_("The destination refspec neither matches an existing\n"
+					 "ref on the remote nor begins with refs/, and we are\n"
+					 "unable to guess a prefix based on the source ref.\n"
+					 "\n"
+					 "The <src> part of the refspec is a commit object.\n"
+					 "Did you mean to create a new branch by pushing to\n"
+					 "'%s:refs/heads/%s'?"),
+				       matched_src->name, dst_value);
+			} else if (type == OBJ_TAG) {
+				advise(_("The destination refspec neither matches an existing\n"
+					 "ref on the remote nor begins with refs/, and we are\n"
+					 "unable to guess a prefix based on the source ref.\n"
+					 "\n"
+					 "The <src> part of the refspec is a tag object.\n"
+					 "Did you mean to create a new tag by pushing to\n"
+					 "'%s:refs/tags/%s'?"),
+				       matched_src->name, dst_value);
+			} else if (type == OBJ_TREE) {
+				advise(_("The destination refspec neither matches an existing\n"
+					 "ref on the remote nor begins with refs/, and we are\n"
+					 "unable to guess a prefix based on the source ref.\n"
+					 "\n"
+					 "The <src> part of the refspec is a tree object.\n"
+					 "Did you mean to tag a new tree by pushing to\n"
+					 "'%s:refs/tags/%s'?"),
+				       matched_src->name, dst_value);
+			} else if (type == OBJ_BLOB) {
+				advise(_("The destination refspec neither matches an existing\n"
+					 "ref on the remote nor begins with refs/, and we are\n"
+					 "unable to guess a prefix based on the source ref.\n"
+					 "\n"
+					 "The <src> part of the refspec is a blob object.\n"
+					 "Did you mean to tag a new blob by pushing to\n"
+					 "'%s:refs/tags/%s'?"),
+				       matched_src->name, dst_value);
+			} else {
+				BUG("'%s' should be commit/tag/tree/blob, is '%d'",
+				    matched_src->name, type);
+			}
+		}
 		break;
 	default:
 		matched_dst = NULL;
diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh
index d2a2cdd453..1eabf06aa4 100755
--- a/t/t5505-remote.sh
+++ b/t/t5505-remote.sh
@@ -1222,4 +1222,29 @@  test_expect_success 'add remote matching the "insteadOf" URL' '
 	git remote add backup xyz@example.com
 '
 
+test_expect_success 'unqualified refspec DWIM and advice' '
+	test_when_finished "(cd test && git tag -d some-tag)" &&
+	(
+		cd test &&
+		git tag -a -m "Some tag" some-tag master &&
+		for type in commit tag tree blob
+		do
+			if test "$type" = "blob"
+			then
+				oid=$(git rev-parse some-tag:file)
+			else
+				oid=$(git rev-parse some-tag^{$type})
+			fi &&
+			test_must_fail git push origin $oid:dst -n 2>err &&
+			test_i18ngrep "error: unable to push" err &&
+			test_i18ngrep "hint: Did you mean" err &&
+			test_must_fail git -c advice.pushAmbigiousRefName=false \
+				push origin $oid:dst -n 2>err &&
+			test_i18ngrep "error: unable to push" err &&
+			test_i18ngrep ! "hint: Did you mean" err
+		done
+	)
+'
+
+
 test_done