How to ignore files in GIT – gitignore

HOME

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 /node_modules or /packages
  • compiled code, such as .o.pyc, and .class files
  • build output directories, such as /bin/out, or /target
  • files generated at runtime, such as .log.lock, or .tmp
  • hidden system files, such as .DS_Store or Thumbs.db
  • personal IDE config files, such as .idea/workspace.xml

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.

mkdir config

Step 5 – Create a file named test.class into the directory.

touch config/test.class

Step 6 – Type git status to check that config folder is untracked.

Step 7 – Create a .gitignore file.

touch .gitignore

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!!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s