On my branch I had some files in .gitignore
On a different branch those files are not.
I want to merge the different branch into mine, and I don\'t care if t
Update - a better version
This tool (https://github.com/mklepaczewski/git-clean-before-merge) will:
git pull equivalents,git pull equivalents,git pull version,--pretend option that will not modify any files.Old version
How this answer differ from other answers?
The method presented here removes only files that would be overwritten by merge. If you have other untracked (possibly ignored) files in the directory this method won't remove them.
The solution
This snippet will extract all untracked files that would be overwritten by git pull and delete them.
git pull 2>&1|grep -E '^\s'|cut -f2-|xargs -I {} rm -rf "{}"
and then just do:
git pull
This is not git porcelain command so always double check what it would do with:
git pull 2>&1|grep -E '^\s'|cut -f2-|xargs -I {} echo "{}"
Explanation - because one liners are scary:
Here's a breakdown of what it does:
git pull 2>&1 - capture git pull output and redirect it all to stdout so we can easily capture it with grep.grep -E '^\s - the intent is to capture the list of the untracked files that would be overwritten by git pull. The filenames have a bunch of whitespace characters in front of them so we utilize it to get them.cut -f2- - remove whitespace from the beginning of each line captured in 2.xargs -I {} rm -rf "{}" - us xargs to iterate over all files, save their name in "{}" and call rm for each of them. We use -rf to force delete and remove untracked directories.It would be great to replace steps 1-3 with porcelain command, but I'm not aware of any equivalent.