GIT – How To Ignore Files From Repository?


How To Ignore Files From Repository?

Table Of Contents:

  1. What Is GIT Ignore?
  2. How To Ignore Files In GIT.
  3. Examples Of GIT Ignore.

(1) What Is GIT Ignore ?

  • Sometimes you don’t want some of the files to be tracked by the GIT.
  • These files you generated may be for demo or testing purposes only.
  • These files may only for your reference purpose only.
  • Then you can ignore those files so that they won’t be tracked by the GIT.

(2) How To Ignore Files In GIT ?

  • ‘.gitignore’ file is used to ignore files from your repository.
  • You need to mention the file names inside the.gitignore‘, so that GIT will ignore those files.
  •  The .gitignore’ file is placed in the root folder of the repository.
  • There is no explicit git ignore command; instead, the .gitignore file must be edited and committed by hand when you have new files that you wish to ignore

Step-1: Create The .gitignore File

  • Create a file named .gitignore if you do not have it already in your directory.
  • To create a file, use the command touch or cat. It will use as follows:
touch .gitignore  

Or

cat .gitignore  

Step-2: Add The Files Names Which You Want To Ignore.

  • Now, add the files and directories to the .gitignore file that you want to ignore.
  • To add the files and directory to the .gitignore file, open the file and type the file name, directory name, and pattern to ignore files and directories.

Step-3: Commit The ‘.gitignore’ File

  • Now, GIT to know what are files and folders to be ignored, you have to commit the ‘.gitignore’ file.
  • If you check the status, you will see, The ‘.gitignore’ file is untracked.
git status
  • We have to add the ‘.gitignore’ file to the staging area and then commit it.
git add .gitignore
git commit -m 'Commiting The .gitignore File'

Conclusion:

  • If you check the status, you can see that the ‘demo.txt’ file is not tracked by the GIT.

Leave a Reply

Your email address will not be published. Required fields are marked *