GIT Tutorials

HOME

Git is a Distributed Version Control System (VCS) which is originally developed in 2005 by Linus Torvalds (Creator of Linux) and is open source, i.e. freely available to use. It is the most popular and most used version control tool right now. A staggering number of software projects rely on Git for version control, including commercial projects as well as open source. 

Chapter 1 How to install Git on Windows 10
Chapter 2 How to create a new Git Repository – git init Command
Chapter 3 How to stage changes in Git – git add Command
Chapter 4 How to unstage the changes in Git – git rm command
Chapter 5 How to commit changes in GIT – git commit command
Chapter 6 How to track commits in Git – git log command
Chapter 7 How to commit an empty folder in GIT – gitkeep
Chapter 8 How to ignore files in GIT – gitignore
Chapter 9 How to create a branch in GIT
Chapter 10 How to stash changes in GIT – git stash command
Chapter 11 How to push new local GIT Repository to GitLab
Advertisement

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