Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve hint for diverging branches. #1570

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

perk11
Copy link

@perk11 perk11 commented Sep 4, 2023

I have seen a lot of developers not know what to do when they try
to do a git pull on a master branch with the intention of updating
that branch to the latest version, but see an error about branches
diverging because they accidentally committed their changes to
that branch. They then spend their time resolving conflicts and
still not getting the intended result. The suggestion to do a hard
reset should be something that helps in this situation.

I'm not sure if a new config option needs to be created as
technically these are two different advice now.
I'm also not sure if "refs/remotes" part of the refspec is necessary,
that is what I found the functions in pull.c are returning.
I think "upstream/branch-name" should be the same thing, but kept it
as is ( git reset --hard refs/remotes/upstream/branch-name) for now.
Please feel free to chime in.

@gitgitgadget-git
Copy link

Welcome to GitGitGadget

Hi @perk11, and welcome to GitGitGadget, the GitHub App to send patch series to the Git mailing list from GitHub Pull Requests.

Please make sure that your Pull Request has a good description, as it will be used as cover letter. You can CC potential reviewers by adding a footer to the PR description with the following syntax:

CC: Revi Ewer <revi.ewer@example.com>, Ill Takalook <ill.takalook@example.net>

Also, it is a good idea to review the commit messages one last time, as the Git project expects them in a quite specific form:

  • the lines should not exceed 76 columns,
  • the first line should be like a header and typically start with a prefix like "tests:" or "revisions:" to state which subsystem the change is about, and
  • the commit messages' body should be describing the "why?" of the change.
  • Finally, the commit messages should end in a Signed-off-by: line matching the commits' author.

It is in general a good idea to await the automated test ("Checks") in this Pull Request before contributing the patches, e.g. to avoid trivial issues such as unportable code.

Contributing the patches

Before you can contribute the patches, your GitHub username needs to be added to the list of permitted users. Any already-permitted user can do that, by adding a comment to your PR of the form /allow. A good way to find other contributors is to locate recent pull requests where someone has been /allowed:

Both the person who commented /allow and the PR author are able to /allow you.

An alternative is the channel #git-devel on the Libera Chat IRC network:

<newcontributor> I've just created my first PR, could someone please /allow me? https://github.com/gitgitgadget/git/pull/12345
<veteran> newcontributor: it is done
<newcontributor> thanks!

Once on the list of permitted usernames, you can contribute the patches to the Git mailing list by adding a PR comment /submit.

If you want to see what email(s) would be sent for a /submit request, add a PR comment /preview to have the email(s) sent to you. You must have a public GitHub email address for this. Note that any reviewers CC'd via the list in the PR description will not actually be sent emails.

After you submit, GitGitGadget will respond with another comment that contains the link to the cover letter mail in the Git mailing list archive. Please make sure to monitor the discussion in that thread and to address comments and suggestions (while the comments and suggestions will be mirrored into the PR by GitGitGadget, you will still want to reply via mail).

If you do not want to subscribe to the Git mailing list just to be able to respond to a mail, you can download the mbox from the Git mailing list archive (click the (raw) link), then import it into your mail program. If you use GMail, you can do this via:

curl -g --user "<EMailAddress>:<Password>" \
    --url "imaps://imap.gmail.com/INBOX" -T /path/to/raw.txt

To iterate on your change, i.e. send a revised patch or patch series, you will first want to (force-)push to the same branch. You probably also want to modify your Pull Request description (or title). It is a good idea to summarize the revision by adding something like this to the cover letter (read: by editing the first comment on the PR, i.e. the PR description):

Changes since v1:
- Fixed a typo in the commit message (found by ...)
- Added a code comment to ... as suggested by ...
...

To send a new iteration, just add another PR comment with the contents: /submit.

Need help?

New contributors who want advice are encouraged to join git-mentoring@googlegroups.com, where volunteers who regularly contribute to Git are willing to answer newbie questions, give advice, or otherwise provide mentoring to interested contributors. You must join in order to post or view messages, but anyone can join.

You may also be able to find help in real time in the developer IRC channel, #git-devel on Libera Chat. Remember that IRC does not support offline messaging, so if you send someone a private message and log out, they cannot respond to you. The scrollback of #git-devel is archived, though.

@gitgitgadget-git
Copy link

There are issues in commit 23fa109:
Improve hint for diverging branches.
Lines in the body of the commit messages should be wrapped between 60 and 76 characters.
Indented lines, and lines without whitespace, are exempt

