Git

Git Stop Tracking File

Git is an influential technology for any modern developer.

Git is very intuitive and provides some restrictions to prevent you from making mistakes.

Today, we will be discussing the gitignore file and how we can stop tracking a file in a git repository without removing it from your project.

What is a gitignore file?

The gitignore file allows you to specify a list of file names and directories that git will not track.

Any files or directories specified within the gitignore file will be excluded from the repository. Any changes made to those files will not be available in the commit history.

You can use the gitignore file to hide sensitive files such as environment configurations, API keys, etc.

You can also exclude non-essential files such as editor configurations, binary files, execution and debugging logs, etc.

Where can I find the gitignore file?

The gitignore file is located at the root of any git repository. You can view the file using the ls command:

$ ls -la

How to add a file to gitignore?

You can add a file or directory to the list of gitignore by editing the gitignore file.

Open the file with your favorite text editor:

$ nano .gitignore

You can then add the filename or directory you wish to ignore as an entry in the file.

Git ignore file that has already been committed.

Unfortunately, if you have already committed the file to the repository, simply adding it to the gitignore file will not stop git from tracking it.

For that, you will need to perform a few extra steps.

Before running the commands shown in the steps below, ensure that the files and directories you wish to ignore are added to the gitignore file.

NOTE and ENSURE that any local changes have been committed. This will prevent you from losing any changes made to the repository.

Run the commands:

$ git rm –r --cached

The command above will remove the files from the index.

Next, add the files to the staging area with the command:

$ git add .

Finally, apply a commit to update your repository with the ignored files and directories.

$ git commit -m "fixed gitignore not working."

Feel free to apply your commit message as you see fit.

Conclusion

In this one, we covered how to allow git to stop tracking a file or directory using the gitignore file. We also discussed preventing tracking files even if it has already been committed to the repository.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list