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

[PATCH v3 0/2] checkout -m: recreate conflict labels

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Oct 9, 2026, 09:13 UTC
Message-ID
<cover.1791537203.git.phillip.wood@dunelm.org.uk>
In-Reply-To
<cover.1790761727.git.phillip.wood@dunelm.org.uk>

When "git checkout -m <path>" recreates a merge conflict, it uses the labels "base", "ours", "theirs", rather than the labels used by the original merge. This short series teaches the ort machinery to write the labels to ".git/MERGE_LABELS" when it switches to a merge result containing conflicts, so that "git checkout -m" can then read that file and use the same labels.

Thanks to Junio and Johannes for their comments.
Changes since V2:
 - only write ".git/MERGE_LABELS" when there are conflicts and added
   a check to an existing "checkout -m <branch>" test
 - change write_merge_labels() to take an array of labels
 - use a local variable to store the internal merge state when writing
   labels
 - use strbuf_detach() rather than xmemdupz() when reading labels
 - add a comment to say we ignore trailing cruft when reading the
   labels file
Changes since V1:
 - use strbuf_getline() rather than strbuf_read_file() to read labels
   so that the newline handling of the reading and writing sides match.

NB ".git/MERGE_LABELS" is still undocumented - I'm hoping to find time to add some documentation for all the MERGE_* files in a future series. Johannes suggested using an index extension to store the labels, but as we already have MERGE_MODE, MERGE_RR and MERGE_MSG I think it is easier just to add another file.

base-commit: 3bc0341126508f78f5869cbfc0005e987efdf0c7
Published-As: https://github.com/phillipwood/git/releases/tag/pw%2Fconflict-labels%2Fv3
View-Changes-At: https://github.com/phillipwood/git/compare/3bc034112...182edb2e8
Fetch-It-Via: git fetch https://github.com/phillipwood/git pw/conflict-labels/v3
Phillip Wood (2):
  remove_branch_state: convert boolean argument to flags
  merge: remember conflict labels
 branch.c           | 17 ++++++++----
 branch.h           |  4 ++-
 builtin/checkout.c | 30 +++++++++++++++++----
 builtin/commit.c   |  1 +
 merge-ort.c        | 20 ++++++++++++++
 merge.c            | 66 ++++++++++++++++++++++++++++++++++++++++++++++
 merge.h            |  3 +++
 path.c             |  1 +
 path.h             |  1 +
 repository.c       |  1 +
 repository.h       |  1 +
 sequencer.c        |  1 +
 t/t7201-co.sh      | 22 ++++++++++++++++
 13 files changed, 157 insertions(+), 11 deletions(-)