@perk11 perk11 force-pushed the diverging-advice-improvements branch from 23fa109 to 40aa49a Compare September 4, 2023 09:07
@gitgitgadget-git
Copy link

There are issues in commit 40aa49a:
Improve hint for diverging branches.
Lines in the body of the commit messages should be wrapped between 60 and 76 characters.
Indented lines, and lines without whitespace, are exempt

@perk11 perk11 force-pushed the diverging-advice-improvements branch 2 times, most recently from 237f897 to 5044df2 Compare September 4, 2023 09:12
@dscho
Copy link
Member

dscho commented Sep 4, 2023

/allow

@gitgitgadget-git
Copy link

User perk11 is now allowed to use GitGitGadget.

@perk11 perk11 force-pushed the diverging-advice-improvements branch from 5044df2 to 123b22b Compare September 4, 2023 20:05
@perk11
Copy link
Author

perk11 commented Sep 4, 2023

/preview

@gitgitgadget-git
Copy link

Preview email sent as pull.1570.git.git.1693858078020.gitgitgadget@gmail.com

Added a description of what the offered options will do and for pull
also offered a 3rd option during a pull - a hard reset.
This option should be helpful for the new users that accidentally
committed into the wrong branch which is a scenario I saw very
often.

The resulting tooltip looks like this for pull:

hint: Diverging branches can't be fast-forwarded.
Consider the following options:
hint:
hint: To merge remote changes into your branch:
hint:   git merge --no-ff
hint:
hint: To apply your changes on top of remote changes:
hint:   git rebase
hint:
hint: To discard your local changes and apply the remote changes:
hint:   git reset --hard refs/remotes/upstream/branch-name
hint:
hint: Disable this message with "git config advice.diverging false"

There is some danger because it's semi-destructive, but so are
other options offered if user doesn't know the commands to
revert back. Additionally, I think "To discard your local changes"
wording describes the danger clearly enough.

And for merge I improved the wording and added a description of what
the commands do:

hint: Diverging branches can't be fast-forwarded.
hint: Consider the following options:
hint:
hint: To merge changes into your branch:
hint:   git merge --no-ff
hint:
hint: To apply your changes on top:
hint:   git rebase
hint:
hint: Disable this message with "git config advice.diverging false"

Signed-off-by: Konstantin Pereiaslov <perk11@perk11.info>
@perk11 perk11 force-pushed the diverging-advice-improvements branch from 123b22b to 21bf8fb Compare September 4, 2023 20:20
@perk11
Copy link
Author

perk11 commented Sep 4, 2023

/preview

@gitgitgadget-git
Copy link

Preview email sent as pull.1570.git.git.1693858939372.gitgitgadget@gmail.com

@perk11
Copy link
Author

perk11 commented Sep 4, 2023

/preview

@gitgitgadget-git
Copy link

Preview email sent as pull.1570.git.git.1693859432738.gitgitgadget@gmail.com

@perk11
Copy link
Author

perk11 commented Sep 4, 2023

/submit

@gitgitgadget-git
Copy link

Submitted as pull.1570.git.git.1693861162353.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-git-1570/perk11/diverging-advice-improvements-v1

To fetch this version to local tag pr-git-1570/perk11/diverging-advice-improvements-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-git-1570/perk11/diverging-advice-improvements-v1

@gitgitgadget-git
Copy link

On the Git mailing list, Junio C Hamano wrote (reply to this):

"Konstantin Pereiaslov via GitGitGadget" <gitgitgadget@gmail.com>
writes:

> From: Konstantin Pereiaslov <perk11@perk11.info>
>
> Added a description of what the offered options will do and for pull
> also offered a 3rd option during a pull - a hard reset.
> This option should be helpful for the new users that accidentally
> committed into the wrong branch which is a scenario I saw very
> often.

cf. Documentation/SubmittingPatches:[[describe-changes]]

> The resulting tooltip looks like this for pull:
>
> hint: Diverging branches can't be fast-forwarded.
> Consider the following options:

We do not give "hint:" prefix to this line???

> hint:
> hint: To merge remote changes into your branch:
> hint:   git merge --no-ff
> hint:
> hint: To apply your changes on top of remote changes:
> hint:   git rebase

Hmph, "apply" -> "replay" perhaps?

> hint: To discard your local changes and apply the remote changes:

Here "apply" is definitely a misnomer.  Nothing is applied; you just
discard your work and adopt (or "accept") the state of the remote as
a whole.

