Skip to content

Instantly share code, notes, and snippets.

@stracker-phil
Created February 23, 2026 19:47
Show Gist options
  • Select an option

  • Save stracker-phil/5061b29a3e931af5192888d0dcd74d60 to your computer and use it in GitHub Desktop.

Select an option

Save stracker-phil/5061b29a3e931af5192888d0dcd74d60 to your computer and use it in GitHub Desktop.
Runs "git log" against two branches to help find out, which one introduced a code change

Resolving Merge Conflicts

The inspect-conflict.sh script compares a single file on two branches to find out which branch introduced a specific code changes. This documentation explains, how the log inside the script evolved and what it does.

Rules

  1. Resolve one file at a time
  2. First, apply the upstream version
  3. Unstage all changes of that file
  4. RESOLVE: Review each change one-by-one and either stage or discard

Reason for accepting the upstream version:
This step turns the 3-way-merge into a more digestible 2-way changeset

Stage or Discard?

When code exists in the working branch but not in upstream, we need to find out what happened:

  • A) It was removed from upstream → discard it
  • B) it was added in the working branch → keep it

But how to find out which strategy is the correct one?

Investigation Steps

1. Check Upstream History

git log main -S"method_name" -- path/to/file
  • Shows commits → Go to step 3
  • No results → Likely option B, verify with step 2

2. Check Feature Branch History

git log feature-branch -S"method_name" -- path/to/file
  • Shows commit → Option B (keep it)
  • No results → Check merge base

3. Verify Current State

# Confirm presence in each branch
git show main:path/to/file | grep -A5 "method_name"
git show feture-branch:path/to/file | grep -A5 "method_name"

4. Inspect Addition Context (if keeping)

# See why it was added
git show $(git log feature-branch -S"method_name" --format=%H -1)

# Find usage
git grep "method_name" feature-branch

Example

# Check upstream (no results = not in upstream)
git log develop -S"api.subscription_mode" -- modules/ppcp-api-client/services.php

# Control: verify search works
git log develop -S"api.helpers.paymentLevelHelper" -- modules/ppcp-api-client/services.php
# Returns 1 commit ✓

# Check current branch (found it)
git log remove-legacy-ui -S"api.subscription_mode" -- modules/ppcp-api-client/services.php
# Returns 1 commit → Option B: Keep the service

Result: Service added in current branch, keep it in merge resolution.

#!/usr/bin/env bash
# Helps me determine whether a code snippet was added in the feature branch
# or removed from upstream — so you can decide: keep or discard?
prompt_if_empty() {
local var_name="$1"
local label="$2"
if [[ -z "${!var_name}" ]]; then
read -rp "${label} " input
printf -v "$var_name" '%s' "$input"
else
echo "$label ${!var_name}"
fi
}
function show_usage() {
echo "Usage: $(basename "$0") [-f <file>] [-s <search>] [-u <upstream>]"
echo ""
echo " -f, --file Path to the file"
echo " -s, --search Search term (method name, variable, etc.)"
echo " -u, --upstream Upstream branch name"
}
while [[ $# -gt 0 ]]; do
case "$1" in
-f|--file) FILE_PATH="$2"; shift 2 ;;
-s|--search) SEARCH_TERM="$2"; shift 2 ;;
-u|--upstream) UPSTREAM_BRANCH="$2"; shift 2 ;;
-h|--help) show_usage; exit 0 ;;
*) echo "Unknown option: $1"; show_usage; exit 1 ;;
esac
done
CURRENT_BRANCH=$(git branch --show-current)
prompt_if_empty FILE_PATH "File path: "
prompt_if_empty SEARCH_TERM "Search term: "
echo "Current branch: $CURRENT_BRANCH"
prompt_if_empty UPSTREAM_BRANCH "Upstream branch:"
echo ""
echo "--- Upstream ($UPSTREAM_BRANCH) ---"
UPSTREAM_RESULTS=$(git log "$UPSTREAM_BRANCH" -S"$SEARCH_TERM" --oneline -- "$FILE_PATH")
echo "${UPSTREAM_RESULTS:-No commits found}"
echo ""
echo "--- Feature branch ($CURRENT_BRANCH) ---"
FEATURE_RESULTS=$(git log "$CURRENT_BRANCH" -S"$SEARCH_TERM" --oneline -- "$FILE_PATH")
echo "${FEATURE_RESULTS:-No commits found}"
echo ""
echo "--- Verdict ---"
if [[ -z "$UPSTREAM_RESULTS" && -n "$FEATURE_RESULTS" ]]; then
echo "KEEP — added in this branch, not present upstream"
elif [[ -n "$UPSTREAM_RESULTS" && -z "$FEATURE_RESULTS" ]]; then
echo "DISCARD — removed from upstream, not introduced here"
elif [[ -n "$UPSTREAM_RESULTS" && -n "$FEATURE_RESULTS" ]]; then
echo "REVIEW — found in both branches, likely modified"
else
echo "INCONCLUSIVE — no commits found in either branch"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment