git - continuing after a fatal message

Viewed 179

After resolving a merge conflicts I haved added files to the staging area. Then I have accidentally executed a commit command with the amend option:

git commit --amend --no-edit

I have got the following message:

fatal: You are in the middle of a merge -- cannot amend.

Does it mean that the commit command has not been executed at all ? Can I just continue with the correct command ? :

git commit --no-edit
1 Answers

The error message "You are in the middle of a merge -- cannot amend." is generated by the function parse_and_validate_options() that is called by the function cmd_commit() before it starts the actual work.

The relevant part of the code of the commit command looks starts like this:

    ...
    if (argc == 2 && !strcmp(argv[1], "-h"))
        usage_with_options(builtin_commit_usage, builtin_commit_options);

    status_init_config(&s, git_commit_config);
    s.commit_template = 1;
    status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
    s.colopts = 0;

    if (get_oid("HEAD", &oid))
        current_head = NULL;
    else {
        current_head = lookup_commit_or_die(&oid, "HEAD");
        if (parse_commit(current_head))
            die(_("could not parse HEAD commit"));
    }
    verbose = -1; /* unspecified */
    argc = parse_and_validate_options(argc, argv, builtin_commit_options,
                      builtin_commit_usage,
                      prefix, current_head, &s);
    if (verbose == -1)
        verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose;

    if (dry_run)
        return dry_run_commit(argv, prefix, current_head, &s);
    index_file = prepare_index(argv, prefix, current_head, 0);

    /* Set up everything for writing the commit object.  This includes
       running hooks, writing the trees, and interacting with the user.  */
    if (!prepare_to_commit(index_file, prefix,
                   current_head, &s, &author_ident)) {
        rollback_index_files();
        return 1;
    }

    ...

Before the line index_file = prepare_index(argv, prefix, current_head, 0) the code does not change anything, it validates the command line arguments and identifies the needed objects (the current commit).

The command you have run by mistake didn't change anything in the repository. The safety measures implemented in Git prevented it to break your work. You can safely run the correct command to conclude the merge.

Related