> hint:   git reset --hard refs/remotes/upstream/branch-name
> hint:
> hint: Disable this message with "git config advice.diverging false"

OK.

Overall, "... looks like this" should be shown a bit indented so
that the example stands out from the text that explains the example.

> There is some danger because it's semi-destructive, but so are
> other options offered if user doesn't know the commands to
> revert back.

Sorry, but I do not quite understand what you want to say here.

> @@ -1112,8 +1126,10 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
>  
>  	/* ff-only takes precedence over rebase */
>  	if (opt_ff && !strcmp(opt_ff, "--ff-only")) {
> -		if (divergent)
> -			die_ff_impossible();
> +		if (divergent) {
> +			const char* pull_branch_spec = get_pull_branch(repo, *refspecs);

In this codebase, asterisk sticks to the variable/function
identifier, not types.

But more importantly, what guarantees your recomputation using
'*refspecs' here will match the result of the logic that computed
'divergent', which certainly would have already known what commit we
tried to fast-forward our branch to, and where that commit came
from?  We shouldn't be computing the same thing twice, and in
different ways; that is a sure way to introduce inconsistent
results.

> +			die_ff_impossible_during_pull(pull_branch_spec);
> +		}
>  		opt_rebase = REBASE_FALSE;
>  	}
>  	/* If no action specified and we can't fast forward, then warn. */
>
> base-commit: d814540bb75bbd2257f9a6bf59661a84fe8cf3cf

Thanks.

@gitgitgadget-git
Copy link

On the Git mailing list, Konstantin Pereiaslov wrote (reply to this):

Thank you, all good points. I will work on the wording
improvements based on your suggestions.

"Consider the following options:" should be on a new line and
get a "hint:" prefix, I will fix that.


As far as the danger to the user, I was talking about the fact that
the user doing "git reset --hard" is going to lose any uncommitted
work as well as any commits currently in the branch.

> "But more importantly, what guarantees your recomputation using
> '*refspecs' here will match the result of the logic that computed
> 'divergent', which certainly would have already known what commit we
> tried to fast-forward our branch to, and where that commit came
> from?  We shouldn't be computing the same thing twice, and in
> different ways; that is a sure way to introduce inconsistent
> results.

This makes sens, I did it this way because I wanted to get a remote and
branch name, not just hash id and it was difficult to get this information.
I will try to rework it so that it shares the code with the logic that determines
the divergence status.
Any tips on what's the best way to do that would be highly appreciated.

On 9/5/23 18:20, Junio C Hamano wrote:
> "Konstantin Pereiaslov via GitGitGadget" <gitgitgadget@gmail.com>
> writes:
>
>> From: Konstantin Pereiaslov <perk11@perk11.info>
>>
>> Added a description of what the offered options will do and for pull
>> also offered a 3rd option during a pull - a hard reset.
>> This option should be helpful for the new users that accidentally
>> committed into the wrong branch which is a scenario I saw very
>> often.
> cf. Documentation/SubmittingPatches:[[describe-changes]]
>
>> The resulting tooltip looks like this for pull:
>>
>> hint: Diverging branches can't be fast-forwarded.
>> Consider the following options:
> We do not give "hint:" prefix to this line???
>
>> hint:
>> hint: To merge remote changes into your branch:
>> hint:   git merge --no-ff
>> hint:
>> hint: To apply your changes on top of remote changes:
>> hint:   git rebase
> Hmph, "apply" -> "replay" perhaps?
>
>> hint: To discard your local changes and apply the remote changes:
> Here "apply" is definitely a misnomer.  Nothing is applied; you just
> discard your work and adopt (or "accept") the state of the remote as
> a whole.
>
>> hint:   git reset --hard refs/remotes/upstream/branch-name
>> hint:
>> hint: Disable this message with "git config advice.diverging false"
> OK.
>
> Overall, "... looks like this" should be shown a bit indented so
> that the example stands out from the text that explains the example.
>
>> There is some danger because it's semi-destructive, but so are
>> other options offered if user doesn't know the commands to
>> revert back.
> Sorry, but I do not quite understand what you want to say here.
>
>> @@ -1112,8 +1126,10 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
>>   >>   	/* ff-only takes precedence over rebase */
>>   	if (opt_ff && !strcmp(opt_ff, "--ff-only")) {
>> -		if (divergent)
>> -			die_ff_impossible();
>> +		if (divergent) {
>> +			const char* pull_branch_spec = get_pull_branch(repo, *refspecs);
> In this codebase, asterisk sticks to the variable/function
> identifier, not types.
>
> But more importantly, what guarantees your recomputation using
> '*refspecs' here will match the result of the logic that computed
> 'divergent', which certainly would have already known what commit we
> tried to fast-forward our branch to, and where that commit came
> from?  We shouldn't be computing the same thing twice, and in
> different ways; that is a sure way to introduce inconsistent
> results.
>
>> +			die_ff_impossible_during_pull(pull_branch_spec);
>> +		}
>>   		opt_rebase = REBASE_FALSE;
>>   	}
>>   	/* If no action specified and we can't fast forward, then warn. */
>>
>> base-commit: d814540bb75bbd2257f9a6bf59661a84fe8cf3cf
> Thanks.

