git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Give git-pull a --reset option

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Apr 17, 2019, 08:51 UTC
Message-ID
<CAPig+cQDU=Nounwiy5CFCfPvnj5eJJXNv+ihgbh2nYYVTYPBCQ@mail.gmail.com>
In-Reply-To
<20190417040733.22200-1-alexhenrie24@gmail.com>
On Wed, Apr 17, 2019 at 12:07 AM Alex Henrie <alexhenrie24@gmail.com> wrote:
Show 9 quoted lines
> A common workflow is to make a commit on a local branch, push the branch
> to the remote, check out the remote branch on a second computer, amend
> the commit on the second computer, force-push back to the remote branch,
> and finally submit a pull request. However, if the user switches back to
> the first computer, they must then run the cumbersome command
> `git fetch && git reset --hard origin`. (Actually, at this point Git
> novices often try running `git pull --force`, but it doesn't do what
> they expect.) This patch adds the shortcut `git pull --reset` to serve
> as a complement to `git push --force`.

I didn't know this was a particularly common workflow, although I admit to working this way myself often enough.

> Signed-off-by: Alex Henrie <alexhenrie24@gmail.com>
> ---
>  Documentation/git-pull.txt |  8 ++++++++
>  builtin/pull.c             | 28 ++++++++++++++++++++++++++++
Perhaps add a test or two to t/t5520-pull.sh covering this new feature.
Show 9 quoted lines
> diff --git a/builtin/pull.c b/builtin/pull.c
> @@ -860,6 +864,21 @@ static int run_rebase(const struct object_id *curr_head,
> +/**
> + * Runs git-reset, returning its exit status.
> + */
> +static int run_reset(void)
> +{
> +       int ret;
> +       struct argv_array args = ARGV_ARRAY_INIT;
In this codebase, it's typical to have a blank line following the declarations.
> +       argv_array_pushl(&args, "reset", NULL);
> +       argv_array_push(&args, "--hard");
> +       argv_array_push(&args, "FETCH_HEAD");

Not sure why you're using "pushl" for the first and then plain "push" for the rest. With "pushl", the entire setup can be collapsed to:

    argv_array_pushl(&args, "reset", "--hard", "FETCH_HEAD", NULL);

But, given that this argument list isn't dynamic, you could simplify further by just declaring:

    const char *argv[] = { "reset", "--hard", "FETCH_HEAD", NULL };
> +       ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
> +       argv_array_clear(&args);
...and do away with the argv_array_clear() altogether.
> +       return ret;
> +}

In fact, taking these suggestions into account, the code collapses to just two lines, which you could easily inline into the sole caller, thus you don't need a separate function at all.

> @@ -892,6 +911,9 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
> +       if (opt_rebase && opt_reset)
> +               die(_("--rebase and --reset are mutually exclusive."));

Modern practice on this project is to not end these messages with a period, however, as most messages in this file currently end with a period, this may be okay.

>         if (!opt_rebase && opt_autostash != -1)
>                 die(_("--[no-]autostash option is only valid with --rebase."));
Previous: Alex Henrie
Message 2 of 2 in “Give git-pull a --reset option”
  1. Give git-pull a --reset optionAlex Henrie, Apr 17, 2019
  2. Eric SunshineApr 17, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.