Range-diff against v2:
1:  86ef0f848a = 1:  86ef0f848a remove_branch_state: convert boolean argument to flags
2:  18bdf7df49 ! 2:  182edb2e88 merge: remember conflict labels
    @@ merge-ort.c: struct merge_options_internal {
      	struct string_list conflicted_submodules;
     +
     +	/* Copies of the labels used for conflict markers */
    -+	char *labels[3];
    ++	const char *labels[3];
      };
      
      struct conflicted_submodule_item {
    @@ merge-ort.c: void merge_switch_to_result(struct merge_options *opt,
      		}
      		trace2_region_leave("merge", "write_auto_merge", opt->repo);
     +
    -+		trace2_region_enter("merge", "write_merge_labels", opt->repo);
    -+		opt->priv = result->priv;
    -+		write_merge_labels(opt->repo, opt->priv->labels[0], opt->priv->labels[1],
    -+				   opt->priv->labels[2]);
    -+		opt->priv = NULL;
    -+		trace2_region_leave("merge", "write_merge_labels", opt->repo);
    ++		if (!result->clean) {
    ++			struct merge_options_internal *priv = result->priv;
    ++
    ++			trace2_region_enter("merge", "write_merge_labels", opt->repo);
    ++			write_merge_labels(opt->repo, priv->labels);
    ++			trace2_region_leave("merge", "write_merge_labels", opt->repo);
    ++		}
      	}
      	if (display_update_msgs)
      		merge_display_update_messages(opt, /* detailed */ 0, result);
    @@ merge.c: int checkout_fast_forward(struct repository *r,
      	return 0;
      }
     +
    -+int write_merge_labels(struct repository *r, const char *base,
    -+			  const char *ours, const char *theirs)
    ++int write_merge_labels(struct repository *r, const char *labels[3])
     +{
     +	FILE *f = fopen_or_warn(git_path_merge_labels(r), "w");
     +
     +	if (!f)
     +		return -1;
     +
    -+	fprintf(f, "%s\n%s\n%s\n", base, ours, theirs);
    ++	fprintf(f, "%s\n%s\n%s\n", labels[0], labels[1], labels[2]);
     +	if (fclose(f))
     +		return error_errno("could not write '%s'",
     +				   git_path_merge_labels(r));
     +
     +	return 0;
     +}
     +
    -+static char *parse_merge_label_line(struct strbuf *buf, FILE *fp)
    -+{
    -+	if (strbuf_getline(buf, fp) == EOF)
    -+		return NULL;
    -+
    -+	return xmemdupz(buf->buf, buf->len);
    -+}
    -+
    -+int read_merge_labels(struct repository *r,
    -+		      char **pbase, char** pours, char** ptheirs)
    ++static char *parse_merge_label_line(FILE *fp)
     +{
     +	struct strbuf buf = STRBUF_INIT;
    ++
    ++	if (strbuf_getline(&buf, fp) == EOF) {
    ++		strbuf_release(&buf);
    ++		return NULL;
    ++	}
    ++
    ++	return strbuf_detach(&buf, NULL);
    ++}
    ++
    ++int read_merge_labels(struct repository *r,
    ++		      char **pbase, char **pours, char **ptheirs)
    ++{
     +	char *base = NULL, *ours = NULL, *theirs = NULL;
     +	int ret = -1;
     +	FILE *fp = fopen(git_path_merge_labels(r), "r");
     +
     +	if (!fp)
     +		return -1;
     +
    -+	base = parse_merge_label_line(&buf, fp);
    ++	base = parse_merge_label_line(fp);
     +	if (!base)
     +		goto out;
     +
    -+	ours = parse_merge_label_line(&buf, fp);
    ++	ours = parse_merge_label_line(fp);
     +	if (!ours)
     +		goto out;
     +
    -+	theirs = parse_merge_label_line(&buf, fp);
    ++	theirs = parse_merge_label_line(fp);
     +	if (!theirs)
     +		goto out;
    ++	/* We ignore any trailing lines */
     +
     +	ret = 0;
     +	*pbase = base;
    @@ merge.c: int checkout_fast_forward(struct repository *r,
     +		free(theirs);
     +	}
     +	fclose(fp);
    -+	strbuf_release(&buf);
     +
     +	return ret;
     +}
    @@ merge.h: int checkout_fast_forward(struct repository *r,
      			  const struct object_id *from,
      			  const struct object_id *to,
      			  int overwrite_ignore);
    -+int write_merge_labels(struct repository *r,
    -+		       const char *base, const char *ours, const char *theirs);
    ++int write_merge_labels(struct repository *r, const char *labels[3]);
     +int read_merge_labels(struct repository *r,
     +		      char **base, char **ours, char **theirs);
      
    @@ sequencer.c: static int pick_commits(struct repository *r,
      		struct todo_item *item = todo_list->items + todo_list->current;
     
      ## t/t7201-co.sh ##
    +@@ t/t7201-co.sh: test_expect_success 'checkout -m with dirty tree' '
    + 
    + 	fill 0 1 2 3 4 5 6 7 8 >one &&
    + 	git checkout -m side >messages &&
    ++	test_path_is_missing .git/MERGE_LABELS &&
    + 
    + 	test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
    + 
     @@ t/t7201-co.sh: test_expect_success 'format of merge conflict from checkout -m' '
      	d
      	>>>>>>> local
-- 
2.56.0.134.g299a3c16181
Previous: Junio C HamanoNext: Phillip Wood
Message 23 of 27 in “checkout -m: recreate conflict labels”
  1. 0/2 checkout -m: recreate conflict labelsPhillip Wood, Sep 30, 2026
  2. 1/2 remove_branch_state: convert boolean argument to flagsPhillip Wood, Sep 30, 2026
  3. 2/2 merge: remember conflict labelsPhillip Wood, Sep 30, 2026
  4. Junio C HamanoSep 30, 2026
  5. Phillip WoodOct 1, 2026
  6. Junio C HamanoOct 1, 2026
  7. Johannes SixtSep 30, 2026
  8. Junio C HamanoSep 30, 2026
  9. Johannes SixtSep 30, 2026
  10. Phillip WoodOct 1, 2026
  11. 0/2 checkout -m: recreate conflict labelsPhillip Wood, Oct 5, 2026
  12. 1/2 remove_branch_state: convert boolean argument to flagsPhillip Wood, Oct 5, 2026
  13. 2/2 merge: remember conflict labelsPhillip Wood, Oct 5, 2026
  14. Junio C HamanoOct 5, 2026
  15. Phillip WoodOct 6, 2026
  16. Junio C HamanoOct 5, 2026
  17. Phillip WoodOct 6, 2026
  18. Junio C HamanoOct 6, 2026
  19. Phillip WoodOct 7, 2026
  20. Johannes SixtOct 5, 2026
  21. Phillip WoodOct 5, 2026
  22. Junio C HamanoOct 5, 2026
  23. 0/2 checkout -m: recreate conflict labelsPhillip Wood, Oct 9, 2026
  24. 1/2 remove_branch_state: convert boolean argument to flagsPhillip Wood, Oct 9, 2026
  25. 2/2 merge: remember conflict labelsPhillip Wood, Oct 9, 2026
  26. Junio C HamanoOct 10, 2026
  27. Junio C HamanoOct 9, 2026

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.