.gitignore and "The following untracked working tree files would be overwritten by checkout"

So I added a folder to my .gitignore file.

Once I do a git status it tells me

# On branch latest
nothing to commit (working directory clean)

However, when I try to change branches I get the following:

My-MacBook-Pro:webapp marcamillion$ git checkout develop
error: The following untracked working tree files would be overwritten by checkout:
    public/system/images/9/thumb/red-stripe.jpg
    public/system/images/9/original/red-stripe.jpg
    public/system/images/8/thumb/red-stripe-red.jpg
    public/system/images/8/original/red-stripe-red.jpg
    public/system/images/8/original/00-louis_c.k.-chewed_up-cover-2008.jpg
    public/system/images/7/thumb/red-stripe-dark.jpg
    public/system/images/7/original/red-stripe-dark.jpg
    public/system/images/7/original/DSC07833.JPG
    public/system/images/6/thumb/red-stripe-bw.jpg
    public/system/images/6/original/website-logo.png
    public/system/images/6/original/red-stripe-bw.jpg
    public/system/images/5/thumb/Guy_Waving_Jamaican_Flag.jpg
    public/system/images/5/original/logocompv-colored-squares-100px.png
    public/system/images/5/original/Guy_Waving_Jamaican_Flag.jpg
    public/system/images/4/thumb/DSC_0001.JPG
    public/system/images/4/original/logo.png
    public/system/images/4/original/DSC_0001.JPG
    public/system/images/4/original/2-up.jpg
    public/system/images/3/thumb/logo2.gif
    public/system/images/3/original/logo2.gif
    public/system/images/3/original/Guy_Waving_Jamaican_Flag.jpg
    public/system/images/3/original/11002000962.jpg
    public/system/images/2/thumb/Profile Pic.jpg
    public/system/images/2/original/Profile Pic.jpg
    public/system/images/2/original/02 Login Screen.jpg
    public/system/images/1/original/Argentina-2010-World-Cup.jpg
Please move or remove them before you can switch branches.
Aborting

This is what my .gitignore file looks like:

.bundle
.DS_Store
db/*.sqlite3
log/*.log
tmp/**/*
public/system/images/*
public/system/avatars/*

How do I get this working so I can switch branches without deleting those files?

If I make a change, will it affect those files? In other words, if I came back to this branch afterwards would everything be perfect as up to my latest commit?

I don't want to lose those files, I just don't want them tracked.


Solution 1:

WARNING: it will delete untracked files, so it's not a great answer to the question being posed.

I hit this message as well. In my case, I didn't want to keep the files, so this worked for me:

git 2.11 and newer

git clean  -d  -f .

older git

git clean  -d  -f ""

If you also want to remove files ignored by git, then execute the following command.

BE WARNED!!! THIS MOST PROBABLY DESTROYS YOUR PROJECT, USE ONLY IF YOU KNOW 100% WHAT YOU ARE DOING

git 2.11 and newer

git clean  -d  -fx .

older git

git clean  -d  -fx ""

http://www.kernel.org/pub/software/scm/git/docs/git-clean.html

  • -x means ignored files are also removed as well as files unknown to git.

  • -d means remove untracked directories in addition to untracked files.

  • -f is required to force it to run.

Solution 2:

Warning: This will delete the local files that are not indexed

Just force it : git checkout -f another-branch

Solution 3:

It seems like you want the files ignored but they have already been commited. .gitignore has no effect on files that are already in the repo so they need to be removed with git rm --cached. The --cached will prevent it from having any effect on your working copy and it will just mark as removed the next time you commit. After the files are removed from the repo then the .gitignore will prevent them from being added again.

But you have another problem with your .gitignore, you are excessively using wildcards and its causing it to match less than you expect it to. Instead lets change the .gitignore and try this.

.bundle
.DS_Store
db/*.sqlite3
log/*.log
tmp/
public/system/images/
public/system/avatars/

Solution 4:

If you're on OS X, it may be because a file's name has had certain characters change case. Try setting the following config option:

git config core.ignorecase true

Solution 5:

Git is telling you that it wants to create files (named public/system/images/9/... etc), but you already have existing files in that directory that aren't tracked by Git. Perhaps somebody else added those files to the Git repository, and this is the first time you have switched to that branch?

There's probably a reason why those files in your develop branch but not in your current branch. You may have to ask your collaborators why that is.

how do I get this working so I can switch branches without deleting those files?

You can't do it without making the files disappear somehow. You could rename public to my_public or something for now.

if I came back to this branch afterwards would everything be perfect as up to my latest commit?

If you commit your changes, Git won't lose them. If you don't commit your changes, then Git will try really hard not to overwrite work that you have done. That's what Git is warning you about in the first instance here (when you tried to switch branches).