Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • whenever anybody changes its contents in the upstream repository, I don't get any merge conflicts even if my copy is changed too.
  • whenever I change its contents, any commits I make won't reflect these changes

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.

like image 809
jpalecek Avatar asked Dec 04 '22 04:12

jpalecek


2 Answers

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>

like image 175
Andrew T Finnell Avatar answered Dec 07 '22 21:12

Andrew T Finnell


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)

like image 41
KurzedMetal Avatar answered Dec 07 '22 23:12

KurzedMetal