Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search git branches for file in common

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.
like image 901
BLaZuRE Avatar asked Feb 25 '16 07:02

BLaZuRE


People also ask

How do I search for a specific file in git?

As stated, use gitk --all, then in View | New view, enable All Branches. Then set your search criteria: filenames (with wild cards) in the penultimate field. Finally: OK.

How can you find out the differences between the files in two different branches?

The git diff command is used to perform the diff function on Git data sources. For example, commits, branches, files, and so on. It can also be used to compare two files of different branches.

How can you tell if two branches are identical?

In order to compare two branches easily, you have to use the “git diff” command and provide the branch names separated by dots. Using this command, Git will compare the tip of both branches (also called the HEAD) and display a “diff” recap that you can use to see modifications.


2 Answers

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
like image 77
VonC Avatar answered Oct 03 '22 21:10

VonC


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 )
}
like image 31
jthill Avatar answered Oct 03 '22 21:10

jthill