The previous tutorial has explained the addition of an empty file or directory to the GIT using .gitkeep. This tutorial explains how to not stage certain files in GIT (ignored file).
What type of files are ignored?
Ignored files are usually built artifacts and machine-generated files that can be derived from your repository source or should otherwise not be committed. Some common examples are:
- dependency caches, such as the contents of
- compiled code, such as
- build output directories, such as
- files generated at runtime, such as
- hidden system files, such as
- personal IDE config files, such as
Imagine we have 100 files, and we want to commit only 99 files and ignore 1 file. This seems to be a very difficult situation.
To add the files to the GIT Staging area, we can use git add command. If we use git add ., it will add all 100 files to the GIT Staging area. Another way is to mention git add file1, file2….. soon, which is also a very tedious task.
Ignored files are tracked in a special file named as .gitignore named which is checked in at the root of the repository. We need to mention the file we want to ignore in .gitignore file.
Let me explain how to use .gitignore with the help of an example:-
Step 1 – Create a GIT Repository named as GitTest.
Step 2 – Right-click and click on Git Bash Here to open GitBash at that place.
Step 3 – Type git status to check if any file is not committed.
Step 4 – Create a new directory – config.
Step 5 – Create a file named test.class into the directory.
Step 6 – Type git status to check that config folder is untracked.
Step 7 – Create a .gitignore file.
Step 8 – Add the folder or file we want to ignore.
echo config/ >> .gitignore
Step 9 – Type git status again. This time, we can see that the untracked file is .gitignore not the config directory.
Congratulations!!! We are done. Have a happy Learning!!