As a part of GIT Tutorials End To End, we will learn about the .gitignore file or ignoring file and directory in git in this post.
I will expect that you are aware of the basic concepts and commands of GIT. But if you are a beginner in GIT then I strongly recommend you to refer GIT Basic Commands and Concepts section on my blog first.
Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.
Why ignore file/s?
When we create a new file in a git repository, it is marked as Untracked by Git. Git will ask you to stage/commit untracked files. But all the files created or autogenerated are not meant to push to the remote repository. For example – a folder named target is auto-created locally by a maven project during the build. This target folder is not required to commit and push to remote. In short, some files and folders need to be marked ignored for git. An ignored file by git will not be tracked for any action on the file.
By default, git will not ignore any file. We need to intentionally let git know about files and directories to be ignored.
The .gitignore file
In fact, there is no command to ignore a file in Git. You must need to mention the file or directory name to be ignored in the .gitignore file. A .gitignore file is generally kept in the root directory and must be committed and pushed to the remote repository. Let’s see these concepts in action.
I just initialized a git repository and created a file FileA. Git marks FileA as untracked.
I want FileA should not be tracked by git. For this to happen I need to create a .gitignore file and add FileA in the .gitignore file. The best way to create a .gitignore file is using the touch command shown below.
Please note when I added FileA in the .gitignore file then git is no longer saying that FileA is untracked like the previous screenshot. FileA is ignored by git now. Anyway .gitignore is shown as untracked which we should commit and push to remote.
Please note that a file whose name starts with period (.) is hidden from normal view. In git bash, you need to run ls -a command to see it. You also need to do some changes based on the operating system to see these files in the file directory.
If you want to add more files to .gitignore file then either you can use Linux commands or the file directory to edit it. Let me create a new file inside a folder and add it to .gitignore file.
Please note the pattern I used above. Since I have kept the file in demoFolder, so I need to pass it as “demoFolder/FileB”. There are multiple patterns to add in the .gitignore file. We will see some important ones in the next post.
- We can create a .gitignore file using the touch command which works on all OS.
- Files and directories mentioned in the .gitignore file will be ignored by GIT.
- There are multiple patterns to use to mention entries in the .gitignore file.
- We should place the .gitignore file in the root directory and commit and push it to remote.
- We can have more than one .gitignore file in a repo. It is helpful in multimodule projects.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe to my YouTube channel.