How do you ignore a file in Git without deleting it?

How do you ignore a file in Git without deleting it?

a) Add the file to .gitignore
b) Use git rm --cached filename
c) Delete the file locally
d) Use git reset filename

Answer:

a) Add the file to .gitignore

Explanation:

To ignore a file in Git without deleting it, you can add the file to the .gitignore file. This prevents Git from tracking changes to the file in future commits but leaves the file intact in your working directory.

For example, adding config.php to .gitignore will prevent Git from tracking changes to config.php, while keeping it available for use locally.

This is useful for ignoring sensitive files or configuration files that should not be committed to the repository.

Reference:

Top 100 Git and GitHub MCQ Questions and Answers

Scroll to Top