How to track files or directories included in gitignore?

You can force add ignored files or directories included in gitignore file using git add -f command.

gitignore file is used to ignore any files or directories that you don’t want git to track. Once you add a gitingore file containing details on files or directories that you want git to ignore, git will not track any new file or directory.

Example:

Create a new git local repository:

Create a change and commit to your local repository default branch:

Add a gitignore file to exclude mydir, myfile and itself:

Check the git status:

Create a new file and directory included in your gitignore file:

Check the git status:

Observe: Since the new file and directory are added in the exclusion list of your gitignore file they were not being tracked by git.

Stage the new file and directory and check if they are being tracked:

Observe: Since the new file and directory are added in the exclusion list of your gitignore file they were not being tracked by git.

Track files and directories included in gitognore file:

Now check the git status:

Observe: Since we have forcefully added the new file and directory, they are being tracked by git though included in .gitignore file.

Leave a Reply