How to ignore a file or directory in git, be it tracked, untracked or even part of commit
Solution 1
If the file is supposed to never get into the repository, fix the problem and do it correctly...
- add the file/pattern to the
.gitignore
file - delete the unwanted files from the repository
- commit to untrack the undesired file and commit
.gitignore
- push the changes
And you'll never have to deal with those unwanted files again because .gitignore
will avoid future git add
s of those files (unless --force
d)
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>
BetweenTwoTests
Updated on June 04, 2022Comments
-
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 about 12 yearsIt 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 about 12 yearsYou mean push including the
.gitignore
, right? That might work well. -
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 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 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 about 10 yearsI'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 somegit config
option. -
chharvey over 9 years@KurzedMetal please edit/explain step 3, it is confusing and/or has a typo.
-
KurzedMetal over 9 yearsAfter adding
.gitignore
and deleting the file you gotta commit the changes and push.