jpalecek
jpalecek

Reputation: 47762

How to ignore a file or directory in git, be it tracked, untracked or even part of commit

I want to completely ignore a part of a git repository. The directory is currently tracked in the repository and I'd like to ensure that

I don't care particularly about the actual contents.

The problem arises with files automatically generated during build. Unfortunately, someone happened to commit them to the repository, but they cause build errors due to different paths etc. when used on different machine than their creator and consequently nasty merge conflicts.

AFAIK, .gitignore won't work for this purpose, as it only applies to untracked files.

Upvotes: 5

Views: 14702

Answers (4)

Levac Onlajn
Levac Onlajn

Reputation: 271

You can try with following line at beginning of .gitignore file :

!.gitignore

I think this should force git to ignore listed firs/files. I don't know is it best way, but it works in my case. e.g.

!.gitignore
my_dir/
some_dir/

Note: Of course you should git add and commit .gitignore than push to origin.

Upvotes: 1

KurzedMetal
KurzedMetal

Reputation: 12946

If the file is supposed to never get into the repository, fix the problem and do it correctly...

  1. add the file/pattern to the .gitignore file
  2. delete the unwanted files from the repository
  3. commit to untrack the undesired file and commit .gitignore
  4. push the changes

And you'll never have to deal with those unwanted files again because .gitignore will avoid future git adds of those files (unless --forced)

Upvotes: 6

Szczepan Hołyszewski
Szczepan Hołyszewski

Reputation: 3167

All answers above fail to handle branches that had split off before gitignoring and removing the unwanted files, and contain commits with changes to those files. Such branches will just sit there waiting for an opportunity to wreak havoc upon a merge or rebase.

Practically speaking, there is no solution other than recreating all repos from scratch with the unwanted files in .gitignore from the very start. Un-tracking stuff is simply thoroughly broken in git.

Git fails to model the simple reality where users are not effing clairvoyant, and just plainly don't know what kind of do-not-track (temporaries | intermediates | caches | whatever) will be created by a framework that hasn't even been written yet, but will be incorporated into the project at some point in the future.

Upvotes: 0

Andrew T Finnell
Andrew T Finnell

Reputation: 13628

You can easily remove and untrack the files with git rm which seems to be what you want to do. If the files are automatically generated during the build process, this seems to be the route you want to take.

However, if only a single person has their machine setup to properly generate these files, I believe a bigger problem exists. But to do what you want, this is the way

Start ignoring changes git update-index --assume-unchanged <dir>

Start caring again git update-index --no-assume-unchanged <dir>

Upvotes: 12

Related Questions