SOLVED: git remove file from tracking [Practical Examples]
Finding a quick solution to typical issues in git remove file from tracking will boost your software management experience. It enables you to comfortably wipe off push and pull request problems.
Scenario-1
If you want to ignore a tracked file without deleting it, run this command on the command line:
Assume you commit a file that you were to ignore and later reference the file in the .gitignore file. You add the changes to the staging area and commit them. However, the files keep appearing under tracked files.
The most straightforward steps to clear such errors include committing pending code changes then running this command:
Scenario-2
Other times you work on a file with other software engineers. You want to git remove the file from tracking without deleting it without interfering with your teammates who track the file.
The best command to use in the circumstance is
If you are sure you will not change the file frequently.
Scenario-3
Lastly, you may want to untrack a folder with many files. The recommended command to use is
Let us navigate to the following sections of this tutorial to understand the four standard issue-tracking environments and associated challenges.
Setup Lab Environment
I am creating a remote repo called remove_from_tracking on Github.
Grab the clone URL.
![SOLVED: git remove file from tracking [Practical Examples]](https://www.golinuxcloud.com/wp-content/uploads/git-clone-URL-1-e1637686055249.jpg)
Head over to the terminal or command line and clone the repo into a local directory called golinux .
![SOLVED: git remove file from tracking [Practical Examples]](https://www.golinuxcloud.com/wp-content/uploads/clone-result.png)
cd into the cloned repo, remove_from_tracking .
![SOLVED: git remove file from tracking [Practical Examples]](https://www.golinuxcloud.com/wp-content/uploads/lab-setup-is-ready.png)
Now that the lab setup is complete, you are ready to learn file untracking by following simple steps.
Example-1: git remove file from tracking without deleting it
Let us continue building the repo in readiness for file tracking.
Check tracked files using the command
Currently, only one file, README.md , gets tracked.
![SOLVED: git remove file from tracking [Practical Examples]](https://www.golinuxcloud.com/wp-content/uploads/readMe-file-initially-tracked.png)
Let us create two more files, file1.txt and file2.txt .
shows the two files are untracked.
![SOLVED: git remove file from tracking [Practical Examples]](https://www.golinuxcloud.com/wp-content/uploads/untracked-file1-and-file2.png)
Let us stage the untracked files
and commit them by entering the following commands on the command line or terminal.
shows we are tracking the three files, README.md , file1.txt , and file2.txt .
![SOLVED: git remove file from tracking [Practical Examples]](https://www.golinuxcloud.com/wp-content/uploads/README-file1-and-file2-tracked.png)
Assume we decide to untrack file2.txt . We can git remove the file from tracking by running the command:
to check tracked files.

shows file2.txt is now untracked.
![SOLVED: git remove file from tracking [Practical Examples]](https://www.golinuxcloud.com/wp-content/uploads/file2-is-now-untracked.png)
Using the —cached flag removes the target files from the staging area, without affecting the files in the working directory.
To confirm that git rm —cached <file> command does not alter the working area, open the file using a code editor or list the files and directories using the command
in the remove_from_tracking folder.
Bonus trick
You are probably wondering, «How can I do git remove a file from tracking and delete it from the working area as well?«
Here is the command to untrack and delete a file from the working directory:
Let us apply it.
to stage file2.txt file again, then commit it by running
to confirm that we are tracking file2.txt again.
Remove the file from tracking using this short command
on your command line.
What did you notice?
Using the command
removes files from tracking and deletes them from the working area.
Example-2: git remove file from tracking and solve ignore issues
Sometimes you stage, commit and push a file to a remote repo then realize it was necessary to ignore a file before pushing the changes. You decide to create a .gitignore file and reference your target file in it, but you still face particular challenges, as you are about to see below.
So far, we are tracking two files, README.md and file1.txt .
Create a test.py file and append a new line to it.
git add . , git commit -m » password tracked accidentally» and git push it to the remote repo you created at the beginning of this tutorial.
Check the test.py file’s content on the remote repo. Anybody can see our password.
![SOLVED: git remove file from tracking [Practical Examples]](https://www.golinuxcloud.com/wp-content/uploads/password-leaked-on-remote-repo-e1637686244720.jpg)
Let us do a git remove file from tracking by creating a .gitignore file and referencing the test.py file in it.
Track the changes.
Check tracked files.
We expected the test.py to be removed from of the tracking zone because we now ignore it, but it did not disappear from tracked files.
![SOLVED: git remove file from tracking [Practical Examples]](https://www.golinuxcloud.com/wp-content/uploads/python-file-still-tracked.png)
Let us remove it from the tracking space
and check the changes.
test.py is out of the tracked files, but still referenced in .gitignore .
![SOLVED: git remove file from tracking [Practical Examples]](https://www.golinuxcloud.com/wp-content/uploads/python-file-is-successfully-ignored.png)
Let us also update the remote repo.
The remote repo lacks test.py file.
![SOLVED: git remove file from tracking [Practical Examples]](https://www.golinuxcloud.com/wp-content/uploads/password-secured-e1637687529376.png)
Knowing how to ignore tracked files helps secure files promptly. What if you want to git remove file from tracking without stopping your teammates from tracking its changes? Let us take a look at that scenario.
Example-3: Remove file from tracking from all git pull requests
We can untrack a file on every pull request while letting other engineers continue tracking it by doing the following.
Assume we want to untrack the README.md . We can run this command:
if we don’t alter the file or it takes up massive storage space.
![SOLVED: git remove file from tracking [Practical Examples]](https://www.golinuxcloud.com/wp-content/uploads/untrack-a-file-on-subsequent-pull-request.png)
Now we won’t track the file on subsequent pull requests.
Example-4: Remove entire directory from tracking in git
Assume you have a bunch of files in a folder to untrack. We don’t to waste time doing a cd into the folder to untrack each of the directory’s files. Here is a step-step way to untrack the files from the directory.
Return to the terminal or command line and create a folder, html_files .
cd into the html_files folder and add two files, index.html , and about.html .
Navigate into the parent folder, remove_from_tracking .
shows we are now tracking index.html and about.html .

We later realize we won’t work with HTML. We, therefore, want to untrack index.html and about.html then deleted them.
We can untrack the folder by running this one-line command:
Recheck the tracked files.
![SOLVED: git remove file from tracking [Practical Examples]](https://www.golinuxcloud.com/wp-content/uploads/html-folder-not-tracked.png)
Remember to put a slash \ before the star * to escape from the git commit command. Our files are now untracked and rest on the hard drive.
After untracking the files we can delete them by running the command
Conclusion
Understanding and applying git remove file from tracking will boost your personal experience and ease team interactions when handling files. Now that you know why, when, and how to remove files from tracking, go ahead and enjoy your file tracking workflows.
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
How to Stop Tracking and Start Ignoring in Git
In Git, the ignored files are tracked in a file, called .gitignore. You have an opportunity to add file paths and directory paths that you don’t wish to be tracked by Git in this file. Almost all programmers, working with Git, are eager to learn how to stop tracking files, which were already inserted into a git repository. This snippet is aimed at explaining the process of stopping to track and ignoring changes to a file in Git. The .gitignore will prevent untracked files from being added to the set of files tracked by Git, however, Git will continue to track the files that are already being tracked even when they are added to .gitignore .
Ignoring changes to a file
If you have a tracked file and want to ignore it, you need to add the path of that file or directory into the .gitignore file. Afterwards, you need to tell Git not to track it. For that purpose, you should run the command below:
If you want to do that for the folders instead of files, you should add the -r option to the command above like so:
Suppose, you have a folder containing a lot of files. The following command instructs Git to stop checking that folder every time for changes, as it won’t have any. The —assume-unchanged index will be reset, and files will be overwritten if there are upstream changes to the file or folder when you pull. As the idea is ignoring the local changes, you can delete all of them by running the git reset —hard command.
For getting a list of directories or files that are —assume-unchanged , you should run the command below:
The following command will instruct Git that you need your own independent version of the file or folder. After that, any changes made to that file(s) will not be flagged as a change in Git.
What is Git Repository
Git repository serves as special storage for the files of your project. With git repository , it is possible to save the versions of your code, as well as, to have access to them. If you want to create a new git repository , you need to run the git init command. It is used once to initialize a new repository. Another one-time operation is the git clone command, with the help of which you can create a clone of an existing repository.
What is the git rm Command
The git rm command can remove particular files or a group of files from a git repository . You can also use it to remove files from the working directory and the staging index. But it is not possible to remove a file only from the working directory. The git rm command doesn’t remove branches either.
What is a .gitignore File
Generally, Git classifies the files of the working copy as 3 things:
- A tracked file — the one that has been committed or staged before;
- An untracked file- the one that has not been committed or staged before;
- An ignored file- the one that has been commanded to be ignored.
In your working copy, the files are visible for git as untracked, tracked or ignored. Ignored files are those you told Git to ignore. In case you want to commit this kind of files, you should derive them from the repository source. The ignored files are stored in the file called .gitignore. There is no git ignore command, hence you should edit and commit these files by hand.
Как перестать отслеживать файл в локальном Git?
Подскажите новичку в Git:
есть локальный репозиторий (для изучения гит). Есть некие файлы, и один из файлов ‘two.txt’ (уже проиндексированный), нужно оставить на диске, но исключить из области отслеживания.
В книге «GIT для профессионального программиста» прописана команда, которой я и воспользовался:
git rm —cached two.txt
Статус показал следующую картину:
Вопрос: как избавиться от отображения информации, что файл не отслеживается?
Я закоммитил это изменение:
Однако строка с информацией о наличии неотслеживаемого файла все равно присутствует:
В чем моя ошибка или непонимание?
В книге этот момент вообще не освещён.
На Тостере есть три сообщения по этому вопросу, но они лишь повторяют те команды, которые я вводил и безрезультатно.
Спасибо.
How to stop tracking and ignore changes to a file in Git?
I have cloned a project that includes some .csproj files. I don’t need/like my local csproj files being tracked by Git (or being brought up when creating a patch), but clearly they are needed in the project.
I have added *.csproj to my LOCAL .gitignore , but the files are already in the repo.
When I type git status, it shows my changes to csproj which I am not interested in keeping track of or submitting for patches.
How do I remove the «tracking of» these files from my personal repo (but keep them in the source so I can use them) so that I don’t see the changes when I do a status (or create a patch)?
Is there a correct/canonical way to handle this situation?
![]()
20 Answers 20
Just calling git rm —cached on each of the files you want to remove from revision control should be fine. As long as your local ignore patterns are correct you won’t see these files included in the output of git status.
Note that this solution removes the files from the repository, so all developers would need to maintain their own local (non-revision controlled) copies of the file
To prevent git from detecting changes in these files you should also use this command:
What you probably want to do: (from below @Ryan Taylor answer)
- This is to tell git you want your own independent version of the file or folder. For instance, you don’t want to overwrite (or delete) production/staging config files.
There are 3 options; you probably want #3
This will keep the local file for you, but will delete it for anyone else when they pull.
git rm —cached <file-name> or git rm -r —cached <folder-name>
This is for optimization, like a folder with a large number of files, e.g. SDKs that probably won’t ever change. It tells Git to stop checking that huge folder every time for changes, locally, since it won’t have any. The assume-unchanged index will be reset and file(s) overwritten if there are upstream changes to the file/folder (when you pull).
This is to tell Git that you want your own independent version of the file or folder. For instance, you don’t want to overwrite (or delete) production/staging config files.
It’s important to know that git update-index will not propagate with Git, so each user will have to run it independently.
![]()
If you do git update-index —assume-unchanged file.csproj , git won’t check file.csproj for changes automatically: that will stop them coming up in git status whenever you change them. So you can mark all your .csproj files this way- although you’ll have to manually mark any new ones that the upstream repo sends you. (If you have them in your .gitignore or .git/info/exclude , then ones you create will be ignored)
I’m not entirely sure what .csproj files are. if they’re something along the lines of IDE configurations (similar to Eclipse’s .eclipse and .classpath files) then I’d suggest they should simply never be source-controlled at all. On the other hand, if they’re part of the build system (like Makefiles) then clearly they should— and a way to pick up optional local changes (e.g. from a local.csproj a la config.mk) would be useful: divide the build up into global parts and local overrides.
This is a two step process:
Remove tracking of file/folder — but keep them on disk — using
Now they do not show up as «changed» but still show as
Add them to .gitignore
![]()
The accepted answer still did not work for me
git rm -r —cached .
git add .
git commit -m «fixing .gitignore»
Found the answer from here
![]()
Forgot your .gitignore?
If you have the entire project locally but forgot to add you git ignore and are now tracking some unnecessary files use this command to remove everything
make sure you are at the root of the project.
Then you can do the usual
Commit
Conclusion
Hope this helps out people who have to make changes to their .gitignore or forgot it all together.
- It removes the entire cache
- Looks at your .gitignore
- Adds the files you want to track
- Pushes to your repo
![]()
As pointed out in other answers, the selected answer is wrong.
The answer to another question suggests that it may be skip-worktree that would be required.
That answer links to an article (http://fallengamer.livejournal.com/93321.html) and quotes the article with a nice summary of the difference between —assume-unchanged and —skip-worktree as follows:
—assume-unchanged assumes that a developer shouldn’t change a file. This flag is meant for improving performance for not-changing folders like SDKs.
—skip-worktree is useful when you instruct git not to touch a specific file ever because developers should change it. For example, if the main repository upstream hosts some production-ready configuration files and you don’t want to accidentally commit changes to those files, —skip-worktree is exactly what you want.
All credit to borealid for their research and answer. Including this info here purely for the convenience of others.
To prevent monitoring a file or path by git
And to revert it back use
To save some time the rules you add to your .gitignore can be used for removing multiple files/folders i.e.
git rm —cached app/**/*.xml
git rm —cached -r app/widgets/yourfolder/
Lots of people advise you to use git update-index —assume-unchanged . Indeed, this may be a good solution, but only in the short run.
What you probably want to do is this: git update-index —skip-worktree .
(The third option, which you probably don’t want is: git rm —cached . It will keep your local file, but will be marked as removed from the remote repository.)
Difference between the first two options?
- assume-unchanged is to temporary allow you to hide modifications from a file. If you want to hide modifications done to a file, modify the file, then checkout another branch, you’ll have to use no-assume-unchanged then probably stash modifications done.
- skip-worktree will follow you whatever the branch you checkout, with your modifications!
Use case of assume-unchanged
It assumes this file should not be modified, and gives you a cleaner output when doing git status . But when checking out to another branch, you need to reset the flag and commit or stash changes before so. If you pull with this option activated, you’ll need to solve conflicts and git won’t auto merge. It actually only hides modifications ( git status won’t show you the flagged files).
I like to use it when I only want to stop tracking changes for a while + commit a bunch of files ( git commit -a ) related to the same modification.
Use case of skip-worktree
You have a setup class containing parameters (eg. including passwords) that your friends have to change accordingly to their setup.
- 1: Create a first version of this class, fill in fields you can fill and leave others empty/null.
- 2: Commit and push it to the remote server.
- 3: git update-index —skip-worktree MySetupClass.java
- 4: Update your configuration class with your own parameters.
- 5: Go back to work on another functionnality.
The modifications you do will follow you whatever the branch. Warning: if your friends also want to modify this class, they have to have the same setup, otherwise their modifications would be pushed to the remote repository. When pulling, the remote version of the file should overwrite yours.
PS: do one or the other, but not both as you’ll have undesirable side-effects. If you want to try another flag, you should disable the latter first.