Git rightly gives an error when you do a git pull
when the commit(s) merged would create a file that you have locally but is not tracked.
error: The following untracked working tree files would be overwritten by merge
I would like to detect this situation in a script so that I can do something about it before doing the git pull for real.
How can I do this? I tried git pull --dry-run
but annoyingly that does not notice that the pull would fail (git version 2.11.0)
Test
mkdir a b; cd a
git init .; echo hi >file1 ; git add .; git commit -m "initial commit"
cd ../
git clone a b
cd a; echo boo>file2; git add .; git commit -m "2nd commit"
cd ../b
echo xxx>file2
git fetch
if some_git_command_detects_pull_would_fail
then
echo 'Uh oh!'
fi
Nb. I realise I can "clean" out untracked files; that untracked files are nasty; that I can stash changes etc. but this is not what I want.