Quickly learn how to ignore files in Git

There are a couple of different ways to ignore files in Git, it really depends on your reasons for ignoring it.

The first and most common reason is that it is something that is important to have in the repo (or repository) but is maybe updated by the server, or something that is user environment specific. The way to ignore files and folders like these is to add them to a .gitignore file. This file is generally created in the route of the project where the .git file and folder exists and it specifically tells  Git where the files are that need to be ignored. The .gitignore file is not created when the Git repo is initiated so you will need to jump in your text editor and create and save a new file called .gitignore (be sure to have the . at the start of the filename).

Below are the different ways you can ignore files –
.DS_Store (ignores all .DS_Store files in the entire Git repo)
/build/server.log (ignores file named ‘server.log’, but only inside the build folder of the Repo)
*.log (ignores all files with the .log extension in the Git repo, the star is a filename wildcard)
/caches/* (ignores all files and folders inside the folder named ‘caches’, the star is a wildcard for all files and folders)

You need to commit your .gitignore file before the files and folders you want to be ignored are commited to the repo. If you have already commited your files or folders you want to ignore you can temporarily remove them and reintroduce them once the .gitignore has been added to the repo.

The other scenario for wanting to ignore files can be for a more contained reason like changing some configuration to suit your local build. This can be potentially dangerous if you commit these files in to live without anybody noticing and they get pushed to the live site especially if you are changing database details etc. To stop the files being accidentally added you can use the assume unchanged command, it works how it sounds. If you change the file it will not show in the git status, so you will never add it by mistake.

git update-index --assume-unchanged filename (this will stop the changes in the file from being picked up in statuses)
git update-index --no-assume-unchanged filename (this will add the changes in the file back to being picked up in statuses)

This is not a foolproof approach though and you will need be sure that when you do make changes in the file that you wish to commit that you revert the old changes.

These are just the two most common ways to ignore files and folders in Git and what situations to use them in.


Yes / No