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

501    Asked by AadityaSrivastva in Python , Asked on Apr 27, 2021

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:
.......
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 afterward 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.

Answered by Behailu

 The question seems like you want the files ignored but they have already been committed.

For that .gitignore does not affect 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. After the files are removed from the repo then the .gitignore will prevent them from being added again.

But the other problem with your .gitignore, you are excessively using wildcards and it's causing it to match less than you expect it to.

For that see this .gitignore file

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

This will help you move or remove them before you switch branches.



Your Answer

Interviews

Parent Categories