How to configure Git to ignore files and directories using .gitignore?

To configure Git to ignore files and directories using the .gitignore file, follow these steps:

  1. Create a file named ".gitignore" in the root directory of your Git repository. You can use any text editor to create this file.

  2. Open the .gitignore file and add the names of the files or directories you want Git to ignore. Each entry should be on a separate line.

  3. Use wildcards and patterns to specify multiple files or directories. For example, to ignore all files with the .txt extension, use "*.txt". To ignore a directory named "logs", use "logs/".

  4. You can also use negation patterns to exclude specific files or directories from being ignored. For example, if you want to ignore all files in a directory except for one specific file, you can add a negation pattern like "!directory/file.txt".

  5. Save the .gitignore file and commit it to your repository. Git will now ignore the specified files and directories.

Please note that if you have already committed the files you want to ignore, you need to remove them from the Git tracking using the following command before the changes in the .gitignore file take effect:

git rm --cached <file>

Replace <file> with the name of the file or directory you want to remove from Git tracking.