8

I would like to search git branches for any modified files in common between mine and all others, as a method of early conflict detection. Is there a method that git would allow me to do this that I'm overlooking (or via shell scripting)?

The method I've thought of for now is, as a post-commit hook:

  1. Performing a git diff --name-only master on the branch I'm in, to determine all the files I'll be searching for in other branches (to generate a list of conflicting branches)
  2. Searching/grep'ing for each file I've obtained (in previous step) in everyone's other remote branches via git diff --name-only origin/<remote branch> origin/master on the remote repository
  3. Returning a list of branches that contain one or more of the files that conflict, based on the results of my search/grep.
BLaZuRE
  • 2,356
  • 2
  • 26
  • 43

2 Answers2

4

Most of the available methods are listed in "How can I preview a merge in git?"

The second step you mention is one of the fastest (compared to actually perform the merge with --no-ff and --no-commit)

git diff --name-status origin/abranch

(compared to HEAD by default)

You can add a status filter, since you search for modified files (common files between branches with modification)

No need for the first step: the second one will directly list common modified files compared to HEAD.

git diff --name-status --diff-filter=M origin/abranch
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I've revised my question since I feel I was unclear, but the commands in http://stackoverflow.com/a/33267627/1034537 is essentially an example of the bash script I would create in my scenario (except I would loop through all remote branches ). – BLaZuRE Feb 28 '16 at 08:54
  • @BLaZuRE simply loop on your remote tracking branches and do the `git diff --name-status --diff-filter=M origin/abranch` on each. You will get your list of common modified files (candidate for potential conflicts) – VonC Feb 28 '16 at 08:56
3
potentially-conflicting-changes-between () { 
        local base=`git merge-base $1 $2`
        { git diff --name-only $base $1
          git diff --name-only $base $2
        } | sort | uniq -d
}

will show all files that merge would examine for conflicts, then it's just a matter of running the refs, easiest might be

for other in `git branch -r`; do
        printf '--- \n%s %s\n' master $other
        potentially-conflicting-changes-between master $other
done

You can also do the low-level merge prep directly to eliminate false positives on identical changes, this could save much time but will list index details for all potential conflicts and checks whether the merge could actually run in your current worktree without stomping on uncommitted work

potentially-conflicting-changes-between ()
{
    ( export GIT_INDEX_FILE=`git rev-parse --git-dir`/scratch-index;
    git read-tree --empty;
    git read-tree -m $(git merge-base $1 $2) $1 $2;
    git ls-files -u )
}
jthill
  • 55,082
  • 5
  • 77
  • 137
  • Thanks. . . . hm. It occurs to me my favorite command is just the ticket here. – jthill Feb 28 '16 at 21:23
  • read-tree! I should have known (http://stackoverflow.com/search?q=user%3A1290731+%22read-tree%22) – VonC Feb 28 '16 at 21:32
  • lol. it's behaving perfectly, i just screwed up and committed the same changes to two branches so read-tree didn't report the changes I expected. – jthill Feb 28 '16 at 21:55