Git Pocket Guide says
Git ignores untracked files while switching branches, unless the file exists in the target branch; then it aborts, even if the versions in the working tree and destination branch are the same. You can use the --merge option to get around this without having to delete the untracked file, only to have Git restore it a moment later. The merge operation results in the same file, in this case.
On branch master
, I have a tracked file hellofile
$ git branch
feature2
* master
$ cat hellofile
hello
$ git status
On branch master
nothing to commit, working tree clean
On branch feature2
, it has no file hellofile
, and I create it but leave it untracked
$ git checkout feature2
$ ls
common reader README writer
$ echo "world" > hellofile
I then try to checkout master
. It is understandable that git checkout
will fail, but with -m
succeed. But why does git checkout -m
succeed without merge? (I am expecting a file hellofile
with marked conflict)
$ git checkout master
error: The following untracked working tree files would be overwritten by checkout:
hellofile
Please move or remove them before you switch branches.
Aborting
$ git branch
* feature2
master
$ git checkout -m master
error: Dirty index: cannot merge (dirty: f2f hellofile)
Switched to branch 'master'
$ cat hellofile
hello