Ignoring files that are not present in the destination branch when merging a source branch into it with git

83 views Asked by At

Hello my fellow SW developers,

I have two branches with different .gitignore files in my git repository, develop and feature/my_feature. There are files that are present in feature/my_feature that are not present in develop. When merging the branch feature/my_feature back in the develop branch, I would like the files that are not already present in the develop branch to be automatically left out.

I tried the strategy proposed in the respective chapter in the Git Book as well as the StackOverflow post Git - Ignore files during merge, but the files that are missing in the develop branch cause a conflict to happen which is not automatically solved.

I also tried the strategy proposed in the StackOverflow post (Git Merging) When to use 'ours' strategy, 'ours' option and 'theirs' option?, using the following bash script custom-merge.sh (which has been placed in the root directory of my repository for testing purposes) for the defined strategy custom:

#!/bin/sh
# Custom merge script to handle conflicts caused by files not present in current branch

# Check if the file exists in the current branch
if test -e "$1"; then
    # File exists in current branch, use default merge strategy
    git merge-file "$1" "$2" "$3"
else
    # File does not exist in current branch, keep file from branch being merged
    git checkout --theirs -- "$1"
    git add "$1"
fi

I added the strategy to my repository using the following commands:

git config --local merge.custom.name "Custom merge driver to handle conflicts caused by files not present in current branch"
git config --local merge.custom.driver "custom-merge.sh %O %A %B"

Also when trying to add the strategy to the global git config and also using the full path to custom-merge.sh, git returns the following error:

Could not find merge strategy 'custom'.
Available strategies are: octopus ours recursive resolve subtree.

How could I merge the branch feature/my_feature back in the develop branch while automatically exclude the files that are not already present in the develop branch from the merge?

Edit 24.10.2023 08:35:
I don't know if it's important, but I am working on Windows 10 22H2 with git 2.35.1.windows.2.

Thank you in advance for your help!

Best, Francesco

1

There are 1 answers

0
frankieee On BEST ANSWER

I think I have found an acceptable solution.

Placing the following code into the pre-merge-commit hook deletes the ignored files prior to merging:

git restore --staged --worktree -- *.gitignore
git rm --cached --ignore-unmatch $(git ls-files --cached --ignored --exclude-standard)

Explanation

  1. git restore --staged --worktree -- *.gitignore restores all the .gitignore files in the destination branch.
  2. git rm --cached --ignore-unmatch $(git ls-files --cached --ignored --exclude-standard) deletes all the staged files present in all .gitignore files

Drawback

This is almost equivalent to a cleanup of the working tree, which might not be desired.