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

10,973

Solution 1

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)

Solution 2

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>

Share:
10,973
BetweenTwoTests
Author by

BetweenTwoTests

Updated on June 04, 2022

Comments

  • BetweenTwoTests
    BetweenTwoTests almost 2 years

    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.

  • BetweenTwoTests
    BetweenTwoTests about 12 years
    It is not that only a single person can generate the files properly; everybody can. However, they are different for each person, so if one person pushes it, (s)he creates a problem for everybody else.
  • BetweenTwoTests
    BetweenTwoTests about 12 years
    You mean push including the .gitignore, right? That might work well.
  • Andrew T Finnell
    Andrew T Finnell about 12 years
    @jpalecek I completely understand that. Which is why I suggested you use git rm to untrack the files so your .gitignore will work. You also seemed to suggest that .gitignore wasnt what you were looking for, but perhaps it was just because you didnt know you could git rm the files. So I provided two different solutions: Use git rm to untrack the files so you can use .gitignore, or tell git to ignore changes with update-index.
  • KurzedMetal
    KurzedMetal about 12 years
    @jpalecek If you don't commit the .gitignore file to the repository, you'd be the only one ignoring the files.
  • trusktr
    trusktr about 10 years
    @AndrewFinnell Does git update-index --assume-unchanged <dir> get applied and pushed back to origin so that the setting takes effect fore everyone? Or does the setting take effect only in the repository I've run the command in?
  • trusktr
    trusktr about 10 years
    I'd like a feature such that if you add a file to .gitignore after it's tracked future changes to the file will not be commited with you do something like git add .. That'd be a nice feature. Perhaps a feature you could enable with some git config option.
  • chharvey
    chharvey over 9 years
    @KurzedMetal please edit/explain step 3, it is confusing and/or has a typo.
  • KurzedMetal
    KurzedMetal over 9 years
    After adding .gitignore and deleting the file you gotta commit the changes and push.