@gitgitgadget-git
Copy link

On the Git mailing list, Konstantin Pereiaslov wrote (reply to this):

Thank you, all good points. I will work on the wording
improvements based on your suggestions.

"Consider the following options:" should be on a new line and
get a "hint:" prefix, I will fix that.


As far as the danger to the user, I was talking about the fact that
the user doing "git reset --hard" is going to lose any uncommitted
work as well as any commits currently in the branch.

> "But more importantly, what guarantees your recomputation using
> '*refspecs' here will match the result of the logic that computed
> 'divergent', which certainly would have already known what commit we
> tried to fast-forward our branch to, and where that commit came
> from?  We shouldn't be computing the same thing twice, and in
> different ways; that is a sure way to introduce inconsistent
> results.

This makes sens, I did it this way because I wanted to get a remote and
branch name, not just hash id and it was difficult to get this information.
I will try to rework it so that it shares the code with the logic that determines
the divergence status.
Any tips on what's the best way to do that would be highly appreciated.

On 9/5/23 18:20, Junio C Hamano wrote:
> "Konstantin Pereiaslov via GitGitGadget" <gitgitgadget@gmail.com>
> writes:
>
>> From: Konstantin Pereiaslov <perk11@perk11.info>
>>
>> Added a description of what the offered options will do and for pull
>> also offered a 3rd option during a pull - a hard reset.
>> This option should be helpful for the new users that accidentally
>> committed into the wrong branch which is a scenario I saw very
>> often.
> cf. Documentation/SubmittingPatches:[[describe-changes]]
>
>> The resulting tooltip looks like this for pull:
>>
>> hint: Diverging branches can't be fast-forwarded.
>> Consider the following options:
> We do not give "hint:" prefix to this line???
>
>> hint:
>> hint: To merge remote changes into your branch:
>> hint:   git merge --no-ff
>> hint:
>> hint: To apply your changes on top of remote changes:
>> hint:   git rebase
> Hmph, "apply" -> "replay" perhaps?
>
>> hint: To discard your local changes and apply the remote changes:
> Here "apply" is definitely a misnomer.  Nothing is applied; you just
> discard your work and adopt (or "accept") the state of the remote as
> a whole.
>
>> hint:   git reset --hard refs/remotes/upstream/branch-name
>> hint:
>> hint: Disable this message with "git config advice.diverging false"
> OK.
>
> Overall, "... looks like this" should be shown a bit indented so
> that the example stands out from the text that explains the example.
>
>> There is some danger because it's semi-destructive, but so are
>> other options offered if user doesn't know the commands to
>> revert back.
> Sorry, but I do not quite understand what you want to say here.
>
>> @@ -1112,8 +1126,10 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
>>   >>   	/* ff-only takes precedence over rebase */
>>   	if (opt_ff && !strcmp(opt_ff, "--ff-only")) {
>> -		if (divergent)
>> -			die_ff_impossible();
>> +		if (divergent) {
>> +			const char* pull_branch_spec = get_pull_branch(repo, *refspecs);
> In this codebase, asterisk sticks to the variable/function
> identifier, not types.
>
> But more importantly, what guarantees your recomputation using
> '*refspecs' here will match the result of the logic that computed
> 'divergent', which certainly would have already known what commit we
> tried to fast-forward our branch to, and where that commit came
> from?  We shouldn't be computing the same thing twice, and in
> different ways; that is a sure way to introduce inconsistent
> results.
>
>> +			die_ff_impossible_during_pull(pull_branch_spec);
>> +		}
>>   		opt_rebase = REBASE_FALSE;
>>   	}
>>   	/* If no action specified and we can't fast forward, then warn. */
>>
>> base-commit: d814540bb75bbd2257f9a6bf59661a84fe8cf3cf
> Thanks.
>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants