Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie, we are going to learn how to tell Git to ignore tracked files, that is files that has already started tracking, we want to ignore them after the fact. When we were looking gitignore previously, we were looking mainly at ignoring new files. So a new file is out of the repo, we don't want to track it, so we tell Git that it ought to ignore it rather than continue to show it to us. But you should note that Git will not ignore a file that was already tracked before a rule was added to the gitignore file, telling it that it ought to ignore it.
If that's the case Git will still try to keep track of the file and the file must first be untracked. Let me show you what I mean. Let's say that I have a new file here, tempfile2.txt, I'm going to drop this into my working directory. If I now do git status, you will see that it comes up and tells me that now this new file has been added. Now before we do a gitignore rule for it, what we want to do is commit it to our repository. So let's do git add tempfile2 and then git commit with the message, "Adding tempfile2.txt to repo before ignoring it".
Okay, so it's now been added to our repository, git status, the working directory is clean, and we look and here is the file. So we've just committed a file. That's all we have done, just a basic commit. Now that Git is tracking the file, let's tell it that we wanted to ignore changes to this file in the future. So let's open up our gitignore, so if you remember that's right here in the root of the project directory. We can open this file up and edit in a number of ways, because it has the dot in front of it, it's not visible here from this window, which makes it a little bit tricky.
So I am going to use the Command Line, and I am going to use nano which is a Unix program, that will let me edit, gitignore and just right here below tempfile.txt let's add a new line tempfile2.txt. Ctrl+X to Exit, Y for Yes to save changes and Return to keep the file name. So now it's been told that it ought to ignore it, and of course gitignore shows up now on our status list is something that has edits that needs to be committed to the repository. Let's not do that just yet.
Instead let's go over here to tempfile2, and let's make an edit. This is the second temporary file. If we come here, say git status, notice that even though we told it to ignore it, it did not ignore our changes, it's still kept track of our changes, because it's a tracked file, so it still sees it. And it doesn't matter if we were to have committed gitignore to the repository first or not, that's not why it's doing this, it's because Git still keeps track of these tracked files. Instead, what we have to do is tell Git to stop tracking it.
One way we could do that is just to remove it, right? Git remove that would do it, git remove tempfile2.txt. It would remove it from the repository, and it would remove our version, it would do both. What if we didn't want to remove our version? What if this was a log file? Of what if it's a set of images or something or Photoshop files that we want, and we want to keep on ours but we want to tell it, you know what, stop tracking it? We may even want to leave it in the repository so that it stays in the repository for other people to download.
We just want to ignore changes that happened to it after that, right? That certainly might be the case with like a log file or something. We want to have a placeholder for the log file that everyone can have, but we don't necessarily want to have changes to that log file be tracked. Well, what we want to do here instead of just remove is a remove with the cached option. So --cached, that's going to tell it to remove this file from the staging index, not from the repository, just from the staging index.
That will cause the file to stop being tracked. It will still leave the copy in the repo, it will still leave the copy in my working directory. It's just going to take it out of the index. So let's go ahead and hit Return, it will say that it removed it, I do git status now, you will see it comes up and says that it was deleted, that's the commit that it wants to make is to delete the file. However, if I come back over here, you will see that the file is still here, it has not deleted it. So let's go ahead and add our gitignore file, so now both of those are ready to go, here they are, ready to be committed.
And let's git commit -m, and we are going to say, "Remove tempfile2.txt from staging index" and then hit Return. Now if we do git status, we will see that the working directory is clean, but it is still listed here in my directory. It did not get rid of it. We make more changes to it, let's open it up, let's say These changes will not be tracked anymore.
Save it, close it up, git status, and now it's not being tracked anymore. So if you want Git to ignore files that are already being tracked, but you don't want to remove them completely, then what you need to do is not only ignore it, but tell Git that it ought to also remove them from the staging index or the cache.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98974 Viewers
61 Video lessons · 86265 Viewers
71 Video lessons · 70107 Viewers
56 Video lessons · 102324